Roto Recursion

I needed to change the motion blur attribute for a long list of roto splines in Nuke. Note here that all of the roto nodes I’m searching for are by convention, contained in groups. If you’re operating on top level roto splines, remove the ‘if (b.Class() == ‘Group’):’ condition, and also don’t descend into groups with the ‘rotoNodeGrp.begin()’ command. (And of course, you won’t need the rotoNodeGrp.end() command either). But since motion blur only applies to roto shape types, I had to dive into each roto group to make sure the moBlurChanger function was attempting to act on shapes, but not layers. Yay, recursion!

import nuke.rotopaint as rp
 
 def moBlurChanger(elem):
     print "Elem name: " + elem.name
     
     for num, rotoTn in enumerate(elem):
         
         if isinstance(rotoTn, rp.Layer):
             print "Is layer: " 
             print rotoTn.name
             moBlurChanger(rotoTn)
         elif isinstance(rotoTn, rp.Shape):
             print "Is shape: "
             print rotoTn.name
             attrs = rotoTn.getAttributes()
             #turn motion blur on
             attrs.set('mbo', 1)
         
 
 print 'HERE WE GO!!!'
 nodez = nuke.allNodes() #This makes a list of all top level nodes
 for b in nodez:
     if (b.Class() == 'Group'):
         print b.name()
         rotoNodeGrp = nuke.toNode(b.name())
         print "Opening Group:"
         rotoNodeGrp.begin()
 
         grpList = rotoNodeGrp.nodes()
         
         for n in grpList:
             #print n.name()
             if n.Class() == 'Roto':
                 print "Roto Node Found!"
                 print n.name()
                 #pass to mBlur changer
                 #moBlurChanger(n)
                 cKnob = n['curves']
                 roto_root = cKnob.rootLayer
                 print "Roto Root: " + roto_root.name
                 for poss in roto_root:
                     print "Shape name: " + poss.name
 
                     #pass to mBlur changer
                     moBlurChanger(poss)
 
                     for num, rotoTn in enumerate(poss):
                         if isinstance(rotoTn, rp.Shape):
                             print "Real name: " + rotoTn.name()
                             attrs = rotoTn.getAttributes()
                             #turn motion blur on
                             attrs.set('mbo', 1)    
 
         print "Closing Group:"
         rotoNodeGrp.end()

Leave a Reply

Your email address will not be published. Required fields are marked *