@babylonjs-toolkit/agent 1.2.16 → 1.2.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -2
- package/package.json +1 -1
- package/skills/bt-blender/SKILL.md +386 -0
- package/skills/bt-blender/THIRD_PARTY_NOTICES.md +141 -0
- package/skills/bt-blender/references/art-style-and-genre.md +173 -0
- package/skills/bt-blender/references/characters-and-creatures.md +644 -0
- package/skills/bt-blender/references/environments.md +478 -0
- package/skills/bt-blender/references/game-engine-export.md +284 -0
- package/skills/bt-blender/references/geometry-nodes.md +447 -0
- package/skills/bt-blender/references/hard-surface.md +720 -0
- package/skills/bt-blender/references/procedural-generation.md +358 -0
- package/skills/bt-blender/references/rigging-and-animation.md +1080 -0
- package/skills/bt-blender/references/sculpting-and-retopology.md +544 -0
- package/skills/bt-blender/references/uv-and-pbr-materials.md +381 -0
- package/skills/bt-blender/references/vehicles.md +632 -0
- package/skills/bt-blender/scripts/blender/assembly_check.py +420 -0
- package/skills/bt-blender/scripts/blender/character_blockout.py +462 -0
- package/skills/bt-blender/scripts/blender/construction_kit.py +1710 -0
- package/skills/bt-blender/scripts/blender/environment_kit.py +525 -0
- package/skills/bt-blender/scripts/blender/export_asset.py +623 -0
- package/skills/bt-blender/scripts/blender/geometry_nodes_builders.py +575 -0
- package/skills/bt-blender/scripts/blender/hard_surface_panels.py +374 -0
- package/skills/bt-blender/scripts/blender/hard_surface_trims.py +200 -0
- package/skills/bt-blender/scripts/blender/image_measure.py +568 -0
- package/skills/bt-blender/scripts/blender/materials_pbr.py +439 -0
- package/skills/bt-blender/scripts/blender/measure_stations.py +486 -0
- package/skills/bt-blender/scripts/blender/ortho_views.py +309 -0
- package/skills/bt-blender/scripts/blender/procedural_game_ready.py +258 -0
- package/skills/bt-blender/scripts/blender/procedural_generators.py +842 -0
- package/skills/bt-blender/scripts/blender/qa_check.py +729 -0
- package/skills/bt-blender/scripts/blender/retopology_tools.py +781 -0
- package/skills/bt-blender/scripts/blender/review_render.py +358 -0
- package/skills/bt-blender/scripts/blender/rigging_tools.py +1170 -0
- package/skills/bt-blender/scripts/blender/style_palette.py +190 -0
- package/skills/bt-blender/scripts/blender/tool_common.py +289 -0
- package/skills/bt-blender/scripts/blender/uv_layout.py +555 -0
- package/skills/bt-blender/scripts/blender/vehicle_parts.py +370 -0
- package/skills/bt-blender/scripts/blender/view_consistency.py +133 -0
- package/skills/bt-blender/scripts/blender/world_gate.py +219 -0
- package/skills/bt-blender/scripts/bt-blender.mjs +329 -0
- package/skills/bt-blender/scripts/validate_gltf.mjs +204 -0
- package/skills/bt-gauntlet/SKILL.md +6 -6
- package/skills/bt-gauntlet/references/critic-rubrics.md +71 -4
- package/skills/bt-gauntlet/references/spec-composition.md +13 -0
- package/skills/bt-gauntlet/references/unity-pipeline.md +18 -2
- package/skills/bt-unwrap/scripts/blender/__pycache__/bt_unwrap.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/image_utils.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/mesh_tools.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/model_io.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/previews.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/rasterizer.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/reference_views.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/reference_warp.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/texture_bake.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/texture_compose.cpython-313.pyc +0 -0
- package/skills/bt-unwrap/scripts/blender/__pycache__/uv_layout.cpython-313.pyc +0 -0
|
@@ -0,0 +1,447 @@
|
|
|
1
|
+
# Geometry Nodes: scatter, sweeps, arrays, and shipping instances to engines
|
|
2
|
+
|
|
3
|
+
**Load this when** an asset or a level piece is made by rule rather than by hand: rocks, grass tufts or debris
|
|
4
|
+
scattered over a surface, a road, rail, kerb or cable swept along a path, posts, sleepers or lamps placed
|
|
5
|
+
along a curve, or any parametric piece you will regenerate with new numbers. Load it in particular when you
|
|
6
|
+
must decide whether repeated objects ship as one realised mesh or as instances, because that decision changes
|
|
7
|
+
the file size, the draw calls and what each engine does on import.
|
|
8
|
+
|
|
9
|
+
**What it assumes.** The Agent Reference's Blender guide (`references/unity-blender-cli.md`) covers the basics:
|
|
10
|
+
§6 (operators need context and return CANCELLED instead of raising), §7.1 (the 5.x API: build interfaces with
|
|
11
|
+
`interface.new_socket`, set modifier inputs through `modifier.properties.inputs`, never `mod["Socket_1"]`) and
|
|
12
|
+
§3 (the import and export operators). This file goes deeper and does not repeat them.
|
|
13
|
+
|
|
14
|
+
**Helpers.** `scripts/blender/geometry_nodes_builders.py`:
|
|
15
|
+
- building trees: `new_geometry_group`, `add_node`, `link`, `input_identifiers`;
|
|
16
|
+
- driving modifiers: `add_modifier`, `set_input`, `set_input_attribute`, `node_warnings`;
|
|
17
|
+
- ready groups: `scatter_group`, `sweep_group`, `curve_array_group`, `realize_group`;
|
|
18
|
+
- measuring: `instance_count`, `instance_transforms`;
|
|
19
|
+
- shipping: `realize_to_mesh`, `instances_to_objects`.
|
|
20
|
+
|
|
21
|
+
Every recipe below is a complete script. Save it and run
|
|
22
|
+
`node <skill>/scripts/bt-blender.mjs run recipe.py <out folder>`; the launcher puts the helpers on `sys.path`.
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## 1. The professional workflow
|
|
27
|
+
|
|
28
|
+
1. **One node group per job, with its knobs on the interface.** Density, seed, scale range, spacing: every
|
|
29
|
+
number you might tune is a group input, so a build script sets it and nobody edits nodes. Name the groups
|
|
30
|
+
(`BT_Scatter`) and rebuild them in place on every run: `new_geometry_group` clears a group of the same name
|
|
31
|
+
instead of making `.001` copies.
|
|
32
|
+
2. **Instances first, realise last.** Points, instances and curves are cheap; real geometry is not. Measured on
|
|
33
|
+
5.3.0: 50,000 scattered icospheres (642 vertices each) evaluated in 0.004 s as instances and in 0.15 s, with
|
|
34
|
+
8.1 million vertices, realised. Everything that can act on instances (rotate, scale, pick a variant)
|
|
35
|
+
happens before any Realize Instances node.
|
|
36
|
+
3. **Masks are data, not node edits.** Paint or compute a vertex group or attribute (`scatter_mask`,
|
|
37
|
+
`path_mask`), feed it to a field input with `set_input_attribute`, and the group never changes per level.
|
|
38
|
+
4. **Seed everything, record the seed.** Every random node has a seed input fed from the group's Seed plus a
|
|
39
|
+
per-purpose offset, so scale, yaw and variant pick are independent streams. Write the seed into the asset
|
|
40
|
+
(a custom property) or the variation manifest (see `procedural-generation.md`).
|
|
41
|
+
5. **Measure before you look.** `instance_count` and `instance_transforms` (sorted, so two runs compare value
|
|
42
|
+
for value) prove the result. `node_warnings(modifier)` returns what the node editor would show in red:
|
|
43
|
+
check it after every build, because a broken link does not raise.
|
|
44
|
+
6. **Decide how it ships** (section 6), convert, export, validate (`export`, `validate-gltf`) and look at it in
|
|
45
|
+
the engine.
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
## 2. Building node trees headless on 5.3
|
|
50
|
+
|
|
51
|
+
`new_geometry_group(name, inputs, outputs)` creates the interface (`tree.interface.new_socket`), a Group Input
|
|
52
|
+
and a Group Output. Inputs declared with `"field": True` accept a per-element attribute from the modifier.
|
|
53
|
+
Then `add_node(tree, "GeometryNode…", column, row, **properties)` and `link(tree, a, "Socket", b, "Socket")`.
|
|
54
|
+
|
|
55
|
+
Set node properties **before** linking: `data_type`, `mode`, `domain` and `input_type` decide which sockets
|
|
56
|
+
exist. Many nodes keep a socket per mode with the same name and only enable the current one; `link` takes
|
|
57
|
+
the *enabled* socket of that name and raises `KeyError` listing the enabled ones when there is none.
|
|
58
|
+
|
|
59
|
+
Socket facts read on 5.3.0 that differ from tutorials and from memory:
|
|
60
|
+
|
|
61
|
+
| Node | What 5.3.0 has |
|
|
62
|
+
|---|---|
|
|
63
|
+
| Random Value | Only four inputs per data type: `Min`, `Max`, `ID`, `Seed` (FLOAT also has `Probability` for BOOLEAN). Set `data_type` first, then link by name; positional indices from older versions point at disabled sockets |
|
|
64
|
+
| Resample Curve | `Mode` is a **menu socket** (`"Count"` / `"Length"`), set with `node.inputs["Mode"].default_value = "Length"`, not a node property |
|
|
65
|
+
| Set Curve Normal | `Mode` is a menu socket too: `"Minimum Twist"`, `"Z Up"`; inputs `Curve`, `Selection`, `Mode`, `Normal` |
|
|
66
|
+
| Set Shade Smooth | Geometry input is called `Mesh`, not `Geometry`; the output is index 0 |
|
|
67
|
+
| Distribute Points on Faces | Inputs `Mesh`, `Selection`, `Density`, `Seed`; outputs `Points`, `Normal`, `Rotation`; `distribute_method` `RANDOM` or `POISSON` |
|
|
68
|
+
| Instance on Points | `Points`, `Selection`, `Instance`, `Pick Instance`, `Instance Index`, `Rotation`, `Scale` |
|
|
69
|
+
| Realize Instances | `Geometry`, `Selection`, `Realize All`, `Depth`, `Preserve Normals` (a Selection makes a partial realise possible) |
|
|
70
|
+
| Curve to Mesh | `Curve`, `Profile Curve`, `Scale`, `Fill Caps` plus miter inputs; point attributes of both the path and the profile reach the mesh (measured) |
|
|
71
|
+
| Curve to Points | `mode` `EVALUATED` / `COUNT` / `LENGTH`; outputs `Points`, `Tangent`, `Normal`, `Rotation` |
|
|
72
|
+
| Collection Info | `Separate Children` and `Reset Children` inputs: both on to pick one child per point at the child's own origin |
|
|
73
|
+
| Switch | `input_type="GEOMETRY"` gives `Switch`, `False`, `True` -> `Output` |
|
|
74
|
+
|
|
75
|
+
The modifier side: `set_input(modifier, "Density", 3.0)` writes
|
|
76
|
+
`getattr(modifier.properties.inputs, identifier).value`; `set_input_attribute(modifier, "Density Mask",
|
|
77
|
+
"scatter_mask")` switches that input's `type` to `ATTRIBUTE` and names the attribute (a vertex group counts).
|
|
78
|
+
|
|
79
|
+
**The rotation frame of a point on a curve.** Curve to Points and Sample Curve give a rotation whose local Z is
|
|
80
|
+
the tangent and local X the curve normal (measured). An asset modelled with -Y forward and +Z up needs the
|
|
81
|
+
local Euler (-90°, 0°, 180°) on top; `curve_array_group` applies it (`CURVE_FRAME_TO_ASSET_EULER`). Measured on
|
|
82
|
+
a rising curve: the post's -Y lay along the chord (0.80, 0.59, 0.12) and its +Z stayed at 0.993 of vertical.
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
## 3. Scatter: density, masks, rotation and scale
|
|
87
|
+
|
|
88
|
+
`scatter_group` is the production pattern: Distribute Points on Faces with `Density x mask`, a Collection
|
|
89
|
+
Info of variants, a random variant per point, a seeded uniform scale, an optional alignment to the surface and
|
|
90
|
+
a random yaw about the instance's own up axis, optionally the surface kept and optionally a realise.
|
|
91
|
+
|
|
92
|
+
| Decision | Choose | Because |
|
|
93
|
+
|---|---|---|
|
|
94
|
+
| Random or Poisson disk | **Random** for ground cover, debris, foliage; **Poisson** (`Distance Min`) for props that must not touch (rocks, trees, crates) | Random is cheap and its count tracks density x area (measured: density 5 on 10,000 m² gave exactly 50,000); Poisson rejects points, so the count falls below density x area as the distance grows |
|
|
95
|
+
| Mask by vertex group, attribute or texture | **Vertex group / attribute** for authored masks; a **texture** only when the mask is finer than the mesh | A vertex-group mask is interpolated across faces, so its resolution is the mesh's: subdivide the ground where the edge of the mask matters |
|
|
96
|
+
| Align to normal | **0** for trees, posts, buildings (they grow up); **0.6–1** for rocks, debris, moss, decals on slopes | Upright objects on a slope look wrong tilted; loose objects look glued on when upright |
|
|
97
|
+
| Scale range | 0.8–1.2 for props, 0.6–1.4 for rocks and foliage; never negative | A negative scale mirrors the instance: BabylonJS thin instances with mixed positive and negative determinants render wrong (its documented limitation) |
|
|
98
|
+
| Yaw | Random for everything without a facing | Repeated identical orientation is the first thing a player notices |
|
|
99
|
+
| Variants | 3–5 per kind | Fewer reads as copy-paste; more costs a draw call each (section 6) |
|
|
100
|
+
|
|
101
|
+
**The density mask is a probability per point.** Points are generated at the full density, then each survives
|
|
102
|
+
with the mask's value at its position. So the expected count is density x ∫mask dA, and the spread is
|
|
103
|
+
binomial: measured, density 3 over a half-masked 10 m ground (∫mask dA = 47.5 m²) gave 136 instances where
|
|
104
|
+
142.5 were expected. Test a scatter against expected ± 4·√expected, and pin the exact count only for a fixed
|
|
105
|
+
seed.
|
|
106
|
+
|
|
107
|
+
**Determinism.** Distribute Points on Faces is deterministic for the same mesh, density and seed. Every per-point
|
|
108
|
+
random value is hashed from the point's `ID` (which Distribute writes) and a seed, so scale, yaw and pick do not
|
|
109
|
+
shift when an unrelated input changes. Changing the ground's topology changes every point: freeze the ground
|
|
110
|
+
mesh before you tune a scatter.
|
|
111
|
+
|
|
112
|
+
### Recipe: a masked scatter, shipped realised and as GPU instances
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
# recipe: gn-scatter
|
|
116
|
+
import os, sys
|
|
117
|
+
import bpy
|
|
118
|
+
import construction_kit as kit
|
|
119
|
+
import geometry_nodes_builders as gn
|
|
120
|
+
|
|
121
|
+
out = sys.argv[sys.argv.index("--") + 1]
|
|
122
|
+
kit.clear_default_scene()
|
|
123
|
+
|
|
124
|
+
# Ground: 10 m, 0.5 m cells; the mask covers x < -0.25 (interpolated over one cell).
|
|
125
|
+
bpy.ops.mesh.primitive_grid_add(x_subdivisions=20, y_subdivisions=20, size=10.0)
|
|
126
|
+
ground = bpy.context.active_object
|
|
127
|
+
ground.name = "Ground"
|
|
128
|
+
mask = ground.vertex_groups.new(name="scatter_mask")
|
|
129
|
+
for vertex in ground.data.vertices:
|
|
130
|
+
mask.add([vertex.index], 1.0 if vertex.co.x < -0.25 else 0.0, "REPLACE")
|
|
131
|
+
|
|
132
|
+
# Variants live in a collection excluded from the view layer: they are only instanced.
|
|
133
|
+
variants = bpy.data.collections.new("RockVariants")
|
|
134
|
+
bpy.context.scene.collection.children.link(variants)
|
|
135
|
+
for name, subdivisions in (("RockA", 1), ("RockB", 2)):
|
|
136
|
+
bpy.ops.mesh.primitive_ico_sphere_add(subdivisions=subdivisions, radius=0.2)
|
|
137
|
+
rock = bpy.context.active_object
|
|
138
|
+
rock.name = rock.data.name = name
|
|
139
|
+
for collection in list(rock.users_collection):
|
|
140
|
+
collection.objects.unlink(rock)
|
|
141
|
+
variants.objects.link(rock)
|
|
142
|
+
bpy.context.view_layer.layer_collection.children["RockVariants"].exclude = True
|
|
143
|
+
|
|
144
|
+
modifier = gn.add_modifier(ground, gn.scatter_group())
|
|
145
|
+
gn.set_input(modifier, "Instances", variants)
|
|
146
|
+
gn.set_input(modifier, "Density", 3.0)
|
|
147
|
+
gn.set_input(modifier, "Seed", 11)
|
|
148
|
+
gn.set_input(modifier, "Align To Normal", 0.8)
|
|
149
|
+
gn.set_input_attribute(modifier, "Density Mask", "scatter_mask")
|
|
150
|
+
bpy.context.view_layer.update()
|
|
151
|
+
assert not any(gn.node_warnings(modifier)), gn.node_warnings(modifier)
|
|
152
|
+
print("instances", gn.instance_count(ground))
|
|
153
|
+
|
|
154
|
+
def export_selected(path, objects, **options):
|
|
155
|
+
bpy.context.view_layer.update()
|
|
156
|
+
for obj in bpy.context.view_layer.objects:
|
|
157
|
+
obj.select_set(obj in objects)
|
|
158
|
+
assert "FINISHED" in bpy.ops.export_scene.gltf(filepath=path, export_format="GLB", use_selection=True,
|
|
159
|
+
export_apply=True, **options)
|
|
160
|
+
|
|
161
|
+
# Realised: one mesh, one draw call, every vertex stored.
|
|
162
|
+
realised = ground.copy()
|
|
163
|
+
realised.data = ground.data.copy()
|
|
164
|
+
realised.name = "GroundRealised"
|
|
165
|
+
bpy.context.scene.collection.objects.link(realised)
|
|
166
|
+
bpy.context.view_layer.update()
|
|
167
|
+
print("realised", gn.realize_to_mesh(realised))
|
|
168
|
+
export_selected(os.path.join(out, "scatter_realised.glb"), [realised])
|
|
169
|
+
bpy.data.objects.remove(realised)
|
|
170
|
+
|
|
171
|
+
# Kept as instances: linked duplicates under one parent -> EXT_mesh_gpu_instancing.
|
|
172
|
+
shipped = gn.instances_to_objects(ground, modifier.name, parent_name="Rocks")
|
|
173
|
+
root = bpy.data.objects["Rocks"]
|
|
174
|
+
export_selected(os.path.join(out, "scatter_instanced.glb"), [ground, root, *root.children],
|
|
175
|
+
export_gpu_instances=True)
|
|
176
|
+
print("instanced", shipped)
|
|
177
|
+
bpy.ops.wm.save_as_mainfile(filepath=os.path.join(out, "scatter.blend"))
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
Measured: 136 instances. The realised GLB is 726 KB, one node and one mesh. The instanced GLB is 38 KB: the
|
|
181
|
+
ground, an empty `Rocks` and one node per variant carrying `EXT_mesh_gpu_instancing` with that variant's
|
|
182
|
+
transforms. Both have 0 validator errors. Then run `validate-gltf` on each file.
|
|
183
|
+
|
|
184
|
+
---
|
|
185
|
+
|
|
186
|
+
## 4. Curves: roads, rails, kerbs and cables
|
|
187
|
+
|
|
188
|
+
`sweep_group` sweeps a profile curve along the modified path:
|
|
189
|
+
- it resamples the path by length (`Segment Length`), so ring spacing is even whatever the curve resolution;
|
|
190
|
+
- it orients the profile by world Z (`Up Is Z`) or by minimum twist;
|
|
191
|
+
- it writes `UVMap` in metres: U is the distance along the profile, V the distance along the path, both
|
|
192
|
+
divided by `Texture Length`.
|
|
193
|
+
|
|
194
|
+
| Profile orientation | Use for | Why |
|
|
195
|
+
|---|---|---|
|
|
196
|
+
| **Z Up** | Roads, track surfaces, kerbs, walls, fences | Keeps the cross-section level on a curving, climbing path; bank with the curve's tilt, not with the frame |
|
|
197
|
+
| **Minimum Twist** | Cables, pipes, tubes, ropes, rails that loop | Follows the path smoothly through vertical sections, where Z Up flips |
|
|
198
|
+
|
|
199
|
+
Lay out a road's UVs from the stored lengths, never from the 0..1 curve parameter. A parameter stretches the
|
|
200
|
+
texture wherever control points bunch up. Metres give every swept piece the same texel density: a 6.6 m road
|
|
201
|
+
profile and a 0.3 m kerb share one trim sheet. Measured on a 3-point Bezier: 368 vertices, U 0–1.011 across
|
|
202
|
+
the 6.6 m profile, V 0–6.92 along the path at 6.6 m per tile.
|
|
203
|
+
|
|
204
|
+
**Closed loops.** Curve to Mesh on a cyclic path closes the mesh, so V jumps from the full length back to 0
|
|
205
|
+
in the last ring of faces and the texture streaks. Open the loop at one point, sweep it, then weld the two end
|
|
206
|
+
rings by position: the vertices join, and the UVs keep their seam. `procedural_generators.road_ribbon` does
|
|
207
|
+
exactly this for a racing line (see `procedural-generation.md`).
|
|
208
|
+
|
|
209
|
+
`curve_array_group` places one instance every `Spacing` metres (Curve to Points in LENGTH mode), offsets it
|
|
210
|
+
sideways along the curve normal (`Offset`: one side of the road), keeps it upright (`Up Is Z`) and turns its -Y
|
|
211
|
+
along the curve. Two such modifiers make both roadsides. A straight two-point curve makes a linear array.
|
|
212
|
+
|
|
213
|
+
### Recipe: a road with posts along one side
|
|
214
|
+
|
|
215
|
+
```python
|
|
216
|
+
# recipe: gn-road
|
|
217
|
+
import os, sys
|
|
218
|
+
import bmesh, bpy
|
|
219
|
+
import construction_kit as kit
|
|
220
|
+
import geometry_nodes_builders as gn
|
|
221
|
+
|
|
222
|
+
out = sys.argv[sys.argv.index("--") + 1]
|
|
223
|
+
kit.clear_default_scene()
|
|
224
|
+
|
|
225
|
+
def curve_object(name, points, kind="BEZIER", dimensions="3D"):
|
|
226
|
+
data = bpy.data.curves.new(name, "CURVE")
|
|
227
|
+
data.dimensions = dimensions
|
|
228
|
+
spline = data.splines.new(kind)
|
|
229
|
+
if kind == "BEZIER":
|
|
230
|
+
spline.bezier_points.add(len(points) - 1)
|
|
231
|
+
for point, location in zip(spline.bezier_points, points):
|
|
232
|
+
point.co = location
|
|
233
|
+
point.handle_left_type = point.handle_right_type = "AUTO"
|
|
234
|
+
else:
|
|
235
|
+
spline.points.add(len(points) - 1)
|
|
236
|
+
for point, location in zip(spline.points, points):
|
|
237
|
+
point.co = (*location, 1.0)
|
|
238
|
+
obj = bpy.data.objects.new(name, data)
|
|
239
|
+
bpy.context.scene.collection.objects.link(obj)
|
|
240
|
+
return obj
|
|
241
|
+
|
|
242
|
+
path = curve_object("RoadPath", [(0, 0, 0), (20, 10, 2), (40, 0, 0)])
|
|
243
|
+
# Profile in its own XY plane: X across the road, Y up. Raised 15 cm shoulders.
|
|
244
|
+
profile = curve_object("RoadProfile", [(-3.3, 0.15, 0), (-3.0, 0, 0), (3.0, 0, 0), (3.3, 0.15, 0)],
|
|
245
|
+
kind="POLY", dimensions="2D")
|
|
246
|
+
sweep = gn.add_modifier(path, gn.sweep_group())
|
|
247
|
+
gn.set_input(sweep, "Profile", profile)
|
|
248
|
+
gn.set_input(sweep, "Segment Length", 0.5)
|
|
249
|
+
gn.set_input(sweep, "Texture Length", 6.6)
|
|
250
|
+
gn.set_input(sweep, "Up Is Z", True)
|
|
251
|
+
|
|
252
|
+
edit_mesh = bmesh.new()
|
|
253
|
+
bmesh.ops.create_cube(edit_mesh, size=1.0)
|
|
254
|
+
bmesh.ops.scale(edit_mesh, vec=(0.12, 0.12, 1.0), verts=edit_mesh.verts)
|
|
255
|
+
bmesh.ops.translate(edit_mesh, vec=(0, 0, 0.5), verts=edit_mesh.verts) # origin at the post's foot
|
|
256
|
+
post = bpy.data.objects.new("Post", bpy.data.meshes.new("Post"))
|
|
257
|
+
edit_mesh.to_mesh(post.data)
|
|
258
|
+
bpy.context.scene.collection.objects.link(post)
|
|
259
|
+
post.hide_render = True
|
|
260
|
+
post.hide_set(True)
|
|
261
|
+
|
|
262
|
+
posts_path = bpy.data.objects.new("PostLine", path.data) # shares the road's curve data
|
|
263
|
+
bpy.context.scene.collection.objects.link(posts_path)
|
|
264
|
+
array = gn.add_modifier(posts_path, gn.curve_array_group())
|
|
265
|
+
gn.set_input(array, "Instance", post)
|
|
266
|
+
gn.set_input(array, "Spacing", 2.5)
|
|
267
|
+
gn.set_input(array, "Offset", 3.6)
|
|
268
|
+
bpy.context.view_layer.update()
|
|
269
|
+
for modifier in (sweep, array):
|
|
270
|
+
assert not any(gn.node_warnings(modifier)), gn.node_warnings(modifier)
|
|
271
|
+
|
|
272
|
+
depsgraph = bpy.context.evaluated_depsgraph_get()
|
|
273
|
+
road_mesh = path.evaluated_get(depsgraph).to_mesh()
|
|
274
|
+
uvs = [loop.uv for loop in road_mesh.uv_layers["UVMap"].data]
|
|
275
|
+
print("road", len(road_mesh.vertices), "vertices; U", round(max(uv.x for uv in uvs), 3),
|
|
276
|
+
"V", round(max(uv.y for uv in uvs), 3), "; posts", gn.instance_count(posts_path))
|
|
277
|
+
path.evaluated_get(depsgraph).to_mesh_clear()
|
|
278
|
+
bpy.ops.wm.save_as_mainfile(filepath=os.path.join(out, "road.blend"))
|
|
279
|
+
|
|
280
|
+
# Ship: the road as its evaluated mesh, the posts as GPU instances of one mesh.
|
|
281
|
+
gn.instances_to_objects(posts_path, array.name, parent_name="Posts")
|
|
282
|
+
bpy.data.objects.remove(posts_path)
|
|
283
|
+
root = bpy.data.objects["Posts"]
|
|
284
|
+
for obj in bpy.context.view_layer.objects:
|
|
285
|
+
obj.select_set(obj in (path, root) or obj.parent == root)
|
|
286
|
+
assert "FINISHED" in bpy.ops.export_scene.gltf(filepath=os.path.join(out, "road.glb"), export_format="GLB",
|
|
287
|
+
use_selection=True, export_apply=True,
|
|
288
|
+
export_gpu_instances=True)
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
Measured: 368 vertices, U to 1.011, V to 6.924, 19 posts. In the GLB the road is 546 triangles with its metre
|
|
292
|
+
UVs, and the posts are one `EXT_mesh_gpu_instancing` node. A curve object's Geometry Nodes result exports
|
|
293
|
+
through `export_apply`; to edit or bake it as a mesh first, convert it with
|
|
294
|
+
`bpy.ops.object.convert(target="MESH")` (context override, §6). The skill's `export` tool leaves out objects
|
|
295
|
+
that evaluate to no mesh (a 2D profile curve, an empty curve) and lists them under `skipped_objects`; a curve
|
|
296
|
+
whose nodes make only instances (the post line) needs `--realize` or `--gpu-instances` like any instancer.
|
|
297
|
+
|
|
298
|
+
---
|
|
299
|
+
|
|
300
|
+
## 5. Arrays and procedural modelling
|
|
301
|
+
|
|
302
|
+
| Need | Build | Notes |
|
|
303
|
+
|---|---|---|
|
|
304
|
+
| N copies in a line or grid | Mesh Line / Grid -> Mesh to Points -> Instance on Points | Spacing in metres; the Array modifier is simpler for a few copies of a mesh you edit by hand |
|
|
305
|
+
| Copies along a path | `curve_array_group` | Even spacing by length, not by parameter |
|
|
306
|
+
| Copies on every face or vertex of a mesh (bolts on a panel, windows on a facade) | Instance on Points with the mesh's points, or Instance on Elements | Align with the face normal; pick variants with a Random Value seeded per face index |
|
|
307
|
+
| A parametric part (a stair, a railing, a pipe run) | One group whose inputs are the measurements: step count, rise, going, width | Same structure as the scatter: knobs on the interface, `set_input` from the build script |
|
|
308
|
+
| Variation per copy | Random Value keyed on `ID` or `Index` plus a per-purpose seed offset | Never a Python loop of separate modifiers |
|
|
309
|
+
|
|
310
|
+
Keep procedural modelling in Geometry Nodes only while the shape is rule-driven and will be regenerated. For a
|
|
311
|
+
one-off hero piece, a modelled mesh (bmesh, construction kit) is easier to review and to fix.
|
|
312
|
+
|
|
313
|
+
---
|
|
314
|
+
|
|
315
|
+
## 6. Realise or keep instances: what each engine receives
|
|
316
|
+
|
|
317
|
+
**The decision**
|
|
318
|
+
|
|
319
|
+
| Situation | Ship as | Why |
|
|
320
|
+
|---|---|---|
|
|
321
|
+
| Few copies (< ~10) of a small mesh, or every copy will be edited | **Realised** into the parent mesh | One draw call, no instancing overhead; the file grows by vertices x copies |
|
|
322
|
+
| Many copies (tens to thousands) of a few variants, static | **Instances**: linked duplicates under one parent, exported with `export_gpu_instances=True` | One draw call per variant via thin instances in BabylonJS; the file stores each variant once plus 40 bytes of transform per copy |
|
|
323
|
+
| Copies that must cull individually (spread over a whole level), or that move, animate or get picked one by one | **Instances, chunked**: one parent per area (a grid tile, a track sector) | Thin instances in BabylonJS share one bounding box, so the set is culled all or nothing; collisions also use that one box |
|
|
324
|
+
| Copies that need per-copy physics, scripts or Unity components | **Separate objects** (linked duplicates, no GPU instancing) or Unity prefab instances | Each becomes a node with its own transform; the Toolkit exports Unity mesh instances as BabylonJS `InstancedMesh` |
|
|
325
|
+
| A lightmapped static set | **Realised**, or separate objects with their own lightmap UVs | Lightmapped copies cannot share a mesh: each needs its own lightmap UV region (the Toolkit's mesh-instance export is off for lightmapped renderers) |
|
|
326
|
+
|
|
327
|
+
**What happens on export (measured on 5.3.0)**
|
|
328
|
+
|
|
329
|
+
| Route | glTF result | Notes |
|
|
330
|
+
|---|---|---|
|
|
331
|
+
| A Geometry Nodes object exported as it is (default exporter options) | **The instances are dropped**: only the realised mesh component (the ground) is written | No warning from the exporter, and a size round trip passes because the instances lie inside the ground's bounds. The skill's `export` tool fails such an object unless `--realize` (one mesh) or `--gpu-instances` (`instances_to_objects`, below) is passed |
|
|
332
|
+
| `export_gn_mesh=True` | One node per instance, all sharing the variant meshes: 108 rocks -> 110 nodes, 34 KB | Experimental option. Without `use_selection`, the variant source objects were exported as well, as a stray copy at their own location |
|
|
333
|
+
| `export_gn_mesh=True, export_gpu_instances=True` | One `EXT_mesh_gpu_instancing` node per variant, 16 KB | Works directly on Geometry Nodes instances, but still experimental |
|
|
334
|
+
| `instances_to_objects` -> linked duplicates under one parent, `export_gpu_instances=True` | One `EXT_mesh_gpu_instancing` node per variant (the parent's children sharing a mesh are merged) | **Recommended.** Stable, inspectable, and the same objects export to FBX as linked duplicates (Unity: one mesh asset, many renderers) |
|
|
335
|
+
| `realize_to_mesh` -> export | One node, one mesh | 726 KB for 136 rocks vs 38 KB instanced |
|
|
336
|
+
|
|
337
|
+
The exporter merges objects into one `EXT_mesh_gpu_instancing` node when they share a mesh, have the same
|
|
338
|
+
parent, have no children and no per-object material differences (Blender manual, glTF add-on, GPU
|
|
339
|
+
Instances). Blender writes the extension in `extensionsUsed` only, not `extensionsRequired`. A loader that
|
|
340
|
+
does not support it draws a single copy at the node's transform.
|
|
341
|
+
|
|
342
|
+
**What BabylonJS does with each** (glTF loader, `EXT_mesh_gpu_instancing`): the node's mesh becomes a thin
|
|
343
|
+
instance source with `thinInstanceSetBuffer("matrix", …)`, one draw call for all copies. Per-instance
|
|
344
|
+
`_COLOR_0` is supported. Without the extension, nodes that share a mesh load as `InstancedMesh` objects: one draw
|
|
345
|
+
call per material via hardware instancing, but a node, a transform and culling for each. A realised mesh is
|
|
346
|
+
one ordinary mesh.
|
|
347
|
+
|
|
348
|
+
**Applying a stack that makes instances.** On 5.3.0, these keep only the mesh component and drop every
|
|
349
|
+
instance silently:
|
|
350
|
+
- `bpy.ops.object.modifier_apply`;
|
|
351
|
+
- `bpy.data.meshes.new_from_object`.
|
|
352
|
+
|
|
353
|
+
`construction_kit.apply_modifiers` checks for instances first and hands such an object to `realize_to_mesh`.
|
|
354
|
+
|
|
355
|
+
A Realize modifier further down the stack does not help `modifier_apply`, because that operator applies one
|
|
356
|
+
modifier at a time. `realize_to_mesh` appends a Realize Instances modifier and evaluates the whole stack once
|
|
357
|
+
(measured: 1,417 vertices with the instances, against 121 for the ground alone).
|
|
358
|
+
|
|
359
|
+
---
|
|
360
|
+
|
|
361
|
+
## 7. Performance
|
|
362
|
+
|
|
363
|
+
- **Evaluate as instances, realise once at the end** (section 1: 0.004 s vs 0.15 s for 50,000 copies).
|
|
364
|
+
Rotate, scale and translate instances with the Instances nodes rather than realising to edit vertices.
|
|
365
|
+
- **Use Selection inputs** instead of separating geometry and joining it back.
|
|
366
|
+
- **Capture Attribute** a field you read after the geometry changes (a normal before a realise, a factor
|
|
367
|
+
before a resample) instead of evaluating it twice.
|
|
368
|
+
- **Distribute on the simplest surface that gives the right placement**: a decimated copy of the terrain gives
|
|
369
|
+
the same placement for far less work. Raycast to the detailed surface only when the height must be exact.
|
|
370
|
+
- **Booleans, proximity and raycasts scale with input size.** Run them on low-poly proxies.
|
|
371
|
+
- **Bake heavy results** (the modifier's bake, or `realize_to_mesh` on a copy) when a later step only reads
|
|
372
|
+
them.
|
|
373
|
+
|
|
374
|
+
---
|
|
375
|
+
|
|
376
|
+
## 8. Budgets for the web
|
|
377
|
+
|
|
378
|
+
| Item | Budget | Reason |
|
|
379
|
+
|---|---|---|
|
|
380
|
+
| Variants per scatter kind | 3–5 | Each variant is one draw call per material per instanced set |
|
|
381
|
+
| Triangles per scattered variant | 50–500 for ground clutter, up to ~2,000 for hero rocks | Triangles x copies is what the GPU draws; 136 copies x 20–80 triangles costs less than one hero prop |
|
|
382
|
+
| Copies per thin-instance set | Thousands are fine to draw; split by area so each set culls | One shared bounding box per set |
|
|
383
|
+
| Materials per variant | 1 | Every extra material is another draw call for the whole set |
|
|
384
|
+
| Realised scatter mesh | One mesh per area, not per level | A realised mesh culls as one box, like a thin-instance set |
|
|
385
|
+
|
|
386
|
+
---
|
|
387
|
+
|
|
388
|
+
## 9. Pitfalls
|
|
389
|
+
|
|
390
|
+
| Symptom | Cause | Fix |
|
|
391
|
+
|---|---|---|
|
|
392
|
+
| `link()` raises "no enabled socket" | Node property (data type, mode) set after linking, or a 4.x socket index used | Set properties first, link by name |
|
|
393
|
+
| Modifier input change has no effect | Written to `modifier["Socket_n"]` (raises on 5.3) or to the node default instead of the modifier | `set_input`, then `view_layer.update()` before measuring |
|
|
394
|
+
| Scatter count changes between runs | The ground mesh changed (topology, modifier order) or the seed was not set | Freeze the surface; record the seed; compare `instance_transforms` |
|
|
395
|
+
| Instances missing from the GLB | Default export (and `modifier_apply`, `new_from_object`) drops Geometry Nodes instances | `realize_to_mesh`, or `instances_to_objects` plus `export_gpu_instances=True` |
|
|
396
|
+
| Variant source objects appear at the origin in the file | Exported without `use_selection`, so the variant collection came too | Export with `use_selection=True`, or keep variants in a collection the export ignores |
|
|
397
|
+
| New objects not exported or not selectable | `view_layer.objects` updates lazily; objects made since the last update are not in it | `bpy.context.view_layer.update()` before selecting (`instances_to_objects` does this) |
|
|
398
|
+
| Instances render inside out in BabylonJS | Negative scale in the scatter (mirrored instances) | Keep scale positive; mirror by a second variant mesh |
|
|
399
|
+
| Whole scatter pops in and out | One thin-instance set spans the level | Chunk per area: one parent per tile |
|
|
400
|
+
| Road texture stretches in places | UVs from the curve parameter | Metre UVs from the stored lengths (`sweep_group`) |
|
|
401
|
+
| Road flips or twists on a climb | Minimum Twist frame on a road | `Up Is Z`; bank with tilt |
|
|
402
|
+
| Posts face sideways along a curve | Curve point rotation used directly (Z = tangent) | Apply the curve-frame-to-asset Euler (`curve_array_group` does) |
|
|
403
|
+
|
|
404
|
+
---
|
|
405
|
+
|
|
406
|
+
## 10. Verification checklist
|
|
407
|
+
|
|
408
|
+
1. `node_warnings(modifier)` is empty for every modifier.
|
|
409
|
+
2. `instance_count` is within expected ± 4·√expected (density x ∫mask dA), and `instance_transforms` matches
|
|
410
|
+
a second run with the same seed and differs with another.
|
|
411
|
+
3. No instance lies where its mask is 0 (check positions against the mask's boundary).
|
|
412
|
+
4. Swept meshes: UV ranges in metres as designed (U across = profile length / Texture Length).
|
|
413
|
+
5. The shipping route is chosen from the section 6 table and the export summary agrees: triangle count = surface
|
|
414
|
+
+ copies x variant triangles; `extensions_used` lists `EXT_mesh_gpu_instancing` when instanced.
|
|
415
|
+
6. `validate-gltf` reports 0 errors on every file; `qa-check --model` on the realised result (floating parts,
|
|
416
|
+
z-fighting where instances overlap each other).
|
|
417
|
+
7. A look in the engine through the game camera: density reads right at play distance, and no copies are
|
|
418
|
+
visibly identical side by side.
|
|
419
|
+
|
|
420
|
+
---
|
|
421
|
+
|
|
422
|
+
## Sources
|
|
423
|
+
|
|
424
|
+
- Blender Manual, Geometry Nodes: Distribute Points on Faces, Instance on Points, Realize Instances,
|
|
425
|
+
Instances, Curve to Mesh, Fields, Random Value, Performance —
|
|
426
|
+
https://docs.blender.org/manual/en/latest/modeling/geometry_nodes/point/distribute_points_on_faces.html ,
|
|
427
|
+
https://docs.blender.org/manual/en/latest/modeling/geometry_nodes/instances/instance_on_points.html ,
|
|
428
|
+
https://docs.blender.org/manual/en/latest/modeling/geometry_nodes/instances/realize_instances.html ,
|
|
429
|
+
https://docs.blender.org/manual/en/latest/modeling/geometry_nodes/instances.html ,
|
|
430
|
+
https://docs.blender.org/manual/en/latest/modeling/geometry_nodes/curve/operations/curve_to_mesh.html ,
|
|
431
|
+
https://docs.blender.org/manual/en/latest/modeling/geometry_nodes/fields.html ,
|
|
432
|
+
https://docs.blender.org/manual/en/latest/modeling/geometry_nodes/utilities/random_value.html ,
|
|
433
|
+
https://docs.blender.org/manual/en/latest/modeling/geometry_nodes/performance.html
|
|
434
|
+
- Blender Manual, glTF 2.0 add-on (GPU Instances, Geometry Nodes Instances, Apply Modifiers) —
|
|
435
|
+
https://docs.blender.org/manual/en/latest/addons/import_export/scene_gltf2.html
|
|
436
|
+
- Khronos, `EXT_mesh_gpu_instancing` specification —
|
|
437
|
+
https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Vendor/EXT_mesh_gpu_instancing/README.md
|
|
438
|
+
- Babylon.js glTF loader, `EXT_mesh_gpu_instancing` (thin instance buffers, `_COLOR_0`) —
|
|
439
|
+
https://github.com/BabylonJS/Babylon.js/blob/master/packages/dev/loaders/src/glTF/2.0/Extensions/EXT_mesh_gpu_instancing.ts
|
|
440
|
+
- Babylon.js documentation, Thin Instances (shared bounding info, picking, negative determinants) —
|
|
441
|
+
https://doc.babylonjs.com/features/featuresDeepDive/mesh/copies/thinInstances
|
|
442
|
+
- Poliigon, "Environment Scattering with Geometry Nodes in Blender" (masks, variation, scale and rotation
|
|
443
|
+
ranges) — https://www.blog.poliigon.com/blog/environment-scattering-with-geometry-nodes-in-blender
|
|
444
|
+
- Blender Studio, "Geometry Nodes from Scratch: Advanced Rock Scattering" (course outline) —
|
|
445
|
+
https://studio.blender.org/training/geometry-nodes-from-scratch/example-advanced-rock-scattering/
|
|
446
|
+
- EL4CTEO/blender-skills (MIT, © 2026 EL4CTEO): the 5.x modifier-input pattern in its scatter and node-builder
|
|
447
|
+
scripts, read and re-verified here; no code taken.
|