Halloween Costume ideas 2015

PYTHON / Skin the Bone

Human Meta Rig to Skin Modifier

The other day, I received an email asking a question below:

SAL:
“Although Blender now generates the armature from skin, is there still a way to generate rigged mesh from armature? I have great many animated armatures I need to re-model, this would help so much.

Aha, cool question! I actually had the same exact questions a while back ago, when I was touching a little bit on Skin Modifier.

I believe SAL's real question is on converting pre-existing Armature into Skin. The answer is:
"Yes, certainly there is way to generate rigged mesh from armature" and maybe easiest by mean of Python script. Back then, I would feel a little bit stuck because I could not script and probably have to wait until someone else write script that does the job.

Today, even though I am still a beginner CG programmer and a long way to go, but I can "hack thing a bit" and it is always fun to dig the BPY (Blender Python).

I bet that someone else out there already created similar script that is probably better than this script I am writing. However, it is still necessary exercise to go through.

Let see how far can we go in converting Skin from Bone.

BLENDER ARMATURE & RIGIFY

(Before I jump into Blender Armature object via Python, I have to talk a little bit about Blender Armature and the Rigify Add-On....)

Ok, so I am a highly curious person about Blender Armature object. I will really investigate it like a newbie.


Blender Armature is like an Empty that it does not render, but it has a lot of special features in it. Just like Blender Curve and Blender Text object, they are special.

For every Armature object, it consists of Bones hierarchy (aka Bone Chain). Armature has 2 modes to work with: EDIT MODE and POSE MODE.

To switch to Armature Edit Mode, you select the Armature object and hit TAB.
To switch to Armature Pose Mode, you select the Armature object and hit SHIFT+TAB.



Animator animates a rigged character in Pose Mode. Armature is a single object that actually contains a lot of other objects and information, but able to maintain a Single Actions animation data.

(That is pretty much some basic Armature information that we need to know. Next, I talk a bit about Rigify...)

Apart from  my interest with that Bone Object called Armature in Blender, I have also big interested with Art of Rigging inside Blender back then when I first use Blender 2.5x, and now even more so.

I definitely need to study Nathan Vegdahl DVD "Humane Rig" again. I never actually finished the whole rigging process. 

There are too many questions in my mind at the time. Not that I get less questions today, I actually have even more questions than before. But some important questions are answered along the way.

Last Friday, when I was at University computer room, I investigate Blender Rigify Add-On a little bit and use it from user's point of view from scratch to prepare the armature, run the Rigify script and skin "Make Human" mesh and also a random bi-ped creature created using Skin Modifier.

I like Rigify system, it is quite solid, a lot of magic happens in the background. Such as how Rigify uses Layers to separate the actual Skin Bones, Mechanics and the actual Controls.

One day I will spend some time to deconstruct the system that is implemented in Rigify.

For now, I will just use the meta-rig armatures for a test.

BLENDER ARMATURE VIA PYTHON

Like other special Blender Objects, the best way to investigate an object is to start from Properties.

With Armature, we need to go to Edit Mode and investigate each Bone in the hierarchy to truly understand what is happening.



Bone in Blender cannot exist by itself. It always needs 2 "joints" that is connected to each other to form a bone. Each Bone in Blender always has Head and Tail. The relationship between one bone with another bone via Parenting is important. As well as to watch for the "Local Rotation Axis" for each bone that is determined by the Roll value. 

Now that we have a bit clearer picture about Armature object and Bone object (element of Armature), we can then start looking and digging it using Python Console.


  • I rename my Armatures as 'MyArmature' for now.
  • To check the Bones element inside Armature, we simply go down in the object level
  • Using the Python Console, we can see there are 3 bones, named "Bone", "Bone.001", "Bone.003".
I found it interesting how Python looks up Dictionary data.

Listing Methods/Functions that the Bone object has. Notice that we need to specifically point into certain Bone whether by name or index in order to dig deeper and get information.
Blender API help on Bone:

NOTE: By no means I understand Blender Armature and Bone as whole, but here I am trying to walk you through the process on getting information using Python and use them. Often time we don't need to understand everything (sometimes impossible), unless you need to go into more complexity.

GET BONE INFORMATION

Ok, now what information do we really need to accomplish our problem in turning Armature into Skin AKA Create Skin from Bone?

Let's think.... we know that Skin Modifier only needs Polygon Mesh Edge component. What is the bare minimum of an Edge? 2 Points that are connected together.

Since we know Bone always has Head & Tail, while Edge always has 2 points that are connected, we can sort of solve our problem of "converting bone into skin".

So, we need:
1. Locations (2 XYZ positions) of Head and Tail for every bone (local position is fine)
2. Based on those Locations info, we can create Points and connect them into Edge
3. The rest, we will figure out later.


CREATE EDGE USING PYTHON SCRIPT

I am looking at these to understand how to create an Edge.

Slight modification to the script and then I got this below script that will create a mesh object which contains 2 points and an Edge. Exactly what we need at bare minimum.

import bpy 

# HELPER FUNCTION THAT CREATE EDGE BASED ON SUPPLIED POSITIONS
def createEdge(coord1 = (-1.0, 1.0, 0.0), coord2 = (-1.0, -1.0, 0.0)):
    
    Verts = [coord1, coord2]
    Edges = [[0,1]]
    
    profile_mesh = bpy.data.meshes.new("Edge_Profile_Data")
    profile_mesh.from_pydata(Verts, Edges, [])
    profile_mesh.update()
    
    profile_object = bpy.data.objects.new("Edge_Profile", profile_mesh)
    profile_object.data = profile_mesh
    
    scene = bpy.context.scene
    scene.objects.link(profile_object)
    profile_object.select = True

EDGES FROM BONES FOR SKIN MODIFIER

With the helper script above, we can easily turn every Bone into Edge by looping every Bone in the Armature and then run the script.

NOTE: In the script below, I am actually referencing the Object Name, instead of Armature name. Maybe you want to modify the script so that it works on Armature.

import bpy

# ACTUAL FUNCTION THAT TURN BONES INTO EDGES
def boneToEdges(armature_name='metarig'):
    
    myRig = bpy.data.objects[armature_name]
    
    # which armature to work on
    #myRig = bpy.data.objects['Armature']
    #myRig = bpy.data.objects['metarig']
    
    # this actually return STRING NAME of bone
    boneNames = myRig.data.bones.keys()
    
    # the actual data to each 
    myBones = myRig.data.bones
    
    for i, bone in enumerate(myBones):
        #print(bone.name, bone.vector)
        # every bone has HEAD and TAIL
    
        #loc = bone.vector
        head_loc = bone.head_local
        tail_loc = bone.tail_local
        
        createEdge(coord1 = head_loc, coord2 = tail_loc)
        
boneToEdges("MyArmature") # Change the name to your Armature Object Name.

Running the script above and I get multiple Edges objects like below:

We are not actually finish because my scripts is a bit of half done. It only creates Edges from every Bones. We need to Join (CTRL+J) all the edges into a single mesh and the Remove Doubles (merge overlapping points). Once we have a single mesh, we can then apply Skin Modifier.


You can quick select the Edges created using the script using the QUICK SELECT script below and then CTRL+J to join them into a single mesh, and then you can Remove Doubles..

import bpy

# Get all objects named "NAME*" and put it in a list
mesh = bpy.data.objects
sel = [item for item in mesh if "Edge_Profile" in item.name]

for item in sel:
    item.select = True

USING THE SCRIPT ON RIGIFY (HUMAN-METARIG)

Let's try this on Rigify Bones. If we use the script on the Rig object (with controls), we get something like below:


If we use the Human Meta-Rig object, we get slightly cleaner Edges that potentially can be used for Skin Modifier.


With slight clean up and connecting and Remove Doubles, we get a Skin Modifier ready edges, that we can easily turn into a mesh:


BVH Motion Capture Data to Skin Modifier

If we import BVH data into Blender, we know we get Armature that is already animated, right? So we can use the script above to "Skin the Bone". 

Below is from JPOP Group "Perfume" Motion Capture Dance data which was made available for download and really popular last year:

The Skin is dancing in no time.

Once we create Edges that is "Skin Modifier ready", we can easily modify the Skin and quickly Auto-Weight it to the original Skeleton.

I actually quite like the fact that Blender has separate Edit Mode and Pose Mode. We can match the Bone in Edit Mode (the default pose), regardless the animation in Pose Mode.

VIDEO DEMO "Bone to Skin"




SOME LIST OF GOOD VIDEOS ON WORKING WITH BVH
http://www.youtube.com/watch?v=AzYotTZVhcM
https://vimeo.com/12340788
http://www.youtube.com/watch?v=oe1ySIpWZgo


DOWNLOAD FREE BVH
https://sites.google.com/a/cgspeed.com/cgspeed/motion-capture/cmu-bvh-conversion


HORSE BVH?
I am trying to get Quadruped BVH data, that would be interesting. Anyways, for Biped BVH data, there are quite a lot on the Internet. I wish I have my own Kinect to do mocap data, that will be fun.

OTHER LINKS

http://www.blender.org/forum/viewtopic.php?t=26496&sid=bbfac66a1deced62a45d9b4bbd0bda8f
http://blenderartists.org/forum/showthread.php?181392-Armature-Bones-and-Poses-with-Python

Post a Comment

MKRdezign

Contact Form

Name

Email *

Message *

Powered by Blogger.
Javascript DisablePlease Enable Javascript To See All Widget