#!/usr/bin/env python3 import pathlib import sys import bpy def clean_empty_action(empty_obj, keep_obj): bpy.context.view_layer.objects.active = empty_obj keep_obj.select_set(True) empty_obj.select_set(True) window = bpy.context.window screen = window.screen area = next( (candidate for candidate in screen.areas if candidate.type == "DOPESHEET_EDITOR" and candidate.height >= 200), 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" area.spaces.active.dopesheet.show_only_selected = False area.spaces.active.dopesheet.show_only_slot_of_active_object = False 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.anim.channels_clean_empty.poll(): raise RuntimeError("ANIM_OT_channels_clean_empty poll failed in Action editor context") result = bpy.ops.anim.channels_clean_empty() if "FINISHED" not in result: raise RuntimeError(f"ANIM_OT_channels_clean_empty 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-00154.py -- OUTPUT") bpy.ops.wm.read_factory_settings(use_empty=True) empty_mesh = bpy.data.meshes.new("WebGapAnimChannelsCleanEmptyMesh") empty_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)], ) empty_obj = bpy.data.objects.new("WebGapAnimChannelsCleanEmptyObject", empty_mesh) bpy.context.scene.collection.objects.link(empty_obj) empty_obj.animation_data_create() empty_obj.animation_data.action = bpy.data.actions.new("WebGapAnimChannelsCleanEmptyAction") empty_obj.select_set(True) keep_mesh = bpy.data.meshes.new("WebGapAnimChannelsCleanKeepMesh") keep_mesh.from_pydata( [(-0.5, -0.5, 0.0), (0.5, -0.5, 0.0), (0.5, 0.5, 0.0), (-0.5, 0.5, 0.0)], [], [(0, 1, 2, 3)], ) keep_obj = bpy.data.objects.new("WebGapAnimChannelsCleanKeepObject", keep_mesh) bpy.context.scene.collection.objects.link(keep_obj) for frame, location in ((1, (0.0, 0.0, 0.0)), (3, (2.0, 3.0, 4.0)), (5, (4.0, 6.0, 8.0))): keep_obj.location = location keep_obj.keyframe_insert(data_path="location", frame=frame, index=-1) bpy.context.scene.frame_start = 1 bpy.context.scene.frame_end = 5 clean_empty_action(empty_obj, keep_obj) if empty_obj.animation_data is not None: raise RuntimeError("empty animation data was not removed") bpy.ops.wm.save_as_mainfile( filepath=str(pathlib.Path(arguments[0]).resolve()), check_existing=False, compress=True )