#!/usr/bin/env python3 import pathlib import sys import bpy def new_action(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") 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.new.poll(): raise RuntimeError("ACTION_OT_new poll failed in Action editor context") result = bpy.ops.action.new() if "FINISHED" not in result: raise RuntimeError(f"ACTION_OT_new 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-00129.py -- OUTPUT") bpy.ops.wm.read_factory_settings(use_empty=True) mesh = bpy.data.meshes.new("WebGapActionNewMesh") 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("WebGapActionNewObject", mesh) bpy.context.scene.collection.objects.link(obj) obj.location = (1.0, 2.0, 3.0) obj.keyframe_insert(data_path="location", frame=1, index=-1) old_name = obj.animation_data.action.name old_actions = {action.name for action in bpy.data.actions} new_action(obj) created = [action for action in bpy.data.actions if action.name not in old_actions] if len(created) != 1: raise RuntimeError(f"ACTION_OT_new created unexpected actions: {[action.name for action in created]}") obj.animation_data.action = created[0] if obj.animation_data.action.name == old_name: raise RuntimeError("ACTION_OT_new did not assign a new action") bpy.ops.wm.save_as_mainfile( filepath=str(pathlib.Path(arguments[0]).resolve()), check_existing=False, compress=True )