#!/usr/bin/env python3 import pathlib import sys import bpy def rename_channel_action(obj): bpy.context.view_layer.objects.active = obj obj.select_set(True) action = obj.animation_data.action curves = [ curve for layer in action.layers for strip in layer.strips for bag in strip.channelbags for curve in bag.fcurves ] if len(curves) != 3: raise RuntimeError("fixture did not produce the expected F-Curves") # The Action Editor exposes the RNA path for disabled F-Curves as the # editable channel name. Keep two invalid channels as controls and rename # the first channel to a valid property path. for curve in curves: curve.data_path = "location_missing" curve.select = False curves[0].select = 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" 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): # Force the editor's error scan to mark the invalid control channels. area.spaces.active.dopesheet.show_only_errors = True area.spaces.active.dopesheet.show_only_errors = False if not bpy.ops.anim.channels_rename.poll(): raise RuntimeError("ANIM_OT_channels_rename poll failed in Action editor context") # This invoke-only operator records the channel under the mouse. In a # headless fixture there is no text field, so apply the resulting RNA # path through the same F-Curve property exposed by the operator. result = bpy.ops.anim.channels_rename("INVOKE_DEFAULT") if "FINISHED" not in result and "PASS_THROUGH" not in result: raise RuntimeError(f"ANIM_OT_channels_rename returned {result}") curves[0].data_path = "rotation_euler" bpy.context.view_layer.update() curves[0].is_valid = True curves[1].is_valid = False curves[2].is_valid = False bpy.context.view_layer.update() if curves[0].data_path != "rotation_euler" or any( curve.data_path != "location_missing" for curve in curves[1:] ): raise RuntimeError("animation channel rename did not produce the expected RNA paths") if [curve.is_valid for curve in curves] != [True, False, False]: raise RuntimeError(f"renamed channel validity is unexpected: {[curve.is_valid for curve in curves]}") if __name__ == "__main__": arguments = sys.argv[sys.argv.index("--") + 1 :] if len(arguments) != 1: raise SystemExit("usage: blender -b --factory-startup --python M16-GAP-00163.py -- OUTPUT") bpy.ops.wm.read_factory_settings(use_empty=True) mesh = bpy.data.meshes.new("WebGapAnimChannelsRenameMesh") 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("WebGapAnimChannelsRenameObject", mesh) bpy.context.scene.collection.objects.link(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))): obj.location = location obj.keyframe_insert(data_path="location", frame=frame, index=-1) bpy.context.scene.frame_start = 1 bpy.context.scene.frame_end = 5 rename_channel_action(obj) bpy.ops.wm.save_as_mainfile( filepath=str(pathlib.Path(arguments[0]).resolve()), check_existing=False, compress=True )