56 lines
2.3 KiB
Python
56 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
import pathlib
|
|
import sys
|
|
import bpy
|
|
|
|
|
|
def bake_action_in_editor(obj):
|
|
bpy.context.view_layer.objects.active = obj
|
|
obj.select_set(True)
|
|
window = bpy.context.window
|
|
screen = window.screen
|
|
area = next((candidate for candidate in screen.areas if candidate.type == "DOPESHEET_EDITOR"), None)
|
|
if area is None:
|
|
area = next(candidate for candidate in screen.areas if candidate.type == "VIEW_3D")
|
|
area.type = "DOPESHEET_EDITOR"
|
|
area.spaces.active.ui_mode = "ACTION"
|
|
region = next(candidate for candidate in area.regions if candidate.type == "WINDOW")
|
|
with bpy.context.temp_override(window=window, screen=screen, area=area, region=region):
|
|
if not bpy.ops.action.bake_keys.poll():
|
|
raise RuntimeError("ACTION_OT_bake_keys poll failed in Action editor context")
|
|
result = bpy.ops.action.bake_keys()
|
|
if "FINISHED" not in result:
|
|
raise RuntimeError(f"ACTION_OT_bake_keys returned {result}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
arguments = sys.argv[sys.argv.index("--") + 1:]
|
|
if len(arguments) != 1:
|
|
raise SystemExit("usage: blender -b --factory-startup --python M16-GAP-00113.py -- OUTPUT")
|
|
bpy.ops.wm.read_factory_settings(use_empty=True)
|
|
mesh = bpy.data.meshes.new("WebGapBakeKeysMesh")
|
|
mesh.from_pydata(
|
|
[(-1.0, -1.0, 0.0), (1.0, -1.0, 0.0), (1.0, 1.0, 0.0), (-1.0, 1.0, 0.0)],
|
|
[],
|
|
[(0, 1, 2, 3)],
|
|
)
|
|
obj = bpy.data.objects.new("WebGapBakeKeysObject", mesh)
|
|
bpy.context.scene.collection.objects.link(obj)
|
|
obj.location = (0.0, 0.0, 0.0)
|
|
obj.keyframe_insert(data_path="location", frame=1, index=-1)
|
|
obj.location = (2.0, 3.0, 4.0)
|
|
obj.keyframe_insert(data_path="location", frame=5, index=-1)
|
|
for layer in obj.animation_data.action.layers:
|
|
for strip in layer.strips:
|
|
for bag in strip.channelbags:
|
|
for fcurve in bag.fcurves:
|
|
for keyframe in fcurve.keyframe_points:
|
|
keyframe.select_control_point = True
|
|
bpy.context.scene.frame_start = 1
|
|
bpy.context.scene.frame_end = 5
|
|
bake_action_in_editor(obj)
|
|
bpy.context.scene.frame_set(1)
|
|
bpy.ops.wm.save_as_mainfile(
|
|
filepath=str(pathlib.Path(arguments[0]).resolve()), check_existing=False, compress=True
|
|
)
|