Skip to content

Instantly share code, notes, and snippets.

@shrinktofit
Created May 3, 2023 07:23
Show Gist options
  • Save shrinktofit/f6bfeeafbb559c9e7f569e0db3bcb8cc to your computer and use it in GitHub Desktop.
Save shrinktofit/f6bfeeafbb559c9e7f569e0db3bcb8cc to your computer and use it in GitHub Desktop.
Blender 增强版的应用缩放
import bpy
def print(data):
for window in bpy.context.window_manager.windows:
screen = window.screen
for area in screen.areas:
if area.type == 'CONSOLE':
override = {'window': window, 'screen': screen, 'area': area}
bpy.ops.console.scrollback_append(override, text=str(data), type="OUTPUT")
def process_action(action, root_bone_names, scale):
print('Processing action %s' % action.name)
# for fcurve in action.fcurves:
# print('%s %s' % (fcurve.data_path, fcurve.array_index))
for root_bone_name in root_bone_names:
bone_data_prefix = 'pose.bones[\"%s\"].' % root_bone_name
position_fcurves = [action.fcurves.find('%slocation' % bone_data_prefix, index=i) for i in range(3)]
rotation_quaternion_fcurves = [action.fcurves.find('%srotation_quaternion' % bone_data_prefix, index=i) for i in range(4)]
scale_fcurves = [action.fcurves.find('%sscale' % bone_data_prefix, index=i) for i in range(3)]
# print((position_fcurves, rotation_quaternion_fcurves, scale_fcurves))
for component_index, position_fcurve in enumerate(position_fcurves):
if not position_fcurve:
continue
print('Processing %s %s[%s]' % (root_bone_name, 'location', component_index))
for keyframe_point in position_fcurve.keyframe_points:
co = keyframe_point.co
co.y *= scale[component_index]
def process(obj):
if obj.type != 'ARMATURE':
return
armature = obj.data;
print('Processing armature %s' % obj.name)
root_bone_names = [bone.name for bone in armature.bones if not bone.parent]
print('Root bones: %s' % root_bone_names)
obj_scale = obj.scale
print('About to apply scale %s' % obj_scale)
animation_data = obj.animation_data
if animation_data:
if animation_data.action:
print('%s uses action %s(as current action)' % (obj.name, animation_data.action.name))
process_action(animation_data.action, root_bone_names, obj_scale)
# for t in animation_data.nla_tracks:
# for s in t.strips:
# print('%s uses action %s(in NLA track strip %s)' % (obj.name, s.action.name, s.name))
bpy.ops.object.transform_apply(location=False, rotation=False, scale=True, properties=False)
for selected_object in bpy.context.selected_objects:
process(selected_object)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment