component

Warning

Tips: 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.Component

Bases: Refreshable, Generic

class BindInfo(decl_cls: type[Item], at_item: Item, key: str)

Bases: object

Encapsulation of component definition information

  • decl_cls: In which class the component is declared as xxx = CmptInfo(...); If a class and its parent class both have xxx = CmptInfo(...) , then decl_cls is the parent class

  • at_item: To which item object this component object belongs

  • key: The variable name of this component object

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 与 BindInfo(MyItem, item, 'cmpt1') 一致
# item.cmpt2.bind_info 与 BindInfo(MyItem, item, 'cmpt2') 一致

item2 = MyItem2()

# item2.cmpt1.bind_info 与 BindInfo(MyItem, item2, 'cmpt1') 一致
# item2.cmpt3.bind_info 与 BindInfo(MyItem2, item2, 'cmpt3') 一致
decl_cls: type[Item]
at_item: Item
key: str
init_bind(bind: BindInfo) None

Used for Item._init_components

Subclasses can inherit this function to perform item-related processing

mark_refresh(func: Callable | str, *, recurse_up=False, recurse_down=False) Self

See broadcast_refresh_of_component()

copy() Self
become(other) Self
not_changed(other) bool
get_same_cmpt(item: Item) Self
get_same_cmpt_without_mock(item: Item) Self | None
get_same_cmpt_if_exists(item: Item) Self | None
walk_same_cmpt_of_self_and_descendants_without_mock(root_only: bool = False) Generator[Self, None, None]
walk_same_cmpt_of_descendants_without_mock() Generator[Self, None, None]
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: Generic

When defining components in a class, you should use this class to wrap

For example:

class MyItem(Item):
    # Wrong!
    # cmpt1 = MyCmpt()

    # Right
    cmpt1 = CmptInfo(MyCmpt[Self])

    # Wrong!
    # cmpt2 = MyCmptWithArgs(1)

    # Right
    cmpt2 = CmptInfo(MyCmptWithArgs[Self], 1)
create() Component
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(...)    # 只有 stroke 的被调用 | Only the method of stroke be called
item.color.set(...)     # stroke 和 fill 的都被调用了 | the methods of stroke and fill are both called