Skip to content

Instantly share code, notes, and snippets.

Created August 4, 2011 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1125825 to your computer and use it in GitHub Desktop.
Save anonymous/1125825 to your computer and use it in GitHub Desktop.
C4D Python Hierarchy Iteration / Looping
def iter2DList(atom, doSomething, mode="children"):
"""
Loops through 2D list with different modes.
MODE
"children" = Atom and all below atom
"all" = Atom and all above/below/beside
"this_level" = Atom and all below/beside
"""
if mode == "children":
return iter2DListFromHere(atom, doSomething)
elif mode == "all":
return iter2DListAll(atom, doSomething)
elif mode == "this_level":
return iter2DListThisLevel(atom, doSomething)
def iter2DListFromHere(atom, doSomething, stop=-1):
"""
Loops through "atom" and all atoms below "atom"
Calls the doSomething() method that is passed in as an argument
Useful for all descendents of the c4d.GeListNode class
Note:
You should not enter a value for stop, leave it at the default: -1
"""
#If we've reached the sibling of the starting object, return
if atom == stop:
return None
#Is this the first time? If so, initialize stop
if stop == -1:
stop = atom.GetNext()
#If there isn't an atom or it's the wrong type, stop looping.
if (atom is None) or (not isinstance(atom, c4d.GeListNode)):
return None
#Perform an action on atom
doSomething( atom )
#Try to get the next one down, there's a chance it will be None
iter2DListFromHere(atom.GetDown(), doSomething, stop)
#Try to get the next atomect, there's a chance it will be None
iter2DListFromHere(atom.GetNext(), doSomething, stop)
def findTopAtom(atom):
"""
Finds the top most item in the list that contains "atom"
"""
if not isinstance(atom, c4d.GeListNode):
return None
if atom.GetUp():
findTopAtom(atom.GetUp())
elif atom.GetPred():
findTopAtom(atom.GetPred())
else:
return atom
def iter2DListThisLevel(atom, doSomething):
"""
Loops through all objects at the same or lower level than atom
"""
if (atom is None) or (not isinstance(atom, c4d.GeListNode)):
return None
doSomething(atom)
iter2DListThisLevel(atom.GetDown(), doSomething)
iter2DListThisLevel(atom.GetNext(), doSomething)
def iter2DListAll(atom, doSomething):
"""
Loops through all atoms in the same list as atom
"""
top_atom = findTopAtom(atom)
iter2DListThisLevel(top_atom, doSomething)
return
"""
Change Log:
8/4/2011
Added additional functions, renamed for clarity. Functions are now called from master
function iter2DList
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment