relation

class janim.items.relation.Relation

Bases: Refreshable, Generic

Defines the containment relationship of a directed acyclic graph and some practical operations

That is, for each object:

  • self.parents stores the directly associated parent objects

  • self.children stores the directly associated child objects

  • Use add() to establish relationships between objects

  • Use remove() to cancel relationships between objects

  • Use get_parents() and get_children() to get copies of the object lists

  • ancestors() represents the ancestor objects directly associated with it (including parent objects and parent objects’ parent objects, …)

  • descendants() represents the descendant objects directly associated with it (including child objects, and child objects’ child objects, …)

  • For ancestors() and descendants() :
    • Does not include the caller itself, and the returned list has no duplicate elements

    • The order of the objects is DFS order

property parents

A copy of the parent-item list

property children

A copy of the child-item list

index(obj: GRelT) int

Get the index of a child item in the list

Parameters:

obj – Child item to find

Returns:

Index of the child item

Raises:

ValueError – Child item not in the list

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

Marks the specified name as needing an update

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

Add child items to this item

Parameters:
  • objs – Child items to add

  • prepend – If insert=True (default is False), then insert at the beginning of the list of child items.

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

Insert child items at the specified index

Parameters:
  • index – Index at which to insert

  • objs – Child items to insert

remove(*objs: GRelT) Self

Remove child items from this item

Parameters:

objs – Child items to remove

shuffle() Self

Randomly shuffle the order of child items

Note

This method uses random.shuffle() to shuffle

For reproducible randomness, set the seed with random.seed() before calling this method

clear_parents() Self

Clear parent items

clear_children() Self

Clear child items

ancestors() list[GRelT]

Get a list of ancestor items

descendants() list[GRelT]

Get a list of descendant items

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

Traverse ancestor nodes with base_cls (default to traverse all) as the base class

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

Traverse descendant nodes with base_cls (default to traverse all) as the base class

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

Traverse self and ancestor nodes

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

Traverse self and descendant nodes

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

Traverse ancestor nodes with base_cls as the base class, but exclude the ancestors of items that already meet the conditions

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

Traverse descendant nodes with base_cls as the base class, but exclude the descendants of items that already meet the conditions