109 lines
4.3 KiB
Python
109 lines
4.3 KiB
Python
#!/usr/bin/env python3
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import pathlib
|
|
import shutil
|
|
import sys
|
|
import tempfile
|
|
|
|
import bpy
|
|
|
|
|
|
ARMATURE_NAME = "WebGapArmatureAutosideArmature"
|
|
OBJECT_NAME = "WebGapArmatureAutosideObject"
|
|
LEFT_NAME = "WebGapArmatureAutosideLeft"
|
|
RIGHT_NAME = "WebGapArmatureAutosideRight"
|
|
LEFT_RENAMED = f"{LEFT_NAME}.L"
|
|
RIGHT_RENAMED = f"{RIGHT_NAME}.R"
|
|
|
|
|
|
def vector(value):
|
|
return [round(float(component), 6) for component in value]
|
|
|
|
|
|
def state_report():
|
|
armature = bpy.data.armatures.get(ARMATURE_NAME)
|
|
obj = bpy.data.objects.get(OBJECT_NAME)
|
|
if armature is None or obj is None or obj.type != "ARMATURE" or obj.data != armature:
|
|
raise RuntimeError("armature.autoside_names fixture is missing")
|
|
bones = [
|
|
{"name": bone.name, "head": vector(bone.head_local), "tail": vector(bone.tail_local)}
|
|
for bone in armature.bones
|
|
]
|
|
bones.sort(key=lambda value: value["name"])
|
|
return {"object": OBJECT_NAME, "armature": ARMATURE_NAME, "bones": bones}
|
|
|
|
|
|
def run_operator(before):
|
|
if {bone["name"] for bone in before["bones"]} == {LEFT_RENAMED, RIGHT_RENAMED}:
|
|
return True, False
|
|
if {bone["name"] for bone in before["bones"]} != {LEFT_NAME, RIGHT_NAME}:
|
|
raise RuntimeError(f"unexpected autoside_names input: {before}")
|
|
bpy.context.view_layer.objects.active = bpy.data.objects[OBJECT_NAME]
|
|
bpy.data.objects[OBJECT_NAME].select_set(True)
|
|
if bpy.context.object.mode != "EDIT":
|
|
bpy.ops.object.mode_set(mode="EDIT")
|
|
bpy.ops.armature.select_all(action="SELECT")
|
|
poll = bool(bpy.ops.armature.autoside_names.poll())
|
|
if not poll:
|
|
raise RuntimeError("ARMATURE_OT_autoside_names poll failed")
|
|
result = bpy.ops.armature.autoside_names(type="XAXIS")
|
|
if result != {"FINISHED"}:
|
|
raise RuntimeError(f"ARMATURE_OT_autoside_names returned {result}")
|
|
bpy.ops.object.mode_set(mode="OBJECT")
|
|
return poll, True
|
|
|
|
|
|
def main():
|
|
arguments = sys.argv[sys.argv.index("--") + 1 :]
|
|
if len(arguments) != 2:
|
|
raise SystemExit("usage: blender -b --factory-startup --python check-action-armature-autoside-names-desktop.py -- FIXTURE REPORT")
|
|
fixture, output = (pathlib.Path(value).resolve() for value in arguments)
|
|
bpy.ops.wm.open_mainfile(filepath=str(fixture), load_ui=False)
|
|
before = state_report()
|
|
poll, changed = run_operator(before)
|
|
after = state_report()
|
|
names = {bone["name"] for bone in after["bones"]}
|
|
if names != {LEFT_RENAMED, RIGHT_RENAMED}:
|
|
raise RuntimeError(f"autoside_names did not produce expected suffixes: {before} -> {after}")
|
|
|
|
descriptor, temporary = tempfile.mkstemp(prefix="m16-armature-autoside-names-reopen-", suffix=".blend")
|
|
os.close(descriptor)
|
|
temporary_path = pathlib.Path(temporary)
|
|
try:
|
|
bpy.ops.wm.save_as_mainfile(filepath=str(temporary_path), check_existing=False, compress=True)
|
|
bpy.ops.wm.open_mainfile(filepath=str(temporary_path), load_ui=False)
|
|
reopened = state_report()
|
|
if reopened != after:
|
|
raise RuntimeError(f"armature.autoside_names save/reopen drift: {after} != {reopened}")
|
|
shutil.copyfile(temporary_path, fixture)
|
|
report = {
|
|
"schemaVersion": 1,
|
|
"task": "M16-GAP-00217",
|
|
"operation": "ARMATURE_AUTOSIDE_NAMES_DESKTOP",
|
|
"fixture": str(fixture),
|
|
"fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(),
|
|
"before": before,
|
|
"after": reopened,
|
|
"alreadyNamed": not changed,
|
|
"poll": poll,
|
|
"operatorStatus": "FINISHED" if changed else "SKIPPED_ALREADY_APPLIED",
|
|
"mainMutation": "BONES_AUTOSIDE_NAMED" if changed else "ALREADY_AUTOSIDE_NAMED",
|
|
"saveReopen": "EXACT",
|
|
"blenderVersion": bpy.app.version_string,
|
|
}
|
|
output.parent.mkdir(parents=True, exist_ok=True)
|
|
output.write_text(json.dumps(report, indent=2, sort_keys=True) + "\n", encoding="utf-8")
|
|
print("armature-autoside-names-desktop-ok poll=true names=left-right saveReopen=exact")
|
|
finally:
|
|
temporary_path.unlink(missing_ok=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except Exception as error:
|
|
print(f"armature-autoside-names-desktop-failed: {error}")
|
|
raise SystemExit(1)
|