Skip to content

New Script: Draw Parenting Trees for Armature Bones in the Node Editor

February 28, 2013

I recently started delving deep into rig generating scripts.
Working on the “Baldy Heights” feature animation movie in a small studio with a modest budget means trying to do everything as efficiently and quickly as possible, and facilitating all repetitive tasks with automation.

One such task is creating custom rigs. The Rigify add-on is a wonderful aid for this task, and in addition to the human meta-rig which is very useful “out of the box”, it provides a robust and extensible platform for creating new automatic rigs.

Rigify enables you to define new rig-types that can generate complex controls, constrains, drivers, widgets and whatever else you need, and to assemble these rig-types into intricate meta-rigs like a really cool Lego construction.

I’ll be writing more about this in the near future, but in the meantime, I wanted to share a little script I wrote today which facilitates the rig-type writing process.

One of the things you need when you’re writing rig-types is a solid understanding of the bone parenting structure in the armature you’re trying to generate. So to help with understanding the tree and testing the generation process, there’s now a useful little script which generates a node-tree in the compositor that corresponds to the armature’s bone parenting structure.

To illustrate the kind of structure it creates, and what sort of overview it provides, here’s a rather extreme example: the node tree for the complete generated human meta-rig with all bells and whistles.

Rigify parenting tree

For a slightly saner version, here’s the human meta-rig’s parenting structure before generating all the widgets and controls:

rigify_bone_tree_01

So how does this script work:

For a complete description see the wiki page on github.

The script uses the compositor (node editor) and its math convertor nodes to draw the parenting tree, since it’s probably the easiest way to represent the parenting tree structure with nodes.

It’s best to avoid using it on production files, as it will delete your existing compositing nodes in order to make room for the parenting tree node structure. Another issue is that it only works with Blender 2.66 and does not support older versions.

How to use:

  1. Open a text editor window.
  2. Create a new textfile, and copy the content of this script to the empty textfile.
    Or: Open the saved .py file in the text editor.
  3. Open the node editor window, and press the compositing nodes button.
  4. Make sure that “Use Nodes” is checked and active.
  5. In the 3D view, select your armature.
  6. Back in the text editor window, press “Run Script”.
  7. View the node editor to see the generated tree.

To download, go to the script’s page on GitHub.

Here’s the source for quick reference:


bl_info = {
"name"        : "Armature parenting tree generator",
"author"      : "Tamir Lousky",
"version"     : (1, 0, 0),
"blender"     : (2, 66, 0),
"category"    : "Rigging",
"wiki_url"    : "https://github.com/Tlousky/production_scripts/wiki/_new?wiki[name]=draw_parenting_tree.py",
"download_url": "https://github.com/Tlousky/production_scripts/blob/master/draw_parenting_tree.py",
"description" : "Creates a node tree representing the armature's bone parenting structure."
}

import bpy

def create_node( bone, x, y, row ):
""" recursive function that creates a math node for each bone in the armature
and draws connections between the bones (math nodes) and their parents """

node = tree.nodes.new('MATH') # create a new math node

# set up node location, label and name, and minimize (hide) the node
node.location = x,y
node.label    = bone.name
node.name     = bone.name
node.hide     = True

# if this isn't the (parentless) root bone, create a link to its parent
if bone.parent:
parent_name = bone.parent.name
parent_node = tree.nodes[parent_name]
links.new( parent_node.outputs[0], node.inputs[0] )

# iterate over all the current's bone's children and draw their nodes
row += 1 # the row represents each "generation" visually via x axis distance
for child in bone.children:
x = 200 * row
y = 0
create_node( child, x, y, row )

def set_node_height( linksnode, i, yp ):
""" recurse all nodes and set their height
according to the parenting structure """

n = len(node.outputs[0].links) # count the number of children
y = yp + (i + n) * -30         # calculate y value according to: parent's y value,
# no. of children and the position of current bone in children
node.location.y = y            # set the node's actual y location value

j = 0
for link in node.outputs[0].links: # iterate current node's links
child = link.to_node           # reference output node
j += 1
set_node_height( child, j, y )

if not bpy.context.scene.use_nodes:
print( 'can only draw tree if you are using nodes in the rendering compositor' )
else:
rig   = bpy.context.object  # reference the selected armature
bones = rig.data.bones      # reference bone data

# find root bone
root = ''
for bone in bones:
if not bone.parent:     # the root bone has no parent
root = bone.name

# create references to node tree and node links
tree  = bpy.context.scene.node_tree
links = tree.links

# clear existing nodes
for n in tree.nodes:
tree.nodes.remove(n)

# create root node's location
(row, x, y) = (0, 0, 0)

# Draw primary tree (starting from root bone)
create_node( bones[root], x, 0, row )

# iterate over all nodes and adjust their y value
i = 0
for node in tree.nodes:
set_node_height( node, i, 0 )
i += 1
Advertisements
2 Comments
  1. that’s grate!
    You should add it as an add-on.
    in case you are planning to develop it further here are some ideas.
    could be nice to visualize also different kind of connections like constraints and drivers, maybe with different noodle colors
    could be also good if we could also influence the hierarchy within the node editor, and not just visualize.
    and the best thing would be if we could use math nodes to influence the rig system as well, kind of like drivers (or maybe just use drivers and a separate ‘math’ node)
    these stuff could really jump the rigging system in blender a head

    did you publish it in other blender channels? maybe some other scripters would be interested to help developing it more.

    have fun in pitchipoy and dash to noam and gilad 🙂

  2. Thanks Tal 🙂
    I’m considering adding constraints and drivers into the mix (constraints first),
    not sure if I can alter the color of connections though I can choose node colors (which don’t show if the node is minimized, but might still be handy).

    However, editing the rig through the node editor will be much trickier (maybe will try it at some point), and since I’m using the compositor itself (and not some other dedicated window), it might not be the best idea to do this, or to make this an official add-on.I did publish it on the blender facebook group and on blender artists, so I hope others who might find it useful heard about it.

    It would be great if we could have a hierarchy and editing node window such as Maya’s hypergraph, and I would have tried expanding my little script into something of that kind if I thought it would be possible to do via python only. But that’s probably not the case since we really need a totally new node editor window which won’t be related to compositing, shading or texturing nodes (which will probably require C programming).

    Nice to (virtually) meet another (ex)pitchipoy dude! 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: