Files
workinf_Blender_Wasm/tools/web/check-action-armature-bone-primitive-add-desktop.py
mes123456 9f43244982
Some checks failed
M6 deployable RC / quick (push) Has been cancelled
M6 deployable RC / chromium (push) Has been cancelled
M6 deployable RC / release (push) Has been cancelled
Continue Blender Web parity task handoff
2026-08-23 05:47:21 -04:00

122 lines
4.7 KiB
Python

#!/usr/bin/env python3
import hashlib
import json
import os
import pathlib
import shutil
import sys
import tempfile
import bpy
ARMATURE_NAME = "WebGapArmaturePrimitiveArmature"
OBJECT_NAME = "WebGapArmaturePrimitiveObject"
BONE_NAME = "WebGapArmaturePrimitiveBone"
CURSOR = [1.5, -2.0, 0.75]
LENGTH = 2.5
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.bone_primitive_add fixture is missing")
bones = [
{
"name": bone.name,
"head": vector(bone.head_local),
"tail": vector(bone.tail_local),
"useDeform": bool(bone.use_deform),
}
for bone in armature.bones
]
bones.sort(key=lambda value: value["name"])
return {"object": OBJECT_NAME, "armature": ARMATURE_NAME, "cursor": vector(bpy.context.scene.cursor.location), "bones": bones}
def run_operator(before):
names = {bone["name"] for bone in before["bones"]}
if names == {BONE_NAME}:
already_added = True
elif names:
raise RuntimeError(f"unexpected bone_primitive_add input: {before}")
else:
already_added = False
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")
poll = bool(bpy.ops.armature.bone_primitive_add.poll())
if not poll:
raise RuntimeError("ARMATURE_OT_bone_primitive_add poll failed")
if not already_added:
result = bpy.ops.armature.bone_primitive_add(
name=BONE_NAME, length=LENGTH, align="UP", space="OBJECT", use_deform=False
)
if result != {"FINISHED"}:
raise RuntimeError(f"ARMATURE_OT_bone_primitive_add returned {result}")
bpy.ops.object.mode_set(mode="OBJECT")
return poll, not already_added
def main():
arguments = sys.argv[sys.argv.index("--") + 1 :]
if len(arguments) != 2:
raise SystemExit("usage: blender -b --factory-startup --python check-action-armature-bone-primitive-add-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()
if len(after["bones"]) != 1 or after["bones"][0]["name"] != BONE_NAME:
raise RuntimeError(f"bone_primitive_add did not create the expected bone: {before} -> {after}")
bone = after["bones"][0]
expected_head = CURSOR
expected_tail = [CURSOR[0], CURSOR[1], CURSOR[2] + LENGTH]
if bone["head"] != expected_head or bone["tail"] != expected_tail or bone["useDeform"]:
raise RuntimeError(f"bone_primitive_add produced unexpected data: {after}")
descriptor, temporary = tempfile.mkstemp(prefix="m16-armature-bone-primitive-add-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.bone_primitive_add save/reopen drift: {after} != {reopened}")
shutil.copyfile(temporary_path, fixture)
report = {
"schemaVersion": 1,
"task": "M16-GAP-00218",
"operation": "ARMATURE_BONE_PRIMITIVE_ADD_DESKTOP",
"fixture": str(fixture),
"fixtureSha256": hashlib.sha256(fixture.read_bytes()).hexdigest(),
"before": before,
"after": reopened,
"alreadyAdded": not changed,
"poll": poll,
"operatorStatus": "FINISHED" if changed else "SKIPPED_ALREADY_APPLIED",
"mainMutation": "BONE_CREATED_AT_CURSOR" if changed else "ALREADY_BONE_CREATED_AT_CURSOR",
"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-bone-primitive-add-desktop-ok poll=true bone=cursor saveReopen=exact")
finally:
temporary_path.unlink(missing_ok=True)
if __name__ == "__main__":
try:
main()
except Exception as error:
print(f"armature-bone-primitive-add-desktop-failed: {error}")
raise SystemExit(1)