Skip to content

Instantly share code, notes, and snippets.

@aftamat4ik
Last active October 24, 2021 08:30
Show Gist options
  • Save aftamat4ik/b2925e3eb42a4a213a3919d483f19929 to your computer and use it in GitHub Desktop.
Save aftamat4ik/b2925e3eb42a4a213a3919d483f19929 to your computer and use it in GitHub Desktop.
Blender quaternion to euler conversion functions
import bpy
def quaternion_euler_all_bones(mode, start, end, on_obj):
for f in range(frame_start, frame_end +1): # Go from start to End
# for each rig bone in current frame
for bone in obj.pose.bones:
#don't forget to restore bone rotation mode for next keyframe
if mode == "EtoQ":
bone.rotation_mode = "XYZ"
elif mode == "QtoE":
bone.rotation_mode = "QUATERNION"
bpy.context.scene.frame_set(f) # switch to frame position
for fc in obj.animation_data.action.fcurves: #obj.animation_data.action.fcurves - current action curves
# https://devtalk.blender.org/t/fastest-way-to-get-fcurves-from-bone/11382/2
if fc.data_path.split('"')[1] == bone.name: #check if current curve represents selected bone
for keyframe in fc.keyframe_points: #each keyframe. keyframe.co - struct out of 2 fields [x,y] that represent keyframe position in curves. Where X is time
if(keyframe.co.x == f): #check if bone keyframe time mets with the currently processing keyframe. If so that means that bone has keyframe in this position and because of taht we now can convert it's rotation
if mode == "EtoQ":
bone.rotation_mode = "QUATERNION"
bone.keyframe_insert(data_path="rotation_quaternion")
elif mode == "QtoE":
bone.rotation_mode = "XYZ"
bone.keyframe_insert(data_path="rotation_euler")
else:
continue
def quaternion_euler_selected_bone(mode, start, end, on_obj):
#bpy.context.object.data.bones.active - selected bone
#bpy.context.active_pose_bone - selected Pose bone.... well did not expected that there is difference between them...
bone = bpy.context.active_pose_bone
for f in range(frame_start, frame_end +1): # Go from start to End
#don't forget to restore bone rotation mode for next keyframe
if mode == "EtoQ":
bone.rotation_mode = "XYZ"
elif mode == "QtoE":
bone.rotation_mode = "QUATERNION"
bpy.context.scene.frame_set(f) # switch to frame position
for fc in obj.animation_data.action.fcurves: #obj.animation_data.action.fcurves - current action curves
# https://devtalk.blender.org/t/fastest-way-to-get-fcurves-from-bone/11382/2
if fc.data_path.split('"')[1] == bone.name: #check if current curve represents selected bone
for keyframe in fc.keyframe_points: #each keyframe. keyframe.co - struct out of 2 fields [x,y] that represent keyframe position in curves. Where X is time
if(keyframe.co.x == f): #check if bone keyframe time mets with the currently processing keyframe. If so that means that bone has keyframe in this position and because of taht we now can convert it's rotation
if mode == "EtoQ":
bone.rotation_mode = "QUATERNION"
bone.keyframe_insert(data_path="rotation_quaternion")
elif mode == "QtoE":
bone.rotation_mode = "XYZ"
bone.keyframe_insert(data_path="rotation_euler")
else:
continue
scn = bpy.context.scene
obj = bpy.context.active_object
frame_start = 1
frame_end = 120
print("-------")
#mode selection
#mode = "QtoE"
mode = "EtoQ"
quaternion_euler_selected_bone(mode, frame_start, frame_end, obj)
#quaternion_euler_all_bones(mode, frame_start, frame_end, obj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment