Last active
April 9, 2018 09:12
-
-
Save fwilleke80/0219bd000456513b8556f49f61f27b22 to your computer and use it in GitHub Desktop.
[C4D] This script contains two generator functions that allow you to effortlessly iterate over objects hierarchies in Cinema 4D, as well as some simple testing code that will iterate the hierarchy of the current document (starting form the selected object), and print it to the console.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import c4d | |
""" | |
This script demonstrates two generator functions, AllChildren() and AllParents(). | |
To test this, use any scene with a reasonably complex hierarchy, select any one object and run the script. | |
""" | |
def AllChildren(startOp, maxDepth = 0, currentRecursionDepth = 1): | |
"""Yield all objects that are part of startOp's child hierarchy. | |
Set maxDepth to a value != 0 to restrict recursion to a certain depth. | |
""" | |
# Start with first child | |
op = startOp.GetDown() | |
# Iterate over child's siblings | |
while op: | |
yield op | |
# If maxRecursionDepth is 0, or hasn't been reached | |
# yet, iterate over each sibling's children | |
if maxDepth == 0 or (currentRecursionDepth < maxDepth): | |
for child in AllChildren(op, maxDepth = maxDepth, currentRecursionDepth = currentRecursionDepth + 1): | |
yield child | |
op = op.GetNext() | |
def AllParents(startOp, maxDepth = 0): | |
"""Yield all parent objects of startOp. | |
Set maxDepth to a value != 0 to restrict the amount of iterated parent levels. | |
""" | |
# Start with first parent | |
op = startOp.GetUp() | |
# Iterate over parent's parents | |
currentDepth = 1 | |
while op: | |
if maxDepth != 0 and (currentDepth > maxDepth): | |
break | |
yield op | |
currentDepth += 1 | |
op = op.GetUp() | |
if __name__=='__main__': | |
# Get start object and print something | |
startObject = doc.GetActiveObject() | |
print('Children of ' + startObject.GetName() + ':') | |
# Iterate children and print their names | |
for obj in AllChildren(startOp = startObject, maxDepth = 0): | |
print(obj.GetName()) | |
# Some more printing | |
print(' ') | |
print('Parents of ' + startObject.GetName() + ':') | |
# Iterate parents and print their names | |
for obj in AllParents(startOp = startObject, maxDepth = 0): | |
print(obj.GetName()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment