# Set initial position and rotation of a bone (python)

I'm making a script for import models from a game. This game has inital position and rotation for each bone. If I load the model without rotating bones, it looks like this:

and if I load rotations it should be like this

Unfortunately, when I load the rotations, they change the model completely

The model should be in something like 'A' pose, like this:

This is the script:

    bpy.ops.object.mode_set(mode='EDIT')
for h in range(boneCount):
name      = f[h * 3 + 1][:-1]
parent    = int(f[h * 3 + 2])
coords    = f[h * 3 + 3][:-1].split(" ")

bone      = skl.data.edit_bones.new(name)
bone.tail = mathutils.Vector([float(coords[0]), float(coords[1])+0.01, float(coords[2])])

bpy.ops.object.mode_set(mode='POSE')
for h in range(boneCount):
name      = f[h * 3 + 1][:-1]
parent    = int(f[h * 3 + 2])
coords    = f[h * 3 + 3][:-1].split(" ")

rot       = mathutils.Quaternion([float(coords[6]), float(coords[3]), float(coords[4]), float(coords[5])])
bone      = skl.data.edit_bones.get(name)
bpy.context.object.pose.bones[h].rotation_quaternion = rot

bpy.ops.object.mode_set(mode='EDIT')
for h in range(boneCount):
name      = f[h * 3 + 1][:-1]
parent    = int(f[h * 3 + 2])
coords    = f[h * 3 + 3][:-1].split(" ")
bone      = skl.data.edit_bones.get(name)
if parent != -1:
bone.parent = skl.data.edit_bones[parent]


Rotations are quaternions. Any ideas on how I can implement it? Thanks

• Hard to guess without knowing what space the data uses. In rest position each pose bone matrix is identity. It looks like you have a reasonable rest pose. Going EDIT - POSE - EDIT doesn't look right. Recommend make the rest pose without the rotation step. Add the pose by manipulating pose bone matrices using the rotations. Check out the convert space method as explained here. blender.stackexchange.com/a/172973/15543 Make a matrix from your quaternion. Convert it to pose space of the bone. Apr 27 '20 at 9:28

Thanks to batFINGER I've solved the issue.

bpy.ops.object.mode_set(mode='EDIT')
for h in range(boneCount):
name        = f[h * 3 + 1][:-1]
parent      = int(f[h * 3 + 2])
coords      = f[h * 3 + 3][:-1].split(" ")
# Converting quaternions values to matrix 4x4
quat        = mathutils.Quaternion([float(coords[6]), float(coords[3]), float(coords[4]), float(coords[5])]).to_matrix().to_4x4()
# Converting location values to matrix 4x4
pos         = mathutils.Matrix.Translation([float(coords[0]), float(coords[1]), float(coords[2])])
bone        = skl.data.edit_bones.new(name)

With this script I'm able to load the original bones rotations in matrix. After bone declaration, must set the bone.head in order to place the bone. bone.tail is useful for boen size, else the bone will be larger. Now, in order to set the desired rotations without altering the object structure, you must set bone.matrix. Is better to use mathutils for creating correct matrix. This is how it looks like As you can see, bones are well rotated looking at the axes.