Brekel-Kinect-bvh to Rigify Script

I wrote a script to connect a rigify-armature to the bvh from the Brekel-MoCap.
It’s working kinda ok and I want to share my work.
Any enhancements are welcome.:o

Here is the code:

import bpy
from math import radians
###############################################################
### Run this script on the T-Position Frame of the bvh-file ###
### or the Empties will be all over the place               ###
###############################################################
print("###############################################################################")
print("############################                         ##########################")
print("############################     Start of script     ##########################")
print("############################                         ##########################")
print("###############################################################################")

def    parent_set_bone(object, armature, bone):
        object.parent = armature
        object.parent_type = 'BONE'
        object.parent_bone = bone

obj = bpy.data.objects
arm1 = obj['rig']
arm2 = obj['KinectArmature']
kinect2rigify = "head,head,0,0,0;neck,neck,0,0,0;ribs,chest,0,0,0;torso,hip,0,0,0;shoulder.L,lCollar,0,0,-0;shoulder.R,rCollar,0,0,0;upper_arm.L,lShldr,0,0,0;upper_arm.R,rShldr,-0,0,0;forearm.L,lForeArm,0,0,0;forearm.R,rForeArm,0,0,0;hand.L,lHand,0,0,90;hand.R,rHand,0,0,0;thigh.L,lThigh,0,0,0;thigh.R,rThigh,0,0,0;shin.L,lShin,0,0,0;shin.R,rShin,0,0,0;foot.L,lFoot,0,0,0;foot.R,rFoot,0,0,0"
kinectarray = [x.split(',') for x in kinect2rigify.split(';')]
y=0

bpy.ops.object.mode_set(mode='OBJECT')

for z in kinect2rigify.split(';'):
    bonename = kinectarray[y][1]
    emptyname = "Empty." + bonename
    objbone = arm1.pose.bones[kinectarray[y][0]].constraints
    rotx=radians(int(kinectarray[y][2]))
    roty=radians(int(kinectarray[y][3]))
    rotz=radians(int(kinectarray[y][4]))
    
    #################################################################
    ### Creating Empties and position them at the Kinect-Armature ###
    #################################################################

    bpy.ops.object.add(type='EMPTY', view_align=False, enter_editmode=False,location=(0,0,0), rotation=(rotx,roty,rotz), layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))
    bpy.context.scene.objects.active.name = emptyname
    bpy.data.objects[emptyname].location =     arm2.pose.bones[bonename].tail * arm2.matrix_world
    parent_set_bone(obj[emptyname], arm2, bonename)
    obj[emptyname].location = 0,0,0
    
    #################################################
    ### Create Constraints on the Rigify-Armature ###
    #################################################

    objbone.new('COPY_ROTATION')
    objbone['Copy Rotation'].target = obj[emptyname]
    objbone['Copy Rotation'].target_space = 'WORLD' ### Empty-Space
    objbone['Copy Rotation'].owner_space = 'POSE' ### rigifybone-Space
#    objbone['Copy Rotation'].use_offset = True
    y=y+1

P.S.: There is some code in it, that is not really needed anymore, like the angles for the Empties, I’ll get rid of those later.
I’m leaving them in to make changes to the Empties easier.

And attached is the .blend file for the ones interested.:wink:

Hope it is helpfull

Yours Spero…

Attachments

Kinect-MoCap.blend (1.78 MB)

:oOk, now I found a better way to do it.
I’m using the MoCapTools for it now and it’s much less trouble.
But the other solution was good code practice for me.:wink:
The new code is this:

import bpy

bpy.ops.object.select_all(action='DESELECT')
obj = bpy.data.objects
perf_arm = bpy.data.armatures['KinectArmature']
perf_arm = bpy.data.armatures['KinectArmature']
bpy.ops.object.mode_set(mode='OBJECT')
kinect2rigify = "head,head;neck,neck;ribs,chest;hips,hip;shoulder.L,lCollar;shoulder.R,rCollar;upper_arm.L,lShldr;upper_arm.R,rShldr;forearm.L,lForeArm;forearm.R,rForeArm;hand.L,lHand;hand.R,rHand;thigh.L,lThigh;thigh.R,rThigh;shin.L,lShin;shin.R,rShin;foot.L,lFoot;foot.R,rFoot"
kinectarray = [x.split(',') for x in kinect2rigify.split(';')]
y=0

selector = bpy.ops.object.select_pattern
selector(pattern="KinectArmature", case_sensitive=False, extend=False)
selector(pattern="rig", case_sensitive=False, extend=True)
bpy.context.scene.objects.active = obj['rig']
bpy.ops.mocap.scale_fix()

for x in kinect2rigify.split(';'):
    print("y: "+str(y)+" "+str(kinectarray[y][0])+"+"+str(kinectarray[y][1]))
    perf_arm.bones[kinectarray[y][1]].map=kinectarray[y][0]
    y=y+1
bpy.ops.mocap.retarget()

I recorded a bvh with the free Brekel MoCap Software.
Then adjusted it a little in BVHacker and saved it as KinectArmature.bvh.
In the blend file I have the Standard Rigify-Armature called ‘rig’
Import the bvh and then just run the script.

Below is the new .blend file, it already contains the bvh.

Hope it’s usefull for someone :evilgrin:

Yours Spero…

Attachments

Kinect-MoCap.blend (1.62 MB)

I’m not really familiar with bvh except that it’s some type of motion-capture data and there are libraries of motions available, most notably the Carnegie-Mellow stuff. So, this script will map one of these motions onto a Rigify rigged character?

I’m just a little confused because you say you recorded the bvh file yourself. With a Kinect? I just want to make sure I understand what you’ve done.

I’m using a Kinect from the X-Box with the OpenNI-Drivers and the free Software for MoCap by Jasper Brekelmans to create a BVH-file:
http://www.brekel.com/?page_id=160

Then I have a look at it in BVHacker, to easily make a loop out of it:
http://www.bvhacker.com/
also a free software (by David Wooldridge)

Then with the script I can asign this with one run of the script to the Rigify-Armature.
So I can use it with an exsisting Figur and don’t have to go through all the hussle of asigning everything over and over again;)

The script is only for the bvhs created with this software, since I only asign each bone to the corresponding one in the Rigify-Armature.
But you can actually just adjust the corresponding bone names in the Kinect2Rigify variable and it should work with other BVHs as well.
My first script was created, cause all the bones in the BVH have different axis then the ones in the Rigify-Armature.
But then I found out about the MoCap-Tools in Blender and the 2nd script uses these to asign the bones now.
I think the addon does a good job in getting the axis correct.
With the Kinect for X-Box, I don’t know if actual Kinects are still supported, since Microsoft wants to sell their Kinect for Windows for twice the price.
But I still have one of the old ones:evilgrin:
You would have to do some research on that.
But I think this is at least the cheapest easy to use MoCap out there.
I’m still just experimenting since I have alot of work to do at the moment.
But I thought if someone else is interessted in it, I should share it.

Yours Spero…

very interesting & very nice idea. thanks.

Thanks for the extra info, Speroangelus. A very interesting tidbit.

Last I noticed, Costco was still selling the Kinect for xBox as an add-on, so there should still be lots out there.

As for Microsoft selling the Windows version for more, aren’t they also beefing up the resolution as well?

Hi, I was just wondering if there is a big difference between the rigify-script now, from what you used when you made this script? I really would like to use this, but i keep getting an error message that tells me that there has been an error, and that i should look in the console for now… But there is nothing in the console that i can see… Is there any chance you could help me out a little? :slight_smile: pretty please?

kind regards
-c-