mxutils.GetTreeString¶
-
mxutils.
GetTreeString
(node: Any, childrenAttr: str | None = None, getDownFunc: str | None = 'GetDown', getNextFunc: str | None = 'GetNext', indentWidth: int = 3, callback: Optional[Callable, None] = None) → str¶ - Returns a string representation of a tree structure.The tree of #node is traversed using either a list of child nodes attached to each node under the attribute #childrenAttr, or by calling the functions #getDownFunc and #getNextFunc on each node to get the first child and the next sibling, respectively.
Examples:
# A node type to print trees for, this is a node type which stores its children as a list. class Node: def __init__(self, value: int) -> None: self.value = value self.children = [] def __str__(self) -> str: return str(self.value) def AddChild(self, child: 'Node') -> None: self.children.append(child) # Create a tree structure. tree: Node = Node(1) tree.AddChild(Node(2)) tree.AddChild(Node(3)) tree.children[1].AddChild(Node(4)) # Print the tree structure by telling the function to use the #children attribute. print(GetTreeString(tree, "children"))
1 ├─ 2 └─ 3 └─ 4
# Example for printing a node tree for a node type which can be navigated using a function which # yields the first child and a function which yields the next sibling, here at the example of a # Cinema 4D BaseObject tree. # Get the first object in the active document. doc: c4d.documents.BaseDocument = c4d.documents.GetActiveDocument() op: c4d.BaseObject = doc.GetFirstObject() if not op: raise ValueError("No object found in the document.") # Print the object tree of #using its navigation functions 'GetDown' and 'GetNext'. We could # just invoke #GetTreeString(op) here since 'GetDown' and 'GetNext' are the default values. print(GetTreeString(op, getDownFunc="GetDown", getNextFunc="GetNext")) print("") def PrintObject(node: c4d.BaseObject) -> str: '''Returns the name of an object and the names of its tags as a string. ''' return f"{node.GetName()} {' '.join([f"[{n.GetName()}]" for n in node.GetTags()])}" # We can also customize the string representation of each node by passing a callback function. print(GetTreeString(op, callback=PrintObject))
<c4d.BaseObject object called Cone/Cone with ID 5162 at 140511902113152> ├── <c4d.BaseObject object called Cube/Cube with ID 5159 at 140511902142592> └── <c4d.BaseObject object called Sphere/Sphere with ID 5160 at 140511902160768> └── <c4d.BaseObject object called Torus/Torus with ID 5163 at 140511902065024> Cone [Display] [Phong] [Material] ├── Cube [Phong] └── Sphere [Phong] [Material] └── Torus [Phong]
- Parameters
node (typing.Any) – The root node of the tree.
childrenAttr (str, optional) – The attribute name to access the children of a node, defaults to None.
getDownFunc (str, optional) – The function name to get the first child of a node, defaults to “GetDown”.
getNextFunc (str, optional) – The function name to get the next sibling of a node, defaults to “GetNext”.
indentWidth (int, optional) – The width of the tree indentation for one level, defaults to 3.
callback (typing.Callable, optional) – A callback function to customize the string representation of each node, defaults to None.
- Returns
The string representation of the tree structure.
- Return type
str
- Raises
ValueError – If the callback is provided but not callable.
ValueError – If the childrenAttr is not found on the node.
ValueError – If the getDownFunc is not found on the node.
ValueError – If the getNextFunc is not found on the node.
ValueError – If neither childrenAttr nor getDownFunc and getNextFunc are provided.