File indexing completed on 2025-01-19 04:00:00

0001 #!/usr/bin/env python3
0002 import sys
0003 import os
0004 import math
0005 sys.path.insert(0, os.path.join(
0006     os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
0007     "lib"
0008 ))
0009 from lottie.utils import script
0010 from lottie import objects
0011 from lottie.utils import animation as anutils
0012 from lottie import Point, Color
0013 from lottie.utils import ik
0014 
0015 
0016 an = objects.Animation(180)
0017 
0018 layer = objects.ShapeLayer()
0019 an.add_layer(layer)
0020 
0021 g = layer.add_shape(objects.Group())
0022 b1 = g.add_shape(objects.Ellipse())
0023 b1.size.value = Point(20, 20)
0024 g.add_shape(objects.Fill(Color(1, 1, 0)))
0025 
0026 g = layer.add_shape(objects.Group())
0027 b2 = g.add_shape(objects.Ellipse())
0028 b2.size.value = Point(20, 20)
0029 g.add_shape(objects.Fill(Color(0, 1, 0)))
0030 
0031 
0032 for i in range(an.out_point):
0033     t = i / (an.out_point-1)
0034     p1 = Point(math.cos(t*math.pi*2)/2+1, math.sin(t*math.pi*2)/2+1) * 256
0035     b1.position.add_keyframe(i, p1)
0036 
0037     p2 = Point(math.sin(2*t*math.pi*2)/2+1, -math.cos(t*math.pi*2)/2+1) * 256
0038     b2.position.add_keyframe(i, p2)
0039 
0040 
0041 def chain_bezier(chain):
0042     b = objects.Bezier()
0043     for seg in chain.joints:
0044         b.add_point(seg)
0045     return b
0046 
0047 
0048 g = layer.add_shape(objects.Group())
0049 s = g.add_shape(objects.Path())
0050 g.add_shape(objects.Stroke(Color(1, 0, 0), 5))
0051 
0052 chain = ik.Chain(Point(0, 0))
0053 chain.add_joint(Point(50, 0))
0054 chain.add_joint(Point(150, 0))
0055 chain.add_joint(Point(350, 0))
0056 chain.add_joint(Point(400, 0))
0057 #chain.add_joint(Point(500, 0))
0058 
0059 
0060 octmaster = ik.Chain(Point(512, 512), True)
0061 octmaster.add_joint(Point(450, 450))
0062 octmaster.add_joint(Point(400, 400))
0063 octmaster.add_joint(Point(450, 450))
0064 octmaster.add_joint(Point(512, 512))
0065 
0066 oct = ik.Octopus(octmaster)
0067 ch1 = oct.add_chain("ch1")
0068 ch1.add_joint(Point(412, 512))
0069 ch1.add_joint(Point(312, 512))
0070 ch2 = oct.add_chain("ch2")
0071 ch2.add_joint(Point(512, 412))
0072 ch2.add_joint(Point(512, 312))
0073 
0074 
0075 g = layer.add_shape(objects.Group())
0076 octshapes = {
0077     n: g.add_shape(objects.Path())
0078     for n in oct.chains.keys()
0079 }
0080 g.add_shape(objects.Stroke(Color(0, 0, 1), 5))
0081 
0082 
0083 for i in range(an.out_point):
0084     t = i / (an.out_point-1)
0085     p1 = Point(math.cos(t*math.pi*2)/2+1, math.sin(t*math.pi*2)/2+1) * 256
0086     p2 = Point(math.sin(2*t*math.pi*2)/2+1, -math.cos(t*math.pi*2)/2+1) * 256
0087     chain.reach(p1)
0088     s.shape.add_keyframe(i, chain_bezier(chain))
0089 
0090     oct.reach({"ch1": p1, "ch2": p2})
0091     for name, ochain in oct.chains.items():
0092         octshapes[name].shape.add_keyframe(i, chain_bezier(ochain))
0093 
0094 script.script_main(an)