4 from ..nvector
import NVector
5 from ..utils.color
import Color, ColorMode
10 Base class for Lottie JSON objects bindings
14 Serializes into a JSON object fit for the Lottie format
16 raise NotImplementedError
21 Loads from a JSON object
22 @returns An instance of the class
24 raise NotImplementedError
28 Returns a copy of the object
30 raise NotImplementedError
35 Hack to counter-hack the hack in enum meta
38 classdict[
"__reduce_ex__"] =
lambda *a, **kw:
None
39 return super().
__new__(cls, name, bases, classdict)
44 Base class for enum-like types in the Lottie JSON structure
59 List tag for some weird values in the Lottie JSON
64 class LottieValueConverter:
66 Factory for property types that require special conversions
71 self.
namename = name
or "%s but displayed as %s" % (self.
pypy.__name__, self.
lottielottie.__name__)
74 return self.
lottielottie(val)
90 Lottie <-> Python property mapper
92 def __init__(self, name, lottie, type=float, list=False, cond=None):
108 Returns the value of the property from a Python object
110 return getattr(obj, self.
namename)
112 def set(self, obj, value):
114 Sets the value of the property from a Python object
116 if isinstance(getattr(obj.__class__, self.
namename,
None), property):
118 return setattr(obj, self.
namename, value)
122 Returns the value for this property from a JSON dict representing the parent object
123 @returns The loaded value or @c None if the property is not in @p lottiedict
125 if self.
lottielottie
in lottiedict:
126 return self.
loadload(lottiedict[self.
lottielottie])
131 Loads from a Lottie dict into an object
133 if self.
condcond
and not self.
condcond(lottiedict):
139 Loads the property from a JSON value
140 @returns the Python equivalent of the JSON value
142 if self.
listlist
is PseudoList
and isinstance(lottieval, list):
144 elif self.
listlist
is True:
145 return list(filter(
lambda x: x
is not None, (
151 def _load_scalar(self, lottieval):
152 if lottieval
is None:
154 if inspect.isclass(self.
typetype)
and issubclass(self.
typetype, LottieBase):
155 return self.
typetype.
load(lottieval)
156 elif isinstance(self.
typetype, type)
and isinstance(lottieval, self.
typetype):
158 elif isinstance(self.
typetype, LottieValueConverter):
159 return self.
typetype.lottie_to_py(lottieval)
160 elif self.
typetype
is NVector:
162 elif self.
typetype
is Color:
163 return Color(*lottieval)
164 if isinstance(lottieval, list)
and lottieval:
165 lottieval = lottieval[0]
166 return self.
typetype(lottieval)
170 Converts the value of the property as from @p obj into a JSON value
171 @param obj LottieObject with this property
173 val = self.
getget(obj)
174 if isinstance(self.
typetype, LottieValueConverter):
175 val = self.
typetype.py_to_lottie(val)
179 if self.
listlist
is PseudoList:
180 if not isinstance(obj, list):
184 def _basic_to_dict(self, v):
185 if isinstance(v, LottieBase):
187 elif isinstance(v, Color):
189 elif isinstance(v, NVector):
191 elif isinstance(v, (list, tuple)):
193 elif isinstance(v, (int, str, bool)):
195 elif isinstance(v, float):
200 raise Exception(
"Unknown value {!r}".format(v))
203 return "<LottieProp %s:%s>" % (self.
namename, self.
lottielottie)
206 if isinstance(value, (list, tuple)):
207 return [self.
clone_valueclone_value(v)
for v
in value]
208 if isinstance(value, (LottieBase, NVector)):
210 if isinstance(value, (int, float, bool, str))
or value
is None:
212 raise Exception(
"Could not convert {!r}".format(value))
219 if type(base) == cls:
221 attr[
"_props"] = props + attr.get(
"_props", [])
222 return super().
__new__(cls, name, bases, attr)
227 @brief Base class for mapping Python classes into Lottie JSON objects
234 prop.lottie: prop.to_dict(self)
235 for prop
in self._props
236 if prop.get(self)
is not None
241 if "__pyclass" in lottiedict:
242 return CustomObject.load(lottiedict)
247 for prop
in cls._props:
248 prop.load_into(lottiedict, obj)
252 def _load_get_class(cls, lottiedict):
255 def find(self, search, propname="name"):
257 @param search The value of the property to search
258 @param propname The name of the property used to search
259 @brief Recursively searches for child objects with a matching property
261 if getattr(self, propname,
None) == search:
263 for prop
in self._props:
265 if isinstance(v, LottieObject):
266 found = v.find(search, propname)
269 elif isinstance(v, list)
and v
and isinstance(v[0], LottieObject):
271 found = obj.find(search, propname)
276 def find_all(self, type, predicate=None, include_self=True):
278 Find all child objects that match a predicate
279 @param type Type (or tuple of types) of the objects to match
280 @param predicate Function that returns true on the objects to find
281 @param include_self Whether should counsider `self` for a potential match
284 if isinstance(self, type)
and include_self:
285 if not predicate
or predicate(self):
288 for prop
in self._props:
291 if isinstance(v, LottieObject):
292 for found
in v.find_all(type, predicate,
True):
294 elif isinstance(v, list)
and v
and isinstance(v[0], LottieObject):
296 for found
in child.find_all(type, predicate,
True):
300 obj = self.__class__()
301 for prop
in self._props:
303 prop.set(obj, prop.clone_value(v))
307 for prop
in self._props:
309 prop.set(other, prop.clone_value(v))
312 return type(self).__name__
317 @brief Simple iterator to generate increasing integers
329 Allows extending the Lottie shapes with custom Python classes
331 wrapped_lottie = LottieObject
338 ld = lottiedict.copy()
339 classname = ld.pop(
"__pyclass")
340 modn, clsn = classname.rsplit(
".", 1)
341 subcls = getattr(importlib.import_module(modn), clsn)
343 for prop
in subcls._props:
344 prop.load_into(lottiedict, obj)
345 obj.wrapped = subcls.wrapped_lottie.load(ld)
349 obj = self.__class__(**self.to_pyctor())
355 dict[
"__pyclass"] =
"{0.__module__}.{0.__name__}".format(self.__class__)
356 dict.update(LottieObject.to_dict(self))
359 def _build_wrapped(self):
367 DONT_RECURSE = object()
370 self.
_process_process(lottie_object)
372 def _process(self, lottie_object):
373 self.
visitvisit(lottie_object)
374 for p
in lottie_object._props:
375 pval = p.get(lottie_object)
377 if isinstance(pval, LottieObject):
379 elif isinstance(pval, list)
and pval
and isinstance(pval[0], LottieObject):
Allows extending the Lottie shapes with custom Python classes.
def clone(self)
Returns a copy of the object.
def load(cls, lottiedict)
Loads from a JSON object.
def to_dict(self)
Serializes into a JSON object fit for the Lottie format.
Simple iterator to generate increasing integers.
Base class for Lottie JSON objects bindings.
def to_dict(self)
Serializes into a JSON object fit for the Lottie format.
def load(cls, lottiedict)
Loads from a JSON object.
def clone(self)
Returns a copy of the object.
Base class for enum-like types in the Lottie JSON structure.
def to_dict(self)
Serializes into a JSON object fit for the Lottie format.
def clone(self)
Returns a copy of the object.
def load(cls, lottieint)
Loads from a JSON object.
Base class for mapping Python classes into Lottie JSON objects.
def clone_into(self, other)
def _load_get_class(cls, lottiedict)
def clone(self)
Returns a copy of the object.
def find_all(self, type, predicate=None, include_self=True)
Find all child objects that match a predicate.
def load(cls, lottiedict)
Loads from a JSON object.
def to_dict(self)
Serializes into a JSON object fit for the Lottie format.
def find(self, search, propname="name")
Recursively searches for child objects with a matching property.
Lottie <-> Python property mapper.
lottie
Name of the Lottie JSON property.
cond
Condition on when the property is loaded from the Lottie JSON.
name
Name of the Python property.
def load_into(self, lottiedict, obj)
Loads from a Lottie dict into an object.
def _basic_to_dict(self, v)
def load_from_parent(self, lottiedict)
Returns the value for this property from a JSON dict representing the parent object.
def __init__(self, name, lottie, type=float, list=False, cond=None)
def get(self, obj)
Returns the value of the property from a Python object.
def load(self, lottieval)
Loads the property from a JSON value.
def clone_value(self, value)
list
Whether the property is a list of self.type.
def _load_scalar(self, lottieval)
type
Type of the property.
def set(self, obj, value)
Sets the value of the property from a Python object.
def to_dict(self, obj)
Converts the value of the property as from obj into a JSON value.
Factory for property types that require special conversions.
def py_to_lottie(self, val)
def lottie_to_py(self, val)
def __init__(self, py, lottie, name=None)
def visit_property(self, object, property, value)
def __call__(self, lottie_object)
def _process(self, lottie_object)
List tag for some weird values in the Lottie JSON.