relation

class janim.items.relation.Relation

基类:Refreshable, Generic

定义了有向无环图的包含关系以及一些实用操作

也就是,对于每个对象:

  • self.parents 存储了与其直接关联的父对象

  • self.children 存储了与其直接关联的子对象

  • 使用 add() 建立对象间的关系

  • 使用 remove() 取消对象间的关系

  • 使用 get_parents()get_children() 获取对象列表的副本

  • ancestors() 表示与其直接关联的祖先对象(包括父对象,以及父对象的父对象,......)

  • descendants() 表示与其直接关联的后代对象(包括子对象、以及子对象的子对象,......)

  • 对于 ancestors() 以及 descendants()
    • 不包含调用者自身并且返回的列表中没有重复元素

    • 对象顺序是 DFS 顺序

property parents

父物件列表的一份拷贝

property children

子物件列表的一份拷贝

index(obj: GRelT) int

获取子物件在列表中的索引位置

参数:

obj -- 要查找的子物件

返回:

子物件的索引位置

抛出:

ValueError -- 子物件不在列表中

mark_refresh(name: str, *, recurse_up=False, recurse_down=False) Self

标记指定的 name 需要进行更新

add(*objs: GRelT, prepend=False, insert=None) Self

向该物件添加子物件

参数:
  • objs -- 要添加的子物件

  • prepend -- 默认为 False,如果为 True,那么插入到子物件列表的开头

insert(index: int, *objs: GRelT) Self

在指定索引位置插入子物件

参数:
  • index -- 插入位置的索引

  • objs -- 要插入的子物件

remove(*objs: GRelT) Self

从该物件移除子物件

参数:

objs -- 要移除的子物件

shuffle() Self

随机打乱子物件的顺序

备注

该方法使用 random.shuffle() 进行随机打乱

如果需要可重复的随机结果,请在调用此方法前使用 random.seed() 设置随机数种子

clear_parents() Self

清空父物件

clear_children() Self

清空子物件

ancestors() list[GRelT]

获得祖先物件列表

descendants() list[GRelT]

获得后代物件列表

walk_ancestors(base_cls: None = None) Generator[GRelT, None, None]
walk_ancestors(base_cls: type[RelT]) Generator[RelT, None, None]

遍历祖先节点中以 base_cls (缺省则遍历全部)为基类的物件

walk_descendants(base_cls: None = None) Generator[GRelT, None, None]
walk_descendants(base_cls: type[RelT]) Generator[RelT, None, None]

遍历后代节点中以 base_cls (缺省则遍历全部)为基类的物件

walk_self_and_ancestors(root_only=False) Generator[Self | GRelT, None, None]

遍历自己以及祖先节点

walk_self_and_descendants(root_only=False) Generator[Self | GRelT, None, None]

遍历自己以及后代节点

walk_nearest_ancestors(base_cls: type[RelT]) Generator[RelT, None, None]

遍历祖先节点中以 base_cls 为基类的物件,但是排除已经满足条件的物件的祖先物件

walk_nearest_descendants(base_cls: type[RelT]) Generator[RelT, None, None]

遍历后代节点中以 base_cls 为基类的物件,但是排除已经满足条件的物件的后代物件