2
$\begingroup$

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: enter image description here

and if I load rotations it should be like thisenter image description here

Unfortunately, when I load the rotations, they change the model completelyenter image description here

The model should be in something like 'A' pose, like this:enter image description here

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.head = mathutils.Vector([float(coords[0]), float(coords[1]), float(coords[2])])
        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

$\endgroup$
1
  • 2
    $\begingroup$ 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. $\endgroup$
    – batFINGER
    Commented Apr 27, 2020 at 9:28

1 Answer 1

3
$\begingroup$

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)
    bone.head   = mathutils.Vector([float(coords[0]), float(coords[1]), float(coords[2])])
    # To establish a length of bone
    bone.tail   = mathutils.Vector([float(coords[0]), float(coords[1])+0.001, float(coords[2])])
    # To apply the rotations
    bone.matrix = pos @ quat

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 likeenter image description here As you can see, bones are well rotated looking at the axes.

$\endgroup$
3
  • 1
    $\begingroup$ Good one. Could we see a pic of result. Does this produce a "posed" rest pose? $\endgroup$
    – batFINGER
    Commented Apr 28, 2020 at 16:14
  • $\begingroup$ Yes, as you can see, the model keeps the original pos with rotated bones. cdn.discordapp.com/attachments/361666379834720259/… $\endgroup$ Commented Apr 28, 2020 at 17:51
  • $\begingroup$ Sorry, I'm not familiar with the mechanics of the place. I hope that's right. $\endgroup$ Commented Apr 28, 2020 at 23:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .