I’ve been diving into Python a lot lately, just because there are things you just want to be able to do yourself. I’ve been talking to a buddy of mine, Charles Wardlaw who is the TD overseer of the Ottoman, and works at March Entertainment. He sometimes challenges me with Python exercies, and sometimes I ask him for ideas(which he offers and they become challenges.)

So between bugging him and Rick, I have been learning a lot of Python in CINEMA 4D. Charles gives me a lot of the theory side of things as well as the standpoint of him being a TD and an Animator he knows how he’d like some scripts to behave, while Rick helps me with SDK stuff in CINEMA. He’s been programming in COFFEE so he knows where to look to find stuff and how to do things. He too is getting into Python. So I usually try to find something and if I have no luck I ask Rick and he usually has a solution.

Which leads me to these useful snippets. Initially the idea was how to copy animation from some objects to other objects. This was originally based on hierarchy(it has now changed) so I needed a way to basically go through an entire hierarchy and perform some action on the object and all it’s children.

def GoDownHierarchy(obj):
if obj is None: return
#Actions can go here
print (obj.GetName())
#End Actions
if (obj.GetDown()):
if (obj.GetNext()):

Now unfortunately, wordpress is kind of killing the necessary tabbing for this to work. But allow me to try to explain what’s going on a bit.

So the first line, def GoDownHierarchy(obj): is just defining our function in Python, no biggie, and it’s also saying when you call this function it needs argument, in this case it’s going to need some kind of object. The next line, if obj is None: return is just a simple check that if you don’t pass an object, nothing happens. Without this, if you passed the function without an object, it should error out I think. After that check, you can put your actions in there. In this case, I’m simply printing out the names of the objects in the hierarchy, nothing fancy.

So after the action we have another if statement: if (obj.GetDown()): This is basically checking to see if there is there is an object as a child of the current object. If there is, it goes to the next line: GoDownHierarchy(obj.GetDown()) This line basically runs the function again. So if the code gets to this line, it goes all the way back to the 3rd line of this code and performs the action again on the object that is a child of the current obj. So effectively when it hits that line, it running the function and passing a new object(obj.GetDown()) to run the function. Then it does this again and again until there is no longer a child and the if (obj.GetDown()): line is FALSE. If it is false, it goes to another if statement if (obj.GetNext()): This line is checking to see if there is a Sibling of the last obj passed to the function. If it is TRUE it will run GoDownHierarchy(obj.GetNext()) which like before it is passing obj,GetNext() into the GoDownHierarchy Function which then causes it to go through the GetDown() conditions(the if statements) so it will go down that hierarchy as well.

So here’s an example of how it works. Here’s an example of my OM. You can see I have Cube 2 selected.


I run the script and I get this in the console:


Note that it didn’t get the numbers 1,14, 15, or 16, because they are not siblings or children.If I took 14 and 15(which also takes 16) and made them a child of 1, this would make them siblings, would this make them be included? Well, that depends. If the siblings are ABOVE the selected object, they won’t be printed. If they are below the selected object, then yes, it will work. Easy Peasy right?

Now, what if you want to actually go UP the hierarchy? For instance, what if you need to find the top root of a selected object? Well that’s easy, you can take this same code, you just have to make 2 little changes. Here’s the Code:

def GoUpHierarchy(obj):
if obj is None: return
#Actions can go here
print (obj.GetName())
#End Actions
if (obj.GetUp()):
if (obj.GetPred()):

So obviously if you’re going to go up your hierarchy you don’t want it named “GoDownHierarchy”. So You’ll see each reference to the function’s name has been changed to “GoUpHierarchy”. Besides that, you need to make some changes. Instead of GetDown() you need to use GetUp(), and instead of GetNext() you gotta use GetPred(). That’s it! Easy cheese!

4 Responses to “A Useful Python Snippet”
  1. [...] This post was mentioned on Twitter by Bret, Bret. Bret said: A Useful Python Snippet http://aq8ww.th8.us [...]

  2. Aleksey says:

    Hey man,

    great stuff, more of these =)
    Just learning the basics of python, but this information has been stored for future reference =)

    Did you do the animation mentor course in cinema or in maya?


  3. Hi Bret,

    Thanks for this. I would recommend tweaking the function so that you pass in a method as an argument. That way you don’t have to rewrite it every time. In addition, your ” if( obj.GetNext() ):” checks are redundant, because the function always checks to see if the object is None before looking for the next object. Here’s my revised version:

    def printName( obj ):
    print obj.GetName()

    def GoDownHierarchy(obj, doSomething):
    #If there isn’t an object, stop looping.
    if obj is None:
    return None

    #call the function
    doSomething( obj )

    #Try to get the next one down, there’s a chance it will be None
    GoDownHierarchy(obj.GetDown(), doSomething)

    #Try to get the next object, there’s a chance it will be None
    GoDownHierarchy(obj.GetNext(), doSomething)

    def main( obj ):
    GoDownHierarchy( obj, printName )

  4. That didn’t come through very well, you can view the formatted code here:

Leave a Reply