2
$\begingroup$

I'm trying to get all the meshes in a scene to their identity transform to export them so I can use their matrix_world to position them properly for animations/instancing/etc. I set them all back to their identity transform by clearing location/rotation/scale with bpy.ops.object.(location|rotation|scale)_clear(). However in the case of animated objects with keyframes their position doesn't get cleared, so they're exported incorrectly.

What's odd is that just doing ALT-(G|R|S) to clear them manually does clear the position properly. Is there something extra I need to do to properly clear the positions from Python?

My script is essentially just:

# Select all the objects we want to export and save
# their matrix_world so we can restore it after exporting

# Clear positions for exporting everything at its identity transform
bpy.ops.object.location_clear()
bpy.ops.object.rotation_clear()
bpy.ops.object.scale_clear()

# Export meshes to OBJ
bpy.ops.export_scene.obj(...)

# Restore transformations by setting matrix_world back to what we saved earlier
$\endgroup$
2
$\begingroup$

Thanks to zeffii on the #blenderpython IRC channel on freenode I've got this sorted out. The issue is that the keyframes take precedence over the plain matrix_world so clearing it has no effect from the script. The solution is to mute the animation before clearing the transform, then un-mute it to restore the animation to how it was previously.

So the final script ends up basically like this:

# Select all the objects we want to export and save
# their matrix_world so we can restore it after exporting
# If the object is animated also mute keyframe animation so
# it doesn't block *_clear
for curve in obj.animation_data.action.fcurves:
    curve.mute = True

# Clear positions for exporting everything at its identity transform
bpy.ops.object.location_clear()
bpy.ops.object.rotation_clear()
bpy.ops.object.scale_clear()

# Export meshes to OBJ
bpy.ops.export_scene.obj(...)

# Restore transformations by setting matrix_world back to what we saved earlier
# If the object is animated also unmute keyframe animation to restore it
for curve in obj.animation_data.action.fcurves:
    curve.mute = False
$\endgroup$

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.