import json
import os
import sys
import bpy
def _parse_cli_args():
if "--" not in sys.argv:
raise SystemExit("export_blend_to_glb.py requires `-- <output.glb>` arguments")
extra = sys.argv[sys.argv.index("--") + 1 :]
if not extra:
raise SystemExit("Missing output .glb path after `--`")
output_path = os.path.abspath(extra[0])
apply_modifiers = "--no-apply-modifiers" not in extra
export_tangents = "--no-tangents" not in extra
selected_only = "--selected-only" in extra
return output_path, apply_modifiers, export_tangents, selected_only
def _extract_world_metadata(scene):
world = scene.world
if world is None:
return None
meta = {
"name": world.name,
"use_nodes": bool(world.use_nodes),
"color": [float(world.color[0]), float(world.color[1]), float(world.color[2])],
"strength": 1.0,
"hdri_filepath": None,
}
if world.use_nodes and world.node_tree is not None:
for node in world.node_tree.nodes:
if node.type == "BACKGROUND":
color_input = node.inputs.get("Color")
strength_input = node.inputs.get("Strength")
if color_input is not None and hasattr(color_input, "default_value"):
val = color_input.default_value
meta["color"] = [float(val[0]), float(val[1]), float(val[2])]
if strength_input is not None:
meta["strength"] = float(strength_input.default_value)
elif node.type == "TEX_ENVIRONMENT" and node.image is not None:
filepath = bpy.path.abspath(node.image.filepath) if node.image.filepath else node.image.name
meta["hdri_filepath"] = filepath
return meta
def _extract_area_light_metadata(obj):
if obj.type != "LIGHT" or obj.data is None or obj.data.type != "AREA":
return None
light = obj.data
width = float(getattr(light, "size", 1.0))
shape = str(getattr(light, "shape", "SQUARE"))
if shape in ("RECTANGLE", "ELLIPSE"):
height = float(getattr(light, "size_y", width))
else:
height = width
return {
"shape": shape,
"width": width,
"height": height,
"energy": float(light.energy),
"color": [float(light.color[0]), float(light.color[1]), float(light.color[2])],
}
def _annotate_euv_extras(scene):
world_meta = _extract_world_metadata(scene)
if world_meta is not None:
scene["euv_world"] = json.dumps(world_meta)
collections_summary = []
for coll in bpy.data.collections:
collections_summary.append(
{
"name": coll.name,
"hide_viewport": bool(coll.hide_viewport),
"hide_render": bool(coll.hide_render),
}
)
scene["euv_collections"] = json.dumps(collections_summary)
for obj in scene.objects:
collection_names = [coll.name for coll in obj.users_collection]
existing_euv = obj.get("euv")
euv_payload = dict(existing_euv.to_dict()) if hasattr(existing_euv, "to_dict") else {}
if isinstance(existing_euv, dict):
euv_payload.update(existing_euv)
euv_payload["collections"] = collection_names
if "collider" not in euv_payload and "euv_collider" in obj:
euv_payload["collider"] = str(obj["euv_collider"])
if "buoyancy" not in euv_payload and "euv_buoyancy" in obj:
euv_payload["buoyancy"] = float(obj["euv_buoyancy"])
if "trigger" not in euv_payload and "euv_trigger" in obj:
euv_payload["trigger"] = bool(obj["euv_trigger"])
if "cast_shadows" not in euv_payload:
euv_payload["cast_shadows"] = bool(getattr(obj, "visible_shadow", True))
area_meta = _extract_area_light_metadata(obj)
if area_meta is not None:
euv_payload["area_light"] = area_meta
obj["euv"] = euv_payload
def _build_gltf_export_kwargs(output_path, apply_modifiers, export_tangents, selected_only):
rna_props = set(bpy.ops.export_scene.gltf.get_rna_type().properties.keys())
desired = {
"filepath": output_path,
"export_format": "GLB",
"use_selection": selected_only,
"export_yup": True,
"export_apply": apply_modifiers,
"export_texcoords": True,
"export_normals": True,
"export_tangents": export_tangents,
"export_materials": "EXPORT",
"export_cameras": True,
"export_lights": True,
"export_extras": True,
"export_skins": True,
"export_morph": True,
"export_morph_normal": True,
"export_morph_tangent": export_tangents,
"export_animations": True,
"export_image_format": "AUTO",
}
return {key: value for key, value in desired.items() if key in rna_props}
def main():
output_path, apply_modifiers, export_tangents, selected_only = _parse_cli_args()
out_dir = os.path.dirname(output_path)
if out_dir:
os.makedirs(out_dir, exist_ok=True)
scene = bpy.context.scene
_annotate_euv_extras(scene)
kwargs = _build_gltf_export_kwargs(
output_path, apply_modifiers, export_tangents, selected_only
)
result = bpy.ops.export_scene.gltf(**kwargs)
if "FINISHED" not in result:
raise SystemExit(f"glTF export failed with result: {result}")
if not os.path.isfile(output_path):
raise SystemExit(f"glTF export did not produce expected file: {output_path}")
print(f"EUV_BLENDER_EXPORT_OK:{output_path}")
if __name__ == "__main__":
main()