"""Prepare a LinuxCNC machine GLB for Blender based editing. Usage: blender -b --python scripts/blender_machine_template.py -- \ --input web/react/public/machine-cases/physical/hexapod-genhexkins.glb \ --output /tmp/hexapod-machine.blend """ import argparse import bpy from mathutils import Vector def arg_value(name, default=None): argv = bpy.app.handlers if False else __import__("sys").argv if "--" not in argv: return default args = argv[argv.index("--") + 1:] for index, value in enumerate(args): if value == name and index + 1 < len(args): return args[index + 1] return default input_path = arg_value("--input") output_path = arg_value("--output", "/tmp/linuxcnc-machine.blend") if not input_path: raise SystemExit("--input GLB is required") bpy.ops.wm.read_factory_settings(use_empty=True) bpy.ops.import_scene.gltf(filepath=input_path) scene = bpy.context.scene scene.unit_settings.system = "METRIC" scene.unit_settings.length_unit = "MILLIMETERS" scene["linuxcnc.workflow"] = "linuxcncWebWASM machine manager" scene["linuxcnc.source_glb"] = input_path collection = bpy.data.collections.new("LinuxCNC_Reference_Axes") scene.collection.children.link(collection) for name, direction, color in [ ("AXIS_X", (1, 0, 0), (1, 0, 0, 1)), ("AXIS_Y", (0, 1, 0), (0, 1, 0, 1)), ("AXIS_Z", (0, 0, 1), (0, 0, 1, 1)), ]: empty = bpy.data.objects.new(name, None) empty.empty_display_type = "ARROWS" empty.empty_display_size = 0.15 empty.color = color empty["linuxcnc.axis_direction"] = direction collection.objects.link(empty) origin = bpy.data.objects.new("MACHINE_ORIGIN", None) origin.empty_display_type = "PLAIN_AXES" origin.empty_display_size = 0.25 origin["linuxcnc.role"] = "base" collection.objects.link(origin) for obj in scene.objects: if obj.name.startswith("physical_") or obj.name == "machine_root": obj["linuxcnc.editable_structure"] = True obj["linuxcnc.units"] = "m in GLB / mm in editor" bpy.ops.wm.save_as_mainfile(filepath=output_path) print(f"BLENDER_MACHINE_TEMPLATE_OK {output_path}")