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.
if obj is None: return
#Actions can go here
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:
if obj is None: return
#Actions can go here
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!