143 lines
5.6 KiB
Python
143 lines
5.6 KiB
Python
#!/usr/bin/env python3
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import pathlib
|
|
import shutil
|
|
import sys
|
|
import tempfile
|
|
|
|
import bpy
|
|
|
|
|
|
ARMATURE_NAME = "WebGapArmatureSymmetrizeArmature"
|
|
OBJECT_NAME = "WebGapArmatureSymmetrizeObject"
|
|
SOURCE_BONE = "WebGapArmatureSymmetrizeSource.L"
|
|
MIRRORED_BONE = "WebGapArmatureSymmetrizeSource.R"
|
|
OTHER_BONE = "WebGapArmatureSymmetrizeOther"
|
|
|
|
|
|
def vector(value):
|
|
return [round(float(component), 6) for component in value]
|
|
|
|
|
|
def ensure_edit_mode():
|
|
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.symmetrize fixture is missing")
|
|
bpy.context.view_layer.objects.active = obj
|
|
obj.select_set(True)
|
|
if obj.mode != "EDIT":
|
|
bpy.ops.object.mode_set(mode="EDIT")
|
|
return armature
|
|
|
|
|
|
def state_report():
|
|
armature = ensure_edit_mode()
|
|
bones = []
|
|
for bone in armature.edit_bones:
|
|
bones.append({
|
|
"name": bone.name,
|
|
"selected": bool(bone.select),
|
|
"hidden": bool(bone.hide),
|
|
"parent": bone.parent.name if bone.parent else None,
|
|
"connected": bool(bone.use_connect),
|
|
"head": vector(bone.head),
|
|
"tail": vector(bone.tail),
|
|
})
|
|
return {
|
|
"object": OBJECT_NAME,
|
|
"armature": ARMATURE_NAME,
|
|
"activeBone": armature.edit_bones.active.name if armature.edit_bones.active else None,
|
|
"bones": bones,
|
|
}
|
|
|
|
|
|
def stable(value):
|
|
return {
|
|
"object": value["object"],
|
|
"armature": value["armature"],
|
|
"activeBone": value["activeBone"],
|
|
"bones": sorted(value["bones"], key=lambda bone: bone["name"]),
|
|
}
|
|
|
|
|
|
def main():
|
|
arguments = sys.argv[sys.argv.index("--") + 1 :]
|
|
if len(arguments) != 2:
|
|
raise SystemExit("usage: blender -b --factory-startup --python checker -- 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()
|
|
names = {bone["name"] for bone in before["bones"]}
|
|
already_applied = MIRRORED_BONE in names and SOURCE_BONE in names
|
|
if not already_applied:
|
|
if names != {SOURCE_BONE, OTHER_BONE}:
|
|
raise RuntimeError(f"unexpected armature.symmetrize fixture: {before}")
|
|
if not next(bone for bone in before["bones"] if bone["name"] == SOURCE_BONE)["selected"]:
|
|
raise RuntimeError(f"symmetrize source is not selected: {before}")
|
|
ensure_edit_mode()
|
|
poll = bool(bpy.ops.armature.symmetrize.poll())
|
|
if not poll:
|
|
raise RuntimeError("ARMATURE_OT_symmetrize poll failed")
|
|
if not already_applied:
|
|
result = bpy.ops.armature.symmetrize(direction="NEGATIVE_X")
|
|
if result != {"FINISHED"}:
|
|
raise RuntimeError(f"ARMATURE_OT_symmetrize returned {result}")
|
|
after = state_report()
|
|
by_name = {bone["name"]: bone for bone in after["bones"]}
|
|
if MIRRORED_BONE not in by_name or SOURCE_BONE not in by_name or OTHER_BONE not in by_name:
|
|
raise RuntimeError(f"symmetrize did not create the mirrored bone: {after}")
|
|
source = by_name[SOURCE_BONE]
|
|
mirrored = by_name[MIRRORED_BONE]
|
|
other = by_name[OTHER_BONE]
|
|
if source["head"] != [1.0, 0.0, 0.0] or source["tail"] != [1.0, 1.0, 0.0]:
|
|
raise RuntimeError(f"symmetrize changed source geometry: {after}")
|
|
if mirrored["head"] != [-1.0, 0.0, 0.0] or mirrored["tail"] != [-1.0, 1.0, 0.0]:
|
|
raise RuntimeError(f"symmetrize mirror geometry mismatch: {after}")
|
|
if other["selected"]:
|
|
raise RuntimeError(f"symmetrize selected unrelated bone: {after}")
|
|
bpy.ops.object.mode_set(mode="OBJECT")
|
|
descriptor, temporary = tempfile.mkstemp(prefix="m16-armature-symmetrize-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 stable(reopened) != stable(after):
|
|
raise RuntimeError(f"armature.symmetrize save/reopen drift: {after} != {reopened}")
|
|
shutil.copyfile(temporary_path, fixture)
|
|
report = {
|
|
"schemaVersion": 1,
|
|
"task": "M16-GAP-00263",
|
|
"operation": "ARMATURE_SYMMETRIZE_DESKTOP",
|
|
"fixture": str(fixture),
|
|
"fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(),
|
|
"before": before,
|
|
"after": reopened,
|
|
"sourceBone": SOURCE_BONE,
|
|
"mirroredBone": MIRRORED_BONE,
|
|
"otherBone": OTHER_BONE,
|
|
"direction": "NEGATIVE_X",
|
|
"operatorStatus": "FINISHED" if not already_applied else "SKIPPED_ALREADY_APPLIED",
|
|
"mainMutation": "MIRRORED_SELECTED_BONE" if not already_applied else "MIRRORED_BONE_ALREADY_PRESENT",
|
|
"poll": poll,
|
|
"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(f"armature-symmetrize-desktop-ok source={SOURCE_BONE} mirrored={MIRRORED_BONE} direction=negative_x saveReopen=exact")
|
|
finally:
|
|
temporary_path.unlink(missing_ok=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except Exception as error:
|
|
print(f"armature-symmetrize-desktop-failed: {error}")
|
|
raise SystemExit(1)
|