1
$\begingroup$

i have a skeleton and computed global(armature space) matrix of each bone for posing.

to get relative rotation and location(for keyframes) i am doing this:

for each frame
    for each bone
        poseBone.matrix = new_pose_bone_matrix
        bpy.context.scene.update()
        poseBone.keyframe_insert("location", frame_num)
        poseBone.keyframe_insert("rotation_quaternion", frame_num)

The problem: need to call scene.update() for each bone! (tryed to use it once per frame - getting weird result) Python's cProfile says that script spend 4 of 5 second in scene.update()(need to wrap in function)

Pre-making fcurves and inserting keyframes is more efficient since no scene.update() needed. Done in about 1 second. But i need relative loc/rot for this.

Tryed:

armature.convert_space(
       pose_bone = poseBone,
       matrix = new_pose_bone_matrix,
       from_space = 'WORLD',
       to_space = 'POSE')

But getting incorrect result(rotation is correct but relative location is incorrect) ;( Tryed 'LOCAL' ,'LOCAL_WITH_PARENT'... and other variations

Need this
  PoseBone.location
  PoseBone.rotation_quaternion
From
  PoseBone.matrix

poseBone.matrix datapath is useless for keyframes


EDIT

scene.update() for each pose bone calculation:

with mesh binded to armature:

407072 function calls in 18.281 seconds

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    1    0.817    0.817   18.281   18.281 io_import_scene_unreal_psa_psk.py:760(psaimport)
27166   15.393    0.001   15.393    0.001 io_import_scene_unreal_psa_psk.py:939(scene_update)
54332    1.823    0.000    1.823    0.000 {method 'keyframe_insert' of 'bpy_struct' objects}
80920    0.060    0.000    0.060    0.000 {built-in method Translation}

with NO mesh binded to armature(no armature type children too):

407072 function calls in 4.081 seconds

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    1    0.628    0.628    4.081    4.081 io_import_scene_unreal_psa_psk.py:760(psaimport)
54332    1.821    0.000    1.821    0.000 {method 'keyframe_insert' of 'bpy_struct' objects}
27166    1.383    0.000    1.383    0.000 io_import_scene_unreal_psa_psk.py:939(scene_update)
80920    0.060    0.000    0.060    0.000 {built-in method Translation}

with NO mesh binded to armature and
fcurve.keyframe_points.insert(frame,value)
instead of
poseBone.keyframe_insert("location")

353306 function calls in 2.965 seconds

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    1    1.354    1.354    2.965    2.965 io_import_scene_unreal_psa_psk.py:768(psaimport)
27166    1.361    0.000    1.361    0.000 io_import_scene_unreal_psa_psk.py:951(scene_update)
80920    0.058    0.000    0.058    0.000 {built-in method Translation}
54043    0.037    0.000    0.037    0.000 {method 'to_4x4' of 'Matrix' objects}
$\endgroup$

Your Answer

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

Browse other questions tagged or ask your own question.