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
state=0
while( (getline < "./tool/blender-xrfragments.py") > 0 ){
if( $1 ~ /^}/ && state == REPLACE ) state=0
if( $0 ~ /^}/ && state == REPLACE ){
print $0
state=0
}
if( state == REPLACE ){
while( (getline < "./xrfragment-engine-prefixes.json") > 0 ){
gsub("true","True",$0)
gsub("false","False",$0)
gsub("True","true",$0)
gsub("False","false",$0)
if( $1 == "\"$version\":" ) $2 = ($2+1)"," # bump version
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 ---
SCHEMA = {
"$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",
"title": "XR Fragments",
"type": "object",
"$version": 2,
"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": {
"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": {
"oneOf":[
@ -123,7 +103,7 @@ SCHEMA = {
"-janus-scale": {"$ref": "#/$defs/-janus-scale"},
"-janus-rotation": {"$ref": "#/$defs/-janus-rotation" },
"-janus-locked": {"$ref": "#/$defs/-janus-locked"},
"-janus-draw-layer": {"$ref": "#/$defs/-janus-draw_layer"}
"-janus-draw-layer": {"$ref": "#/$defs/-janus-draw_layer"},
}
},
"janus_video": {
@ -140,7 +120,7 @@ SCHEMA = {
"-janus-scale": {"$ref": "#/$defs/-janus-scale"},
"-janus-rotation": {"$ref": "#/$defs/-janus-rotation" },
"-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 }
}
},
@ -430,7 +410,7 @@ SCHEMA = {
},
"-babylon-material.sideOrientation": {
"type": "string",
"description": "mat.backFaceCulling = True // or mat.sideOrientation",
"description": "mat.backFaceCulling = true // or mat.sideOrientation",
"enum": [
"BABYLON.Material.ClockWiseSideOrientation",
"BABYLON.Material.CounterClockWiseSideOrientation",
@ -470,28 +450,40 @@ SCHEMA = {
}
}
}
},
"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)"},
]
}
}
}
####### JSONSchema-to-form-to-custom-properties generator
#
# The code below was written based on LLM-generated boilerplate.
# I've had to rewrite every function, so at best it was a very convoluted
# way to lookup all necessary function-names.
#
# model: Gemini3 (fast)
# prompt: write a jsonschemaform-tab-to-custom properties-tab blender script.
# The form should appear in a tab of Object properties, and render a
# statically defined jsonschema with: oneOf, anyOf, allOf, $ref & $defs,
# datatypes like float,string,int,boolean, nesting, enums, default, title
# (for human form-display), enumHuman (for human enum display), description,
# pattern, minimum, maximum,
# Regarding output: when clicking an apply-button, write the JSON-object to
# the custom property-tab as 'JSON' with valuetype 'string'.
# 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
# property tab (only integer, string, boolean, float properties).
# --- 2. HELPERS ---
def resolve_schema_node(node, schema_root):
@ -504,21 +496,19 @@ def resolve_schema_node(node, schema_root):
return node
def get_scene_object_items(self, context):
items = ["-- select janus asset --"]
# Use a set to ensure unique names if necessary,
# but obj.name is always unique in Blender
items = [("NONE", "-- select target / asset --", "")]
if context is None:
return items
obj_list = []
for obj in context.scene.objects:
items.append((obj.name, obj.name, f"Object: {obj.name}"))
if not items:
return [("NONE", "No Objects Found", "")]
# Optional: Sort alphabetically
items.sort(key=lambda x: x[0])
obj_list.append((obj.name, obj.name, f"Object: {obj.name}"))
# Sort objects alphabetically by name
obj_list.sort(key=lambda x: x[1].lower())
items.extend(obj_list)
return items
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):
"""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():
attr_name = f"ui_{p_name}"
is_const = tell_if_const(p_name, p_info)
if not p_name.startswith("_"):
if is_const:
data[p_name] = p_info["default"]
elif hasattr(obj, attr_name):
@ -607,6 +597,12 @@ class OBJECT_OT_bake_schema(bpy.types.Operator):
else:
# Handle normal strings, ints, floats, etc.
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 ---
@ -629,9 +625,11 @@ class VIEW3D_PT_json_schema_props(bpy.types.Panel):
if opt["engine_val"] == active_engine), None)
if engine_opt:
box = layout.box()
layout.separator()
box = layout.column(align=True)
resolved_node = resolve_schema_node(engine_opt, SCHEMA)
self.draw_schema_node(box, obj, resolved_node)
layout.separator()
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."""
if "description" in node or "url" in node:
# Create a row and explicitly set alignment to LEFT
row = layout.row(align=True)
row.alignment = 'LEFT'
help_box = layout.column(align=True)
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"]
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()
# 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
properties = node.get("properties", {})
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)
if is_const:
if is_const or p_name.startswith("_"):
continue
attr_name = f"ui_{p_name}"
@ -701,9 +700,8 @@ class VIEW3D_PT_json_schema_props(bpy.types.Panel):
# --- 5. REGISTRATION ---
def register():
# 1. Register top-level engine selector
enum_items = [tuple(item) for item in SCHEMA["properties"]["engine"]["enum"]]
bpy.types.Object.engine = bpy.props.EnumProperty(
items=enum_items,
items=SCHEMA["properties"]["engine"]["enum"],
default="NONE"
)
@ -810,3 +808,4 @@ def register():
if __name__ == "__main__":
register()

View file

@ -1,41 +1,22 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://xrfragment.org/schema/xrfragment-engine-prefixes.json",
"$version": 34,
"title": "XR Fragments Engine Prefixes",
"title": "XR Fragments",
"type": "object",
"$version": 3,
"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": {
"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": {
"oneOf":[
@ -118,7 +99,7 @@
"-janus-scale": {"$ref": "#/$defs/-janus-scale"},
"-janus-rotation": {"$ref": "#/$defs/-janus-rotation" },
"-janus-locked": {"$ref": "#/$defs/-janus-locked"},
"-janus-draw-layer": {"$ref": "#/$defs/-janus-draw_layer"}
"-janus-draw-layer": {"$ref": "#/$defs/-janus-draw_layer"},
}
},
"janus_video": {
@ -135,7 +116,7 @@
"-janus-scale": {"$ref": "#/$defs/-janus-scale"},
"-janus-rotation": {"$ref": "#/$defs/-janus-rotation" },
"-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 }
}
},
@ -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)"},
]
}
}
}