32 lines
3.1 KiB
Python
32 lines
3.1 KiB
Python
#!/usr/bin/env python3
|
|
import hashlib,json,os,pathlib,shutil,sys,tempfile,bpy
|
|
ARMATURE_NAME="WebGapArmatureSplitArmature"; OBJECT_NAME="WebGapArmatureSplitObject"; ROOT_BONE="WebGapArmatureSplitRoot"; CHILD_BONE="WebGapArmatureSplitChild"; OTHER_BONE="WebGapArmatureSplitOther"
|
|
def ensure():
|
|
o=bpy.data.objects.get(OBJECT_NAME); a=bpy.data.armatures.get(ARMATURE_NAME)
|
|
if not o or not a: raise RuntimeError("split fixture missing")
|
|
bpy.context.view_layer.objects.active=o;o.select_set(True)
|
|
if o.mode!="EDIT":bpy.ops.object.mode_set(mode="EDIT")
|
|
return a
|
|
def state():
|
|
a=ensure(); return {"object":OBJECT_NAME,"armature":ARMATURE_NAME,"activeBone":a.edit_bones.active.name if a.edit_bones.active else None,"bones":[{"name":b.name,"selected":bool(b.select),"parent":b.parent.name if b.parent else None,"connected":bool(b.use_connect),"head":[round(float(x),6) for x in b.head],"tail":[round(float(x),6) for x in b.tail]} for b in a.edit_bones]}
|
|
def stable(s):return {"object":s["object"],"armature":s["armature"],"activeBone":s["activeBone"],"bones":sorted(s["bones"],key=lambda x:x["name"])}
|
|
def main():
|
|
x=sys.argv[sys.argv.index("--")+1:]
|
|
if len(x)!=2:raise SystemExit("usage")
|
|
f,out=(pathlib.Path(v).resolve() for v in x);bpy.ops.wm.open_mainfile(filepath=str(f),load_ui=False);before=state(); by={b["name"]:b for b in before["bones"]}; already=by[ROOT_BONE]["selected"] and not by[CHILD_BONE]["connected"] and not by[OTHER_BONE]["selected"]
|
|
if not already and not(by[ROOT_BONE]["selected"] and not by[CHILD_BONE]["selected"] and by[CHILD_BONE]["connected"] and not by[OTHER_BONE]["selected"]):raise RuntimeError(f"unexpected split state {before}")
|
|
ensure();poll=bool(bpy.ops.armature.split.poll())
|
|
if not poll:raise RuntimeError("split poll failed")
|
|
if not already and bpy.ops.armature.split()!={"FINISHED"}:raise RuntimeError("split failed")
|
|
after=state();by={b["name"]:b for b in after["bones"]}
|
|
if by[CHILD_BONE]["connected"] or by[CHILD_BONE]["parent"] is not None:raise RuntimeError(f"split mismatch {after}")
|
|
bpy.ops.object.mode_set(mode="OBJECT");fd,t=tempfile.mkstemp(prefix="m16-split-reopen-",suffix=".blend");os.close(fd);tp=pathlib.Path(t)
|
|
try:
|
|
bpy.ops.wm.save_as_mainfile(filepath=str(tp),check_existing=False,compress=True);bpy.ops.wm.open_mainfile(filepath=str(tp),load_ui=False);re=state()
|
|
if stable(re)!=stable(after):raise RuntimeError("split save/reopen drift")
|
|
shutil.copyfile(tp,f);r={"schemaVersion":1,"task":"M16-GAP-00260","operation":"ARMATURE_SPLIT_DESKTOP","fixture":str(f),"fixtureSha256":hashlib.sha256(f.read_bytes()).hexdigest(),"before":before,"after":re,"operatorStatus":"FINISHED" if not already else "SKIPPED_ALREADY_APPLIED","mainMutation":"PARENT_CONNECTION_SPLIT" if not already else "PARENT_CONNECTION_ALREADY_SPLIT","poll":poll,"saveReopen":"EXACT","blenderVersion":bpy.app.version_string};out.parent.mkdir(parents=True,exist_ok=True);out.write_text(json.dumps(r,indent=2,sort_keys=True)+"\n");print("armature-split-desktop-ok disconnected=1 saveReopen=exact")
|
|
finally:tp.unlink(missing_ok=True)
|
|
if __name__=="__main__":
|
|
try:main()
|
|
except Exception as e:print(f"armature-split-desktop-failed: {e}");raise SystemExit(1)
|