Coverage for lib/lottie/parsers/glaxnimate_helpers.py: 34%

45 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-20 16:17 +0100

1try: 

2 import glaxnimate 

3 has_glaxnimate = True 

4except ImportError: 

5 has_glaxnimate = False 

6 

7import json 

8from ..nvector import NVector, Point 

9 

10 

11def convert(animation, exporter_slug): 

12 with glaxnimate.environment.Headless(): 

13 document = glaxnimate.model.Document("") 

14 glaxnimate.io.registry.from_slug("lottie").load(document, json.dumps(animation.to_dict()).encode("utf8")) 

15 return glaxnimate.io.registry.from_slug(exporter_slug).save(document) 

16 

17 

18def serialize(animation, serializer_slug, time=0): 

19 with glaxnimate.environment.Headless(): 

20 document = glaxnimate.model.Document("") 

21 glaxnimate.io.registry.from_slug("lottie").load(document, json.dumps(animation.to_dict()).encode("utf8")) 

22 document.current_time = time 

23 return glaxnimate.io.registry.serializer_from_slug(serializer_slug).serialize([document.main]) 

24 

25 

26class GlaxnimateRenderer: 

27 def __init__(self, animation, serializer_slug, dpi): 

28 self.context = glaxnimate.environment.Headless() 

29 self.serializer_slug = serializer_slug 

30 self.serializer = None 

31 self.document = None 

32 self.animation = animation 

33 # TODO dpi 

34 

35 def __enter__(self): 

36 self.context.__enter__() 

37 self.serializer = glaxnimate.io.registry.serializer_from_slug(self.serializer_slug) 

38 self.document = glaxnimate.model.Document("") 

39 glaxnimate.io.registry.from_slug("lottie").load(self.document, json.dumps(self.animation.to_dict()).encode("utf8")) 

40 return self 

41 

42 def __exit__(self, *a, **k): 

43 self.context.__exit__(*a, **k) 

44 

45 def serialize(self, frame, fp): 

46 self.document.current_time = frame 

47 data = self.serializer.serialize([self.document.main]) 

48 if fp: 

49 fp.write(data) 

50 return data 

51 

52 

53def color_to_glaxnimate(color: NVector): 

54 return glaxnimate.utils.Color(*(color * 255).components) 

55 

56 

57def color_from_glaxnimate(color): 

58 return NVector(color.red / 255., color.green / 255., color.blue / 255., color.alpha / 255.) 

59 

60 

61def point_from_glaxnimate(point): 

62 return Point(point.x, point.y)