Files
shinyobjectz b2631ff5b6 raea/blender and raea/godot: Blender's and Godot's whole APIs from TypeScript (docs/blender.feature BL22-BL35, docs/godot.feature GD1-GD10)
Shane, 11-10: "include natively a blender api package in raea/blender using samchon/blendia as good starting place and
get us to a solid scripting parity to the official blender python api. we will own this ourselves", and "make sure to
add GodotJS which should add a more complete integration and bindings for godot integration".

raea/blender (sdk/blender, started from samchon/blendia, MIT, and rewritten): bpy, bmesh, mathutils, bpy_extras and
gpu typed from Blender 5.2.1's own RNA and docstrings (tools/gen.sh: 1809 structs, 2498 operators, 41 modules outside
RNA; 102 of about 12 900 members any), run against a real Blender. Paths are lazy as Python's attribute access is
(writes and calls sent in order, reads awaited, py.pin for a stable handle), errors are Python's (BlenderError),
mathutils is computed in TypeScript with Blender's maths (1450 values within 1e-5 of Blender's), float arrays are
the value Blender gives (Vector, Matrix, Euler, Quaternion, Color), a call that makes a thing by its type returns
that class (tools/refine.py), node sockets are typed by name, and every argument can be given by keyword.
`raea blender run SCRIPT.ts [FILE.blend] [ARGS...]` runs a script on raea's engine against a headless Blender
(apps/tools blender_bridge.rs, runtime/calls blender.post); under Bun or Node it starts its own, or joins the person's
Blender through raea's add-on (python/raea_blender).

raea/godot (sdk/godot) on GodotJS (MIT): Godot 4.6.1's every class, singleton, enum and builtin typed from GodotJS's
codegen, JavaScriptCore on the Mac and QuickJS-ng on Windows and Linux (no V8). `raea setup godot` downloads GodotJS's
editor build pinned by size and sha256 (apps/tools godot.rs); `raea godot run SCRIPT.ts` bundles the script and runs
its default export (a function given the SceneTree and ARGS, or a SceneTree subclass as the main loop) headless or
with --window, --movie recording with Godot's Movie Maker (the window kept on top, the frame size from the project),
a callback that throws stopping the run, RAEA_CWD with cwd() and resolve() for paths; `raea godot types` and
`raea godot open` (godot_run.rs).

The guides: raea guide bevy/blender gains "A step in TypeScript" and bevy/godot is new, each example type-checked
with its SDK's tests (sdk/blender/test/guide.ts, sdk/godot/test/guide.ts) and run as shown; raea guide bevy routes to
bevy/godot. Their raea-skills copies are 7497067 (branch blender-godot-ts, not pushed). sdk/tsconfig.base.json maps
raea/blender and raea/godot; tools/release.sh ships @raea/blender's python/.

Proven: sdk/blender's tsc and bun tests against Blender 5.2.1; sdk/godot's tsc and its 7 bun tests against the
installed GodotJS; apps/tools godot_run's unit tests; post 07b's film (releases/0.1.1/src/engines), its left half
built and keyed by raea/blender and its right half recorded by raea/godot.
2026-11-10 21:42:49 -08:00

146 lines
5.4 KiB
Python

# Values between TypeScript and Python. JSON carries None, bools, numbers, strings and lists as themselves; everything
# else is a tagged object ({"$tag": ...}). Python objects with identity (bpy structs, collections, bmesh elements,
# modules, functions) cross as references: {"$ref": id, "t": type name}, the id an index into the session's table.
# mathutils values cross by value, as raea/blender's local mathutils classes are the same maths in TypeScript.
import array
import base64
import math
import mathutils
# TypedArray kind -> array.array typecode (both sides little-endian on every platform Blender ships)
BUF = {"f32": "f", "f64": "d", "i8": "b", "u8": "B", "i16": "h", "u16": "H", "i32": "i", "u32": "I"}
BUF_KIND = {v: k for k, v in BUF.items()}
MAX_SAFE = 2**53
VIEWS = {"dict_keys", "dict_values", "dict_items", "odict_keys", "odict_values", "odict_items"}
class Refs:
"""A session's referenced Python objects. Ids the client chose are positive (slots: a call's result, a pin);
ids this side chose for returned objects are negative."""
def __init__(self):
self.table = {}
self.next = -1
def put(self, obj):
i = self.next
self.next -= 1
self.table[i] = obj
return i
def get(self, i):
try:
return self.table[i]
except KeyError:
raise ReferenceError(f"raea/blender: reference {i} was released or never set (an earlier error?)") from None
def set(self, i, obj):
self.table[i] = obj
def free(self, ids):
for i in ids:
self.table.pop(i, None)
def type_name(v):
rna = getattr(v, "bl_rna", None)
if rna is not None and not isinstance(v, type):
try:
return rna.identifier
except Exception:
pass
return type(v).__name__
def num(x):
return x if math.isfinite(x) else {"$f": "nan" if math.isnan(x) else ("inf" if x > 0 else "-inf")}
def nums(v):
return [num(x) for x in v]
def enc(v, refs):
"""A Python value for the wire."""
if v is None or v is True or v is False or isinstance(v, str):
return v
if isinstance(v, int):
return v if -MAX_SAFE < v < MAX_SAFE else {"$int": str(v)}
if isinstance(v, float):
return num(v)
t = type(v)
if t is list or t is tuple:
return [enc(x, refs) for x in v]
if isinstance(v, mathutils.Vector):
return {"$vec": nums(v)}
if isinstance(v, mathutils.Matrix):
return {"$mat": [nums(r) for r in v]}
if isinstance(v, mathutils.Quaternion):
return {"$quat": nums(v)}
if isinstance(v, mathutils.Euler):
return {"$eul": nums(v), "o": v.order}
if isinstance(v, mathutils.Color):
return {"$col": nums(v)}
if isinstance(v, (set, frozenset)):
return {"$set": [enc(x, refs) for x in sorted(v, key=repr)]}
if t is dict:
return {"$dict": [[enc(k, refs), enc(x, refs)] for k, x in v.items()]}
if isinstance(v, (bytes, bytearray)):
return {"$bytes": base64.b64encode(v).decode("ascii")}
if isinstance(v, array.array) and v.typecode in BUF_KIND:
return {"$buf": base64.b64encode(v.tobytes()).decode("ascii"), "t": BUF_KIND[v.typecode]}
name = t.__name__
if name == "bpy_prop_array":
return [enc(x, refs) for x in v]
if name == "IDPropertyArray":
return v.to_list()
if name == "Buffer" and t.__module__ == "gpu.types": # gpu.types.Buffer: its values, as nested lists
return v.to_list()
if isinstance(v, (list, tuple)) or name in VIEWS or name.startswith("IDPropertyGroupView"):
# a subclass (a struct sequence like bpy.app.version_file), or a dict view (keys(), values(), items())
return [enc(x, refs) for x in v]
return {"$ref": refs.put(v), "t": type_name(v)}
def dec(v, refs, resolve, bufs=None):
"""A wire value as Python. `resolve` evaluates a lazy path ({"$x": expr}); `bufs`, when given, collects the
array.array made for each {"$buf"} so a call can send back what it wrote into them (foreach_get)."""
if isinstance(v, list):
return [dec(x, refs, resolve, bufs) for x in v]
if not isinstance(v, dict):
return v
if "$ref" in v:
return refs.get(v["$ref"])
if "$x" in v:
return resolve(v["$x"])
if "$vec" in v:
return mathutils.Vector(dec(v["$vec"], refs, resolve))
if "$mat" in v:
return mathutils.Matrix(dec(v["$mat"], refs, resolve))
if "$quat" in v:
return mathutils.Quaternion(dec(v["$quat"], refs, resolve))
if "$eul" in v:
return mathutils.Euler(dec(v["$eul"], refs, resolve), v.get("o", "XYZ"))
if "$col" in v:
return mathutils.Color(dec(v["$col"], refs, resolve))
if "$set" in v:
return {dec(x, refs, resolve, bufs) for x in v["$set"]}
if "$tuple" in v:
return tuple(dec(x, refs, resolve, bufs) for x in v["$tuple"])
if "$dict" in v:
return {dec(k, refs, resolve, bufs): dec(x, refs, resolve, bufs) for k, x in v["$dict"]}
if "$bytes" in v:
return base64.b64decode(v["$bytes"])
if "$buf" in v:
a = array.array(BUF[v["t"]])
a.frombytes(base64.b64decode(v["$buf"]))
if bufs is not None:
bufs.append(a)
return a
if "$f" in v:
return {"nan": math.nan, "inf": math.inf, "-inf": -math.inf}[v["$f"]]
if "$int" in v:
return int(v["$int"])
raise TypeError(f"raea/blender: unknown value tag {sorted(v)}")