::object oriented rigging in Skeleton Builder::

July 17th, 2011 by hamish download the zooToolBox

Skeleton builder is a neat framework for writing rigging code, but its also a useful tool for writing runtime tools as well.  I figured I’d go over an example of this because its not terribly obvious until you actually get in there and write some code. And even then, its not terribly obvious until you read all the code written in baseRigPrimitive…

Writing a tool to do seamless Ik/Fk switching on an arbitrary rig is basically impossible.  After all, your particular limb setup might be a simple biped leg, a 5 jointed spider type leg, or a quadruped leg or any other crazy scenario.  Writing a single function to deal with all these possibilities, especially when you factor in the different ways these rigs could be implemented is basically impossible.

But writing the logic to do the switch for any of them in isolation is generally dead easy.  This is the sort of problem that object oriented programming was invented to solve.  In Skeleton Builder the way I solve these problems is like this.

In Skeleton Builder you write rig primitives. Each rig primitive is written to rig a particular sub-section of a skeleton and the code to do so is implemented in its own class.  So using the above examples I would have 3 classes: BipedLegRig, SpiderLegRig and QuadrupedLegRig.  Now on each one of these classes, lets define a switchToIk() method and a switchToFk() method.  So the switching logic for each rig type can be implemented independently on each of these classes.

Your code will look something like this:

import rigPrimitives

class SpiderLegRig(rigPrimitives.RigPart):
  #this class variable defines the alias' for the controls. controls are accessed using these alias'
  CONTROL_NAMES = 'legControl', 'fkThigh', 'fkKnee', 'fkAnkle'

  def _build( self, skeletonPart, **kw ):
    #the code to build the actual rig goes here
    ...

    #this is the list of nodes that will get connected to the alias' defined
    #in the CONTROL_NAMES class variable
    controls = ikControl, fk0, fk1, fk2
    namedNodes = ()

    return controls, namedNodes
  def switchToIk( self ):

    #this code grabs the actual control nodes based on their alias'
    legControl = self.getControl( 'legControl' )
    fkThigh = self.getControl( 'fkThigh' )
    fkKnee = self.getControl( 'fkKnee' )
    fkAnkle = self.getControl( 'fkAnkle' )

    #logic to switch from Fk to Ik goes below
    ...
  def switchToFk( self ):
    #logic to switch from Ik to Fk goes below
    ...

Then calling this switch method is as simple as doing something like this:

from maya import cmds

selectedControls = cmds.ls( sl=True )
if selectedControls:
  rigPart = rigPrimitives.RigPart.InitFromItem( selectedControls[0] )
  rigPart.switchToFk()

And voila. So now you can write a single UI that lets users switch between Ik and Fk for ANY part of the rig simply by running the above code.

I’ll write up another post with another example of a useful runtime tool that I’ve written that would be very difficult to do without a nice object oriented rigging framework to leverage.

Share

This post is public domain

This entry was posted on Sunday, July 17th, 2011 at 07:47 and is filed under main. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.