component¶
Warning
This section covers the implementations of JAnim, which may be quite complex. If you do not have the need to delve into the source code, you may choose to read it at your discretion.
- class janim.components.component.CheckComponentMethods(name: str, bases: tuple[type, ...], attrdict: dict, *, impl=False)¶
Bases:
typeAll subclasses of
Componentare checked to ensure that they implement thecopy,_become, andnot_changedmethods.Meaning:
copy:Calling this function produces a copy of the component.
In this function,
cmpt_copy = super().copy()should be used to obtain a copy of the component. The relevant copying operations should then be performed on this object, and finally thecmpt_copyobject should be returned._become:Takes another component as an argument (most likely one of the same type; this behavior is not yet stable and may be clarified in the future).
In this function,
super()._become(other)should first be used to handle the parent class’s logic (if it exists). The current component’s logic should then be handled, ultimately ensuring that the current component’s data matches that of the target component.not_changed:This function should return a
boolindicating whether it can be determined that the component’s data has not changed, and is used to detect modifications to the component.For example, when the
_pointsdata ofCmpt_Pointshas not been modified, theidof its underlying NumPy array remains unchanged. After the data is modified, however, it becomes a completely different NumPy array with a differentid.Therefore,
Cmpt_PointsreturnsFalsewhenever its underlying NumPy arrays cannot be determined to be the same usingis(even if the data has been modified and then changed back to the same values, it will still returnFalse).
Additionally, for components that do not contain any additional data and only provide additional methods,
impl=Truecan be used to skip the check for that subclass.
- class janim.components.component.Component¶
Bases:
Generic- class BindInfo(decl_cls: type[Item], at_item: Item, key: str, _computed_caches: dict[relation.FlagHandle, Any] = <factory>)¶
Bases:
objectEncapsulation of component definition information
- Parameters:
decl_cls – The class in which it is declared in the form
xxx = CmptInfo(...); if both a class and its parent class havexxx = CmptInfo(...), thendecl_clsis the parent class.at_item – The item object to which this component object is currently bound
key – The variable name of this component object within the item
For example:
class MyCmpt(Component): ... class MyItem(Item): cmpt1 = CmptInfo(MyCmpt[Self]) cmpt2 = CmptInfo(MyCmpt[Self]) class MyItem2(MyItem): cmpt3 = CmptInfo(MyCmpt[Self]) item = MyItem() # item.cmpt1.bind_info is consistent with BindInfo(MyItem, item, 'cmpt1') # item.cmpt2.bind_info is consistent with BindInfo(MyItem, item, 'cmpt2') item2 = MyItem2() # item2.cmpt1.bind_info is consistent with BindInfo(MyItem, item2, 'cmpt1') # item2.cmpt3.bind_info is consistent with BindInfo(MyItem2, item2, 'cmpt3')
- key: str¶
- mark_computed_for(flag_handle: FlagHandle, data: Any) None¶
- reset_computed_for(flag_handle: FlagHandle) None¶
- reset_computed_for_func(func: Callable) None¶
- reset_computed_for_list(lst: list[FlagHandle]) None¶
- reset_computed_for_all() None¶
- init_bind(bind: BindInfo) None¶
Used for
Item._init_componentsSubclasses can inherit this function to perform item-related processing
- copy() Self¶
- become(other) Self¶
- not_changed(other) bool¶
- get_same_cmpt(item: Item, *, use_mock: bool = False, create_mock: Literal[False]) Self | None¶
- get_same_cmpt(item: Item, *, use_mock: bool = False, create_mock: Literal[True] = True) Self
Get the component in the
itemobject with the sameself.bind.keyas this component. By default, a mock will be created if necessary.Note: If
self.bindis invalid, an attribute error will be raised when attempting to access an attribute ofNone. However, this is not checked within the function; if necessary, the intended approach is to perform the check outside the function.- Parameters:
item – The item from which to get the component
use_mock – Whether a previously created mock can be used when the corresponding component is unavailable
create_mock – Whether to create a mock based on
astype()when the corresponding component is unavailable
- Returns:
The resulting component; may return
Noneifcreate_mock=False
- walk_same_cmpt_of_self_and_descendants(root_only: bool = False, *, use_mock: bool = False, create_mock: bool = False, unordered: bool = False) Iterable[Self]¶
Traverse this component and the corresponding components in the descendant items of its containing item. By default, no mocks are created.
If
root_onlyis set orself.bindis invalid, descendant components are ignored.- Parameters:
root_only – Whether to ignore descendant components and only
yieldthis component
For the remaining parameters, see the documentation for
walk_same_cmpt_of_descendants().
- walk_same_cmpt_of_descendants(*, use_mock: bool = False, create_mock: bool = False, unordered: bool = False) Iterable[Self]¶
Traverse the corresponding components in the descendant items of the containing item. By default, no mocks are created.
Note: If
self.bindis invalid, an attribute error will be raised when attempting to access an attribute ofNone. However, this is not checked within the function; if necessary, the intended approach is to perform the check outside the function.- Parameters:
use_mock – Whether a previously created mock can be used when the corresponding component is unavailable
create_mock – Whether to create a mock based on
astype()when the corresponding component is unavailableunordered – Whether to allow traversal without guaranteeing order. If specified, performance may be better for complex structures.
- property r: ItemT¶
The item it resides in, facilitating chained calls to other components within the same item.
- classmethod align_for_interpolate(cmpt1, cmpt2) AlignedData[Self]¶
- interpolate(cmpt1, cmpt2, alpha: float, *, path_func=None) None¶
- class janim.components.component.CmptInfo(cls: type[T], *args, **kwargs)¶
Bases:
GenericWhen defining components in a class, you should use this class to wrap
For example:
class MyItem(Item): # Wrong! # cmpt1 = MyCmpt() # Correct cmpt1 = CmptInfo(MyCmpt[Self]) # Wrong # cmpt2 = MyCmptWithArgs(1) # Correct cmpt2 = CmptInfo(MyCmptWithArgs[Self], 1)
- janim.components.component.CmptGroup(*cmpt_info_list: CmptInfo) CmptInfo¶
Used to package multiple components, allowing them to be called simultaneously
For example:
class MyItem(Item): stroke = CmptInfo(Cmpt_Rgbas[Self]) fill = CmptInfo(Cmpt_Rgbas[Self]) color = CmptGroup(stroke, fill) item = MyItem() item.stroke.set(...) # Only the method of stroke be called item.color.set(...) # the methods of stroke and fill are both called