1
$\begingroup$

I'm not an expert in 3D but I'm using Blender to change model's poses. I have a JSON object which describes how each bone will rotate, translate and scale:

{
  "name": "Soldier",
  "id": "TestId",
  "animMatrices": [
    {
      "name": "mixamorig:Spine",
      "position": [
        0.0,
        0.288420439,
        -0.027956875
      ],
      "rotation": [
        -0.0205147546,
        0.0105156591,
        0.0171466451,
        0.9995872
      ],
      "scale": [
        1.0,
        1.00000012,
        1.00000024
      ]
    },
    {
      "name": "mixamorig:Spine1",
      "position": [
        0.0,
        0.338067949,
        5.486072E-9
      ],
      "rotation": [
        0.0558569357,
        0.0207238179,
        0.0163610559,
        0.9980896
      ],
      "scale": [
        1.0,
        1.00000012,
        1.00000024
      ]
    }
  ]
} 

I tried to generate the 4x4 matrix and assign it to bone.matrix_basis as following:

pos = mathutils.Matrix.Translation((m['position'][0],m['position'][1], m['position'][2]))

rot1 = mathutils.Matrix.Rotation(math.radians(m['rotation'][0]), 4, 'X')
rot2 = mathutils.Matrix.Rotation(math.radians(m['rotation'][1]), 4, 'Y')
rot3 = mathutils.Matrix.Rotation(math.radians(m['rotation'][2]), 4, 'Z')
rot = rot1 * rot2 * rot3

scale1 = mathutils.Matrix.Scale(m['scale'][0], 4)
scale2 = mathutils.Matrix.Scale(m['scale'][1], 4)
scale3 = mathutils.Matrix.Scale(m['scale'][2], 4)
scale = scale1 * scale2 * scale3

bon_mat = pos * rot * scale
bone.matrix_basis = bon_mat

The model started to be having a very incorrect values, or I don't really know what happened.

I tried to to use bone.rotation_axis_angle but this did not effect anything, and by the way I always set:

bpy.ops.object.mode_set(mode="POSE")

and I got the JSON object from Unity.

$\endgroup$
1
$\begingroup$

You need the position for only loose bones. Instead of creating matrices assign the data where it belongs.

import bpy
if bpy.context.active_object.mode != 'POSE':
    bpy.ops.object.mode_set('POSE')

pbones = bpy.context.active_object.pose.bones

pbones['Spine'].location = m['position']
pbones['Spine'].scale = m['scale']

# I think your rotations are in quaternions, they are 4D
pbones['Spine'].rotation_quaternion = m['rotation']

# If not and you only use the first 3 values then:
#pbones['Spine'].rotation_mode = 'XYZ'
#pbones['Spine'].rotation_euler = m['rotation'][:3]

You can sure do this also by building the matrices, but check what your rotation data is in. You are building the rot_mat from Euler but I suspect your data being as quaternions.

$\endgroup$
  • $\begingroup$ I tried to do this but the model did not move at all, do I have to update the scene or do anything? $\endgroup$ – Qurashi Apr 29 '15 at 13:35
  • $\begingroup$ @AlQurashi there should be no need to update but it doesn't hurt. The code above works with this testing dictionary: m = {} m['position'] = [0.0, 0.288420439, -0.027956875] m['rotation'] = [-0.0205147546, 0.0105156591, 0.0171466451, 0.9995872] m['scale'] = [1.0, 1.00000012, 1.00000024] $\endgroup$ – Jaroslav Jerryno Novotny Apr 29 '15 at 14:04
  • $\begingroup$ Ok, now I get it and its changing, The problem is I get these values out from Unity, when I change these values, the models is damaged, what can I do? $\endgroup$ – Qurashi Apr 29 '15 at 15:45
  • $\begingroup$ @AlQurashi You should probably also validate the data getting out of unity. If they are in bone space or global space, left/right hand system, check the rotation values if they are correct and also check if or how were the bones transformed on import to blender. $\endgroup$ – Jaroslav Jerryno Novotny Apr 29 '15 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.