TransWikia.com

Animate armature from 3d joint locations

Blender Asked by aggardner13 on November 27, 2021

I have a simple human armature that I am trying to animate based on real-world 3d coordinates. These are coordinates of 21 distinct joints in (x, y, z) format. Based on the way that I generated my data, I cannot guarantee that the bones are globally the same length, but I assume that it will be close. To combat this, I produced an armature that has a parented joint structure, but where the bones are disconnected. It looks like this:
Simple armature with disconnect bones, but properly parented

I want to set the bone head and tail position of every bone in my armature, and then animate it from the data that I have using the python API.

I have a super simple script that I am working from to read in my data and assign the proper objects. I was attempting to assign positions to head and tail, but I see that the pose_bones are read-only for those objects. Thus, I created a few extra bones and just set .location to see if I could get my bones to move. My script translates all of my bones, but every frame appears identical (not to mention extremely disconnected) – even though my data is not constant in every frame.

Here is that script:

with open('frames.json') as f:
    frame_data = json.load(f)

# frame_data is a dictionary of dictionaries that looks like this: 
# {'frame_0' : {'joint_name': (x, y, z), ... }, ..., 'frame_n': {} }

rig = bpy.data.objects['metarig']

for frame_num, joints in frame_data.items():
    bpy.context.scene.frame_set(int(frame_num))
    for joint_name, joint_positions in joints.items():
        if "hand" not in joint_name:
            rig.pose.bones[joint_name].location = joint_positions
    rig.keyframe_insert(data_path="location", index=-1)

I assume that I am accessing the wrong object to update positions, or that I have to switch back and forth between edit mode and pose mode in every frame, but I am not sure where to turn.

When I run my code, I get some craziness that looks like this:
A nonsensical frame produced by my script

I am suspecting that the giant gaps are because I am not updating the head and tail positions. How does one do that explicitly? And is there any reason in particular why every frame is identical?

I am completely new to blender, and this is the first time that I have ever done a project with the software, so I apologize if my question is a little elementary.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP