1
$\begingroup$

i have a model format and i try to write a plugin for it in blender (import/export script in python).

In this model format i have bone orientations given in world space.

Now i tried to create a test case with 4 bones, bone1 is parent of bone2, bone2 is parent of bone3, and bone4 is parent of bone3.

My approach is to overwrite the matrix field of a pose bone, for example this

bpy.data.objects['Armature'].pose.bones['Bone1'].matrix = <some matrix>

I figure this works, but only for the first bone. If i run this script twice, it works for Bone1 and Bone2. If i run the script 4 times, everything is like i want.

Can someone explain what is wrong here? I can't seem to get my head around thos transformations. I tried inverted stuff too, am i even on the right path by trying to overwrite the matrix field of a pose bone?

Some help is greatly apprciated, script is below. It requires to have a scene with an armature and 4 bones all parented to each other, with exact bonenames Bone1, Bone2, Bone3, Bone4, and the parent relations i described above, which is Bone1 is parent of Bone2, Bone2 is parent of Bone3, etc.

Note: i do not use all stuff i calculate in this script, for example the inverted matrices are not used. In the end i just overwrite the matrix field with the matrix i create woth "mat_bone1_r" etc.

import bpy

import mathutils
import math

#bone 1
mat_bone1_b = bpy.data.objects['Armature'].pose.bones['Bone1'].matrix
mat_bone1_bi = mat_bone1_b.inverted()

# here i create a matrix simulating a world space orientation of a bone
b1_x = mathutils.Vector((1.0, 0.0, 0.0))
b1_y = mathutils.Vector((0.0, 0.0, -1.0))
b1_z = mathutils.Vector((0.0, 1.0, 0.0))
mat_bone1_r = mathutils.Matrix()
mat_bone1_r[0][0], mat_bone1_r[1][0], mat_bone1_r[2][0] = b1_x
mat_bone1_r[0][1], mat_bone1_r[1][1], mat_bone1_r[2][1] = b1_y
mat_bone1_r[0][2], mat_bone1_r[1][2], mat_bone1_r[2][2] = b1_z
mat_bone1_r = mat_bone1_r.to_4x4()

mat_bone1_n = mat_bone1_r

bpy.data.objects['Armature'].pose.bones['Bone1'].matrix = mat_bone1_n

# bone 2
mat_bone2_b = bpy.data.objects['Armature'].pose.bones['Bone2'].matrix
mat_bone2_bi = mat_bone2_b.inverted()

# here i create a matrix simulating a world space orientation of a bone
b2_x = mathutils.Vector((1.0, 0.0, 0.0))
b2_y = mathutils.Vector((0.0, 0.0, -1.0))
b2_z = mathutils.Vector((0.0, 1.0, 0.0))
mat_bone2_r = mathutils.Matrix()
mat_bone2_r[0][0], mat_bone2_r[1][0], mat_bone2_r[2][0] = b2_x
mat_bone2_r[0][1], mat_bone2_r[1][1], mat_bone2_r[2][1] = b2_y
mat_bone2_r[0][2], mat_bone2_r[1][2], mat_bone2_r[2][2] = b2_z
mat_bone2_r = mat_bone2_r.to_4x4()

mat_bone2_n = mat_bone2_r

bpy.data.objects['Armature'].pose.bones['Bone2'].matrix = mat_bone2_n

# bone 3
mat_bone3_b = bpy.data.objects['Armature'].pose.bones['Bone3'].matrix
mat_bone3_bi = mat_bone3_b.inverted()

# here i create a matrix simulating a world space orientation of a bone
b3_x = mathutils.Vector((1.0, 0.0, 0.0))
b3_y = mathutils.Vector((0.0, 0.0, -1.0))
b3_z = mathutils.Vector((0.0, 1.0, 0.0))
mat_bone3_r = mathutils.Matrix()
mat_bone3_r[0][0], mat_bone3_r[1][0], mat_bone3_r[2][0] = b3_x
mat_bone3_r[0][1], mat_bone3_r[1][1], mat_bone3_r[2][1] = b3_y
mat_bone3_r[0][2], mat_bone3_r[1][2], mat_bone3_r[2][2] = b3_z
mat_bone3_r = mat_bone3_r.to_4x4()

mat_bone3_n = mat_bone3_r

bpy.data.objects['Armature'].pose.bones['Bone3'].matrix = mat_bone3_n 
$\endgroup$

0

You must log in to answer this question.

Browse other questions tagged .