update blender addon

This commit is contained in:
Leon van Kammen 2026-04-21 15:27:41 +02:00
parent a43a030c08
commit cda52c467d
3 changed files with 994 additions and 981 deletions

View file

@ -3,19 +3,21 @@ BEGIN {
REPLACE=1 REPLACE=1
state=0 state=0
while( (getline < "./tool/blender-xrfragments.py") > 0 ){ while( (getline < "./tool/blender-xrfragments.py") > 0 ){
if( $1 ~ /^}/ && state == REPLACE ) state=0 if( $0 ~ /^}/ && state == REPLACE ){
print $0 print $0
state=0
}
if( state == REPLACE ){ if( state == REPLACE ){
while( (getline < "./xrfragment-engine-prefixes.json") > 0 ){ gsub("True","true",$0)
gsub("true","True",$0) gsub("False","false",$0)
gsub("false","False",$0)
if( $1 == "\"$version\":" ) $2 = ($2+1)"," # bump version if( $1 == "\"$version\":" ) $2 = ($2+1)"," # bump version
if( $0 !~ /^[{}]/ ) print $0 if( $0 !~ /^[{}]/ ) print $0
} }
}
if( $1 == "SCHEMA" ) state=REPLACE if( $1 == "SCHEMA" ){
state=REPLACE
print "{"
}
} }
} }

View file

@ -3,44 +3,24 @@ import json
# --- 1. SCHEMA --- # --- 1. SCHEMA ---
SCHEMA = { SCHEMA = {
"title": "XR Fragments",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://xrfragment.org/schema/xrfragment-engine-prefixes.json",
"$version": 35,
"title": "XR Fragments Engine Prefixes",
"type": "object", "type": "object",
"$version": 2,
"flatten": True, "flatten": True,
"properties": {
"engine": {
"type": "enum",
"enum": [
["NONE", "-- Select --", ""],
["JANUSXR", "JanusXR", ""],
["THREEJS", "Three.js", ""],
["GODOT3", "Godot 3", ""],
["GODOT4", "Godot 4", ""],
["AFRAME", "AFRAME", ""],
["BABYLON", "Babylon.js", ""],
["PLAYCANVAS", "PlayCanvas", ""]
],
"default": "NONE"
},
"engine_settings": {
"oneOf": [
{"engine_val": "JANUSXR", "$ref": "#/$defs/janus", "title": "JanusXR"},
{"engine_val": "THREEJS", "$ref": "#/$defs/three", "title": "THREE.js"},
{"engine_val": "GODOT3", "$ref": "#/$defs/godot3", "title": "Godot3 (beta)"},
{"engine_val": "GODOT4", "$ref": "#/$defs/godot4", "title": "Godot4 (beta)"},
{"engine_val": "AFRAME", "$ref": "#/$defs/aframe", "title": "AFRAME (beta)"},
{"engine_val": "BABYLON", "$ref": "#/$defs/babylon", "title": "Babylon.js (beta)"},
{"engine_val": "PLAYCANVAS", "$ref": "#/$defs/playcanvas", "title": "PlayCanvas (beta)"}
]
}
},
"$defs": { "$defs": {
"xrf":{
"title": "XR Fragments",
"description": "XR Fragments (level2) promotes embedding clickable hyperlinks\nin 3D files (via the href attribute).\nMake sure to check 'custom attributes' in your export dialog.",
"url": "https://xrfragment.org/#How%20it%20works",
"properties":{
"_xrf-type": { "title": "type", "type": "string", "default":"xrf", "description": "needed for blender UI"},
"href":{
"type":"object.name",
"description": "teleport to / href"
}
}
},
"three": { "three": {
"oneOf":[ "oneOf":[
@ -123,7 +103,7 @@ SCHEMA = {
"-janus-scale": {"$ref": "#/$defs/-janus-scale"}, "-janus-scale": {"$ref": "#/$defs/-janus-scale"},
"-janus-rotation": {"$ref": "#/$defs/-janus-rotation" }, "-janus-rotation": {"$ref": "#/$defs/-janus-rotation" },
"-janus-locked": {"$ref": "#/$defs/-janus-locked"}, "-janus-locked": {"$ref": "#/$defs/-janus-locked"},
"-janus-draw-layer": {"$ref": "#/$defs/-janus-draw_layer"} "-janus-draw-layer": {"$ref": "#/$defs/-janus-draw_layer"},
} }
}, },
"janus_video": { "janus_video": {
@ -140,7 +120,7 @@ SCHEMA = {
"-janus-scale": {"$ref": "#/$defs/-janus-scale"}, "-janus-scale": {"$ref": "#/$defs/-janus-scale"},
"-janus-rotation": {"$ref": "#/$defs/-janus-rotation" }, "-janus-rotation": {"$ref": "#/$defs/-janus-rotation" },
"-janus-locked": {"$ref": "#/$defs/-janus-locked"}, "-janus-locked": {"$ref": "#/$defs/-janus-locked"},
"-janus-draw-layer": {"$ref": "#/$defs/-janus-draw_layer"}, "-janus-draw-layer": {"$ref": "#/$defs/-janus-draw_layer"}
"-janus-gain": { "type":"float", "minimum":0, "maximum":1 } "-janus-gain": { "type":"float", "minimum":0, "maximum":1 }
} }
}, },
@ -430,7 +410,7 @@ SCHEMA = {
}, },
"-babylon-material.sideOrientation": { "-babylon-material.sideOrientation": {
"type": "string", "type": "string",
"description": "mat.backFaceCulling = True // or mat.sideOrientation", "description": "mat.backFaceCulling = true // or mat.sideOrientation",
"enum": [ "enum": [
"BABYLON.Material.ClockWiseSideOrientation", "BABYLON.Material.ClockWiseSideOrientation",
"BABYLON.Material.CounterClockWiseSideOrientation", "BABYLON.Material.CounterClockWiseSideOrientation",
@ -470,27 +450,39 @@ SCHEMA = {
} }
} }
} }
}
}
####### JSONSchema-to-form-to-custom-properties generator
# },
# The code below was written based on LLM-generated boilerplate. "properties": {
# I've had to rewrite every function, so at best it was a very convoluted "engine": {
# way to lookup all necessary function-names. "type": "enum",
# "enum": [
# model: Gemini3 (fast) ("NONE", "-- Select --", ""),
# prompt: write a jsonschemaform-tab-to-custom properties-tab blender script. ("XRF", "generic: hyperlinking", ""),
# The form should appear in a tab of Object properties, and render a ("JANUSXR", "JanusXR", ""),
# statically defined jsonschema with: oneOf, anyOf, allOf, $ref & $defs, ("THREEJS", "Three.js", ""),
# datatypes like float,string,int,boolean, nesting, enums, default, title ("GODOT3", "Godot 3", ""),
# (for human form-display), enumHuman (for human enum display), description, ("GODOT4", "Godot 4", ""),
# pattern, minimum, maximum, ("AFRAME", "AFRAME", ""),
# Regarding output: when clicking an apply-button, write the JSON-object to ("BABYLON", "Babylon.js", ""),
# the custom property-tab as 'JSON' with valuetype 'string'. ("PLAYCANVAS", "PlayCanvas", "")
# If 'flatten' is set on the schema-root, collect all selected key/value ],
# properties from the form, and write each key/value separately to the custom "default": "NONE"
# property tab (only integer, string, boolean, float properties). },
"engine_settings": {
"oneOf": [
{"engine_val": "XRF", "$ref": "#/$defs/xrf", "title": "XR Fragment"},
{"engine_val": "JANUSXR", "$ref": "#/$defs/janus", "title": "JanusXR"},
{"engine_val": "THREEJS", "$ref": "#/$defs/three", "title": "THREE.js"},
{"engine_val": "GODOT3", "$ref": "#/$defs/godot3", "title": "Godot3 (beta)"},
{"engine_val": "GODOT4", "$ref": "#/$defs/godot4", "title": "Godot4 (beta)"},
{"engine_val": "AFRAME", "$ref": "#/$defs/aframe", "title": "AFRAME (beta)"},
{"engine_val": "BABYLON", "$ref": "#/$defs/babylon", "title": "Babylon.js (beta)"},
{"engine_val": "PLAYCANVAS", "$ref": "#/$defs/playcanvas", "title": "PlayCanvas (beta)"},
]
}
}
}
# --- 2. HELPERS --- # --- 2. HELPERS ---
@ -504,21 +496,19 @@ def resolve_schema_node(node, schema_root):
return node return node
def get_scene_object_items(self, context): def get_scene_object_items(self, context):
items = ["-- select janus asset --"] items = [("NONE", "-- select target / asset --", "")]
# Use a set to ensure unique names if necessary, if context is None:
# but obj.name is always unique in Blender return items
obj_list = []
for obj in context.scene.objects: for obj in context.scene.objects:
items.append((obj.name, obj.name, f"Object: {obj.name}")) obj_list.append((obj.name, obj.name, f"Object: {obj.name}"))
# Sort objects alphabetically by name
if not items: obj_list.sort(key=lambda x: x[1].lower())
return [("NONE", "No Objects Found", "")] items.extend(obj_list)
# Optional: Sort alphabetically
items.sort(key=lambda x: x[0])
return items return items
def tell_if_const(p_name, p_info): def tell_if_const(p_name, p_info):
return p_name.startswith("_") or (p_info.get("type") == "string" and "default" in p_info and len(p_info) == 2) return (p_info.get("type") == "string" and "default" in p_info and len(p_info) == 2)
def rgb_to_hex(rgb): def rgb_to_hex(rgb):
"""Converts linear RGB floats to sRGB Hex string.""" """Converts linear RGB floats to sRGB Hex string."""
@ -593,7 +583,7 @@ class OBJECT_OT_bake_schema(bpy.types.Operator):
for p_name, p_info in properties.items(): for p_name, p_info in properties.items():
attr_name = f"ui_{p_name}" attr_name = f"ui_{p_name}"
is_const = tell_if_const(p_name, p_info) is_const = tell_if_const(p_name, p_info)
if not p_name.startswith("_"):
if is_const: if is_const:
data[p_name] = p_info["default"] data[p_name] = p_info["default"]
elif hasattr(obj, attr_name): elif hasattr(obj, attr_name):
@ -607,6 +597,12 @@ class OBJECT_OT_bake_schema(bpy.types.Operator):
else: else:
# Handle normal strings, ints, floats, etc. # Handle normal strings, ints, floats, etc.
data[p_name] = val data[p_name] = val
self.xrf_compose(data,p_name)
# 3. compose correct XR Fragment hrefs
def xrf_compose(self, data, p_name):
if p_name == "href":
data[p_name] = "#" + data[p_name]
# --- 4. PANEL --- # --- 4. PANEL ---
@ -629,9 +625,11 @@ class VIEW3D_PT_json_schema_props(bpy.types.Panel):
if opt["engine_val"] == active_engine), None) if opt["engine_val"] == active_engine), None)
if engine_opt: if engine_opt:
box = layout.box() layout.separator()
box = layout.column(align=True)
resolved_node = resolve_schema_node(engine_opt, SCHEMA) resolved_node = resolve_schema_node(engine_opt, SCHEMA)
self.draw_schema_node(box, obj, resolved_node) self.draw_schema_node(box, obj, resolved_node)
layout.separator()
layout.operator("object.bake_schema") layout.operator("object.bake_schema")
@ -639,14 +637,15 @@ class VIEW3D_PT_json_schema_props(bpy.types.Panel):
"""Recursively renders the schema UI with support for conditional branching.""" """Recursively renders the schema UI with support for conditional branching."""
if "description" in node or "url" in node: if "description" in node or "url" in node:
# Create a row and explicitly set alignment to LEFT help_box = layout.column(align=True)
row = layout.row(align=True)
row.alignment = 'LEFT'
if "url" in node: if "url" in node:
op = row.operator("wm.url_open", text="", icon='HELP', emboss=False) op = help_box.operator("wm.url_open", text="Documentation", icon='URL')
op.url = node["url"] op.url = node["url"]
if "description" in node: if "description" in node:
row.label(text=node["description"] ) description_text = node["description"]
for line in description_text.split("\n"):
help_box.label(text=line)
layout.separator() # Add space before properties
layout.separator() layout.separator()
# 1. Handle oneOf Routing # 1. Handle oneOf Routing
@ -682,9 +681,9 @@ class VIEW3D_PT_json_schema_props(bpy.types.Panel):
# 2. Draw standard properties for the current node # 2. Draw standard properties for the current node
properties = node.get("properties", {}) properties = node.get("properties", {})
for p_name, p_info in properties.items(): for p_name, p_info in properties.items():
# Skip 'const' properties (Internal tags that shouldn't be edited) # Skip underscore properties + (Internal tags that shouldn't be edited)
is_const = tell_if_const(p_name, p_info) is_const = tell_if_const(p_name, p_info)
if is_const: if is_const or p_name.startswith("_"):
continue continue
attr_name = f"ui_{p_name}" attr_name = f"ui_{p_name}"
@ -701,9 +700,8 @@ class VIEW3D_PT_json_schema_props(bpy.types.Panel):
# --- 5. REGISTRATION --- # --- 5. REGISTRATION ---
def register(): def register():
# 1. Register top-level engine selector # 1. Register top-level engine selector
enum_items = [tuple(item) for item in SCHEMA["properties"]["engine"]["enum"]]
bpy.types.Object.engine = bpy.props.EnumProperty( bpy.types.Object.engine = bpy.props.EnumProperty(
items=enum_items, items=SCHEMA["properties"]["engine"]["enum"],
default="NONE" default="NONE"
) )
@ -810,3 +808,4 @@ def register():
if __name__ == "__main__": if __name__ == "__main__":
register() register()

View file

@ -1,41 +1,22 @@
{ {
"$schema": "https://json-schema.org/draft/2020-12/schema", "title": "XR Fragments",
"$id": "https://xrfragment.org/schema/xrfragment-engine-prefixes.json",
"$version": 34,
"title": "XR Fragments Engine Prefixes",
"type": "object", "type": "object",
"$version": 3,
"flatten": true, "flatten": true,
"properties": {
"engine": {
"type": "enum",
"enum": [
["NONE", "-- Select --", ""],
["JANUSXR", "JanusXR", ""],
["THREEJS", "Three.js", ""],
["GODOT3", "Godot 3", ""],
["GODOT4", "Godot 4", ""],
["AFRAME", "AFRAME", ""],
["BABYLON", "Babylon.js", ""],
["PLAYCANVAS", "PlayCanvas", ""]
],
"default": "NONE"
},
"engine_settings": {
"oneOf": [
{"engine_val": "JANUSXR", "$ref": "#/$defs/janus", "title": "JanusXR"},
{"engine_val": "THREEJS", "$ref": "#/$defs/three", "title": "THREE.js"},
{"engine_val": "GODOT3", "$ref": "#/$defs/godot3", "title": "Godot3 (beta)"},
{"engine_val": "GODOT4", "$ref": "#/$defs/godot4", "title": "Godot4 (beta)"},
{"engine_val": "AFRAME", "$ref": "#/$defs/aframe", "title": "AFRAME (beta)"},
{"engine_val": "BABYLON", "$ref": "#/$defs/babylon", "title": "Babylon.js (beta)"},
{"engine_val": "PLAYCANVAS", "$ref": "#/$defs/playcanvas", "title": "PlayCanvas (beta)"}
]
}
},
"$defs": { "$defs": {
"xrf":{
"title": "XR Fragments",
"description": "XR Fragments (level2) promotes embedding clickable hyperlinks\nin 3D files (via the href attribute).\nMake sure to check 'custom attributes' in your export dialog.",
"url": "https://xrfragment.org/#How%20it%20works",
"properties":{
"_xrf-type": { "title": "type", "type": "string", "default":"xrf", "description": "needed for blender UI"},
"href":{
"type":"object.name",
"description": "teleport to / href"
}
}
},
"three": { "three": {
"oneOf":[ "oneOf":[
@ -118,7 +99,7 @@
"-janus-scale": {"$ref": "#/$defs/-janus-scale"}, "-janus-scale": {"$ref": "#/$defs/-janus-scale"},
"-janus-rotation": {"$ref": "#/$defs/-janus-rotation" }, "-janus-rotation": {"$ref": "#/$defs/-janus-rotation" },
"-janus-locked": {"$ref": "#/$defs/-janus-locked"}, "-janus-locked": {"$ref": "#/$defs/-janus-locked"},
"-janus-draw-layer": {"$ref": "#/$defs/-janus-draw_layer"} "-janus-draw-layer": {"$ref": "#/$defs/-janus-draw_layer"},
} }
}, },
"janus_video": { "janus_video": {
@ -135,7 +116,7 @@
"-janus-scale": {"$ref": "#/$defs/-janus-scale"}, "-janus-scale": {"$ref": "#/$defs/-janus-scale"},
"-janus-rotation": {"$ref": "#/$defs/-janus-rotation" }, "-janus-rotation": {"$ref": "#/$defs/-janus-rotation" },
"-janus-locked": {"$ref": "#/$defs/-janus-locked"}, "-janus-locked": {"$ref": "#/$defs/-janus-locked"},
"-janus-draw-layer": {"$ref": "#/$defs/-janus-draw_layer"}, "-janus-draw-layer": {"$ref": "#/$defs/-janus-draw_layer"}
"-janus-gain": { "type":"float", "minimum":0, "maximum":1 } "-janus-gain": { "type":"float", "minimum":0, "maximum":1 }
} }
}, },
@ -465,5 +446,36 @@
} }
} }
} }
},
"properties": {
"engine": {
"type": "enum",
"enum": [
("NONE", "-- Select --", ""),
("XRF", "generic: hyperlinking", ""),
("JANUSXR", "JanusXR", ""),
("THREEJS", "Three.js", ""),
("GODOT3", "Godot 3", ""),
("GODOT4", "Godot 4", ""),
("AFRAME", "AFRAME", ""),
("BABYLON", "Babylon.js", ""),
("PLAYCANVAS", "PlayCanvas", "")
],
"default": "NONE"
},
"engine_settings": {
"oneOf": [
{"engine_val": "XRF", "$ref": "#/$defs/xrf", "title": "XR Fragment"},
{"engine_val": "JANUSXR", "$ref": "#/$defs/janus", "title": "JanusXR"},
{"engine_val": "THREEJS", "$ref": "#/$defs/three", "title": "THREE.js"},
{"engine_val": "GODOT3", "$ref": "#/$defs/godot3", "title": "Godot3 (beta)"},
{"engine_val": "GODOT4", "$ref": "#/$defs/godot4", "title": "Godot4 (beta)"},
{"engine_val": "AFRAME", "$ref": "#/$defs/aframe", "title": "AFRAME (beta)"},
{"engine_val": "BABYLON", "$ref": "#/$defs/babylon", "title": "Babylon.js (beta)"},
{"engine_val": "PLAYCANVAS", "$ref": "#/$defs/playcanvas", "title": "PlayCanvas (beta)"},
]
}
} }
} }