@babylonjs-toolkit/agent 1.2.16 → 1.2.21
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 +448 -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 +724 -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 +637 -0
- package/skills/bt-blender/scripts/blender/__pycache__/construction_kit.cpython-313.pyc +0 -0
- package/skills/bt-blender/scripts/blender/__pycache__/geometry_nodes_builders.cpython-313.pyc +0 -0
- package/skills/bt-blender/scripts/blender/__pycache__/image_measure.cpython-313.pyc +0 -0
- package/skills/bt-blender/scripts/blender/__pycache__/reference_traces.cpython-313.pyc +0 -0
- package/skills/bt-blender/scripts/blender/assembly_check.py +420 -0
- package/skills/bt-blender/scripts/blender/blueprint.py +293 -0
- package/skills/bt-blender/scripts/blender/character_blockout.py +462 -0
- package/skills/bt-blender/scripts/blender/construction_kit.py +1770 -0
- package/skills/bt-blender/scripts/blender/coverage_check.py +221 -0
- package/skills/bt-blender/scripts/blender/environment_kit.py +525 -0
- package/skills/bt-blender/scripts/blender/evidence_sheet.py +243 -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 +917 -0
- package/skills/bt-blender/scripts/blender/materials_pbr.py +439 -0
- package/skills/bt-blender/scripts/blender/measure_stations.py +496 -0
- package/skills/bt-blender/scripts/blender/ortho_views.py +309 -0
- package/skills/bt-blender/scripts/blender/overlay_sheet.py +238 -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/reference_traces.py +344 -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/trace.py +283 -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 +231 -0
- package/skills/bt-blender/scripts/bt-blender.mjs +333 -0
- package/skills/bt-blender/scripts/validate_gltf.mjs +204 -0
- package/skills/bt-execute/SKILL.md +2 -1
- package/skills/bt-gauntlet/SKILL.md +30 -20
- package/skills/bt-gauntlet/references/critic-rubrics.md +106 -7
- package/skills/bt-gauntlet/references/loop-templates.md +23 -3
- package/skills/bt-gauntlet/references/spec-composition.md +20 -1
- package/skills/bt-gauntlet/references/target-images.md +27 -0
- package/skills/bt-gauntlet/references/unity-pipeline.md +41 -4
- package/skills/bt-gauntlet/references/workspace.md +90 -6
- 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,575 @@
|
|
|
1
|
+
"""
|
|
2
|
+
bt-blender Geometry Nodes builders: node groups for the procedural work game assets need, built with the
|
|
3
|
+
Blender 5.3 API, and the helpers to drive, measure and ship them headless.
|
|
4
|
+
|
|
5
|
+
import geometry_nodes_builders as gn
|
|
6
|
+
|
|
7
|
+
tree = gn.scatter_group()
|
|
8
|
+
modifier = gn.add_modifier(ground, tree)
|
|
9
|
+
gn.set_input(modifier, "Instances", rock_collection)
|
|
10
|
+
gn.set_input(modifier, "Seed", 7)
|
|
11
|
+
gn.set_input_attribute(modifier, "Density Mask", "scatter_mask") # a vertex group or float attribute
|
|
12
|
+
print(gn.instance_count(ground))
|
|
13
|
+
|
|
14
|
+
What is in it:
|
|
15
|
+
- building trees: new_geometry_group (interface sockets through `interface.new_socket`), add_node, link,
|
|
16
|
+
input_identifiers;
|
|
17
|
+
- driving modifiers on 5.3: add_modifier, set_input (`modifier.properties.inputs.<identifier>.value`; the old
|
|
18
|
+
`modifier["Socket_1"]` raises TypeError), set_input_attribute, node_warnings;
|
|
19
|
+
- ready groups: scatter_group (density per square metre x a 0..1 mask field, seeded, random pick from a
|
|
20
|
+
collection, align to normal, random yaw and scale), sweep_group (a profile swept along a path with metre-based
|
|
21
|
+
UVs: roads, rails, kerbs, cables), curve_array_group (instances every N metres along a curve: posts, sleepers,
|
|
22
|
+
lamps);
|
|
23
|
+
- measuring: instance_count, instance_transforms (deterministic, sorted);
|
|
24
|
+
- shipping: realize_group, realize_to_mesh (apply with every instance realised; a plain apply drops them), instances_to_objects (linked duplicates
|
|
25
|
+
under one parent: what the glTF exporter turns into EXT_mesh_gpu_instancing and Unity into shared meshes).
|
|
26
|
+
|
|
27
|
+
Every node and socket name used here was introspected on Blender 5.3.0. Node display names are stable
|
|
28
|
+
identifiers inside a tree only in English; the builders link by socket name on nodes they created themselves.
|
|
29
|
+
"""
|
|
30
|
+
import math
|
|
31
|
+
|
|
32
|
+
import bpy
|
|
33
|
+
from mathutils import Matrix
|
|
34
|
+
|
|
35
|
+
# Interface socket types by short name.
|
|
36
|
+
SOCKET_TYPES = {
|
|
37
|
+
"GEOMETRY": "NodeSocketGeometry", "FLOAT": "NodeSocketFloat", "INT": "NodeSocketInt",
|
|
38
|
+
"BOOL": "NodeSocketBool", "VECTOR": "NodeSocketVector", "OBJECT": "NodeSocketObject",
|
|
39
|
+
"COLLECTION": "NodeSocketCollection", "MATERIAL": "NodeSocketMaterial", "ROTATION": "NodeSocketRotation",
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
# Gap between node columns when a builder lays out its tree (pure readability for people who open the file).
|
|
43
|
+
NODE_COLUMN_WIDTH = 220
|
|
44
|
+
|
|
45
|
+
# A full turn in radians, the upper bound of a random yaw.
|
|
46
|
+
FULL_TURN = 2.0 * math.pi
|
|
47
|
+
|
|
48
|
+
# Seed offsets so the scale, yaw and pick streams of one scatter are independent of each other.
|
|
49
|
+
SCALE_SEED_OFFSET = 1
|
|
50
|
+
YAW_SEED_OFFSET = 2
|
|
51
|
+
PICK_SEED_OFFSET = 3
|
|
52
|
+
|
|
53
|
+
# Instance indices are drawn from [0, this) and wrapped by Instance on Points onto the collection size.
|
|
54
|
+
PICK_INDEX_RANGE = 1 << 20
|
|
55
|
+
|
|
56
|
+
# XYZ Euler (radians) that turns Curve to Points' frame (Z = tangent) into the asset convention: -Y forward
|
|
57
|
+
# along the curve, +Z up. Derived from the measured frame and checked by re-measuring the instances.
|
|
58
|
+
CURVE_FRAME_TO_ASSET_EULER = (-math.pi / 2.0, 0.0, math.pi)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
# --- building trees ------------------------------------------------------------------------------------------
|
|
62
|
+
|
|
63
|
+
def new_geometry_group(name: str, inputs: list, outputs=(("Geometry", "GEOMETRY"),)) -> bpy.types.GeometryNodeTree:
|
|
64
|
+
"""
|
|
65
|
+
Creates (or clears and rebuilds) a Geometry Nodes group with its interface, ready to use as a modifier.
|
|
66
|
+
|
|
67
|
+
:param name: Node group name. An existing group of that name is rebuilt in place, so a re-run of a build
|
|
68
|
+
script never makes `.001` copies.
|
|
69
|
+
:param inputs: [{"name", "type", "default"?, "min"?, "max"?, "field"?, "description"?}]; `type` is a key of
|
|
70
|
+
SOCKET_TYPES; `field` True lets the input take a per-element attribute from the modifier.
|
|
71
|
+
:param outputs: [(name, type)].
|
|
72
|
+
:returns: The group, with a Group Input and a Group Output node.
|
|
73
|
+
"""
|
|
74
|
+
tree = bpy.data.node_groups.get(name) or bpy.data.node_groups.new(name, "GeometryNodeTree")
|
|
75
|
+
tree.nodes.clear()
|
|
76
|
+
tree.interface.clear()
|
|
77
|
+
tree.is_modifier = True
|
|
78
|
+
for spec in inputs:
|
|
79
|
+
socket = tree.interface.new_socket(spec["name"], in_out="INPUT", socket_type=SOCKET_TYPES[spec["type"]])
|
|
80
|
+
if "default" in spec:
|
|
81
|
+
socket.default_value = spec["default"]
|
|
82
|
+
if "min" in spec:
|
|
83
|
+
socket.min_value = spec["min"]
|
|
84
|
+
if "max" in spec:
|
|
85
|
+
socket.max_value = spec["max"]
|
|
86
|
+
if spec.get("field"):
|
|
87
|
+
socket.structure_type = "FIELD"
|
|
88
|
+
if spec.get("description"):
|
|
89
|
+
socket.description = spec["description"]
|
|
90
|
+
for output_name, output_type in outputs:
|
|
91
|
+
tree.interface.new_socket(output_name, in_out="OUTPUT", socket_type=SOCKET_TYPES[output_type])
|
|
92
|
+
tree.nodes.new("NodeGroupInput").name = "Group Input"
|
|
93
|
+
tree.nodes.new("NodeGroupOutput").name = "Group Output"
|
|
94
|
+
return tree
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def add_node(tree: bpy.types.NodeTree, node_type: str, column: int = 0, row: int = 0, **properties):
|
|
98
|
+
"""
|
|
99
|
+
Adds a node and sets its properties (data_type, mode, operation, ...) before anything links to it, since
|
|
100
|
+
those properties decide which sockets exist.
|
|
101
|
+
|
|
102
|
+
:param tree: The node tree.
|
|
103
|
+
:param node_type: bl_idname, e.g. "GeometryNodeDistributePointsOnFaces".
|
|
104
|
+
:param column: Layout column (readability only).
|
|
105
|
+
:param row: Layout row (readability only).
|
|
106
|
+
:param properties: Node properties to set.
|
|
107
|
+
:returns: The node.
|
|
108
|
+
"""
|
|
109
|
+
node = tree.nodes.new(node_type)
|
|
110
|
+
for key, value in properties.items():
|
|
111
|
+
setattr(node, key, value)
|
|
112
|
+
node.location = (column * NODE_COLUMN_WIDTH, -row * NODE_COLUMN_WIDTH * 0.8)
|
|
113
|
+
return node
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def _socket(sockets, name):
|
|
117
|
+
"""The enabled socket called `name` (disabled sockets of other modes share names), or an index."""
|
|
118
|
+
if isinstance(name, int):
|
|
119
|
+
return sockets[name]
|
|
120
|
+
for socket in sockets:
|
|
121
|
+
if socket.name == name and socket.enabled:
|
|
122
|
+
return socket
|
|
123
|
+
raise KeyError(f"no enabled socket '{name}'; have {[socket.name for socket in sockets if socket.enabled]}")
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def link(tree: bpy.types.NodeTree, from_node, from_socket, to_node, to_socket) -> None:
|
|
127
|
+
"""
|
|
128
|
+
Links an output to an input by socket name (or index).
|
|
129
|
+
|
|
130
|
+
:raises KeyError: when either socket does not exist or is disabled.
|
|
131
|
+
"""
|
|
132
|
+
tree.links.new(_socket(from_node.outputs, from_socket), _socket(to_node.inputs, to_socket))
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def group_nodes(tree: bpy.types.NodeTree) -> tuple:
|
|
136
|
+
"""The (Group Input, Group Output) nodes of a group built by new_geometry_group."""
|
|
137
|
+
return tree.nodes["Group Input"], tree.nodes["Group Output"]
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def input_identifiers(tree: bpy.types.NodeTree) -> dict:
|
|
141
|
+
"""{interface input name: socket identifier}; the identifier is what the modifier stores values under."""
|
|
142
|
+
return {item.name: item.identifier for item in tree.interface.items_tree
|
|
143
|
+
if item.item_type == "SOCKET" and item.in_out == "INPUT"}
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
# --- driving modifiers ---------------------------------------------------------------------------------------
|
|
147
|
+
|
|
148
|
+
def add_modifier(obj: bpy.types.Object, tree: bpy.types.GeometryNodeTree, name=None) -> bpy.types.NodesModifier:
|
|
149
|
+
"""
|
|
150
|
+
Adds a Geometry Nodes modifier running the group (or reuses the one of that name).
|
|
151
|
+
|
|
152
|
+
:param obj: Mesh or curve object.
|
|
153
|
+
:param tree: The node group.
|
|
154
|
+
:param name: Modifier name (default the group's name).
|
|
155
|
+
:returns: The modifier.
|
|
156
|
+
"""
|
|
157
|
+
modifier_name = name or tree.name
|
|
158
|
+
modifier = obj.modifiers.get(modifier_name) or obj.modifiers.new(modifier_name, "NODES")
|
|
159
|
+
modifier.node_group = tree
|
|
160
|
+
return modifier
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def _input_item(modifier: bpy.types.NodesModifier, name: str):
|
|
164
|
+
"""The 5.3 modifier input item for an interface input name."""
|
|
165
|
+
identifiers = input_identifiers(modifier.node_group)
|
|
166
|
+
if name not in identifiers:
|
|
167
|
+
raise KeyError(f"'{modifier.node_group.name}' has no input '{name}'; inputs: {sorted(identifiers)}")
|
|
168
|
+
return getattr(modifier.properties.inputs, identifiers[name])
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def set_input(modifier: bpy.types.NodesModifier, name: str, value) -> None:
|
|
172
|
+
"""
|
|
173
|
+
Sets a modifier input to a value (5.3: `modifier.properties.inputs.<identifier>.value`).
|
|
174
|
+
|
|
175
|
+
:param modifier: Geometry Nodes modifier.
|
|
176
|
+
:param name: Interface input name.
|
|
177
|
+
:param value: A number, vector, bool, or an ID (object, collection, material).
|
|
178
|
+
"""
|
|
179
|
+
item = _input_item(modifier, name)
|
|
180
|
+
if hasattr(item, "type") and item.type == "ATTRIBUTE":
|
|
181
|
+
item.type = "VALUE"
|
|
182
|
+
item.value = value
|
|
183
|
+
modifier.id_data.update_tag()
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def set_input_attribute(modifier: bpy.types.NodesModifier, name: str, attribute_name: str) -> None:
|
|
187
|
+
"""
|
|
188
|
+
Feeds a field input from a named attribute of the object (a vertex group counts), e.g. a painted density
|
|
189
|
+
mask. The input must have been created with `"field": True`.
|
|
190
|
+
|
|
191
|
+
:param modifier: Geometry Nodes modifier.
|
|
192
|
+
:param name: Interface input name.
|
|
193
|
+
:param attribute_name: Vertex group or attribute on the modified object.
|
|
194
|
+
"""
|
|
195
|
+
item = _input_item(modifier, name)
|
|
196
|
+
item.type = "ATTRIBUTE"
|
|
197
|
+
item.attribute_name = attribute_name
|
|
198
|
+
modifier.id_data.update_tag()
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def node_warnings(modifier: bpy.types.NodesModifier) -> list:
|
|
202
|
+
"""The modifier's node warnings after evaluation (a missing object, a wrong type); empty when clean."""
|
|
203
|
+
bpy.context.evaluated_depsgraph_get()
|
|
204
|
+
return [warning.message for warning in modifier.node_warnings]
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
# --- ready groups -------------------------------------------------------------------------------------------
|
|
208
|
+
|
|
209
|
+
def scatter_group(name: str = "BT_Scatter") -> bpy.types.GeometryNodeTree:
|
|
210
|
+
"""
|
|
211
|
+
Scatters instances from a collection over the faces of the modified mesh.
|
|
212
|
+
|
|
213
|
+
Inputs: Instances (collection; each direct child is one variant, picked at random per point), Density
|
|
214
|
+
(points per square metre), Density Mask (0..1 field: a vertex group or attribute multiplies the density),
|
|
215
|
+
Seed, Scale Min / Scale Max (uniform scale), Align To Normal (0 = upright, 1 = follows the surface),
|
|
216
|
+
Random Yaw (spin about the instance's own up axis), Keep Surface (output the surface too), Realize
|
|
217
|
+
(realise the instances into one mesh).
|
|
218
|
+
|
|
219
|
+
Determinism: the points come from the seed and the mesh; scale, yaw and pick are hashed from each
|
|
220
|
+
point's stable `id`, so the same seed on the same mesh gives the same placement on every run.
|
|
221
|
+
|
|
222
|
+
:param name: Group name.
|
|
223
|
+
:returns: The group.
|
|
224
|
+
"""
|
|
225
|
+
tree = new_geometry_group(name, [
|
|
226
|
+
{"name": "Geometry", "type": "GEOMETRY"},
|
|
227
|
+
{"name": "Instances", "type": "COLLECTION", "description": "One variant per direct child object"},
|
|
228
|
+
{"name": "Density", "type": "FLOAT", "default": 1.0, "min": 0.0, "description": "Points per square metre"},
|
|
229
|
+
{"name": "Density Mask", "type": "FLOAT", "default": 1.0, "min": 0.0, "max": 1.0, "field": True},
|
|
230
|
+
{"name": "Seed", "type": "INT", "default": 0},
|
|
231
|
+
{"name": "Scale Min", "type": "FLOAT", "default": 0.8, "min": 0.001},
|
|
232
|
+
{"name": "Scale Max", "type": "FLOAT", "default": 1.2, "min": 0.001},
|
|
233
|
+
{"name": "Align To Normal", "type": "FLOAT", "default": 0.0, "min": 0.0, "max": 1.0},
|
|
234
|
+
{"name": "Random Yaw", "type": "BOOL", "default": True},
|
|
235
|
+
{"name": "Keep Surface", "type": "BOOL", "default": True},
|
|
236
|
+
{"name": "Realize", "type": "BOOL", "default": False},
|
|
237
|
+
])
|
|
238
|
+
group_input, group_output = group_nodes(tree)
|
|
239
|
+
group_input.location = (-NODE_COLUMN_WIDTH, 0)
|
|
240
|
+
|
|
241
|
+
density = add_node(tree, "ShaderNodeMath", 0, 1, operation="MULTIPLY")
|
|
242
|
+
link(tree, group_input, "Density", density, 0)
|
|
243
|
+
link(tree, group_input, "Density Mask", density, 1)
|
|
244
|
+
distribute = add_node(tree, "GeometryNodeDistributePointsOnFaces", 1, 0, distribute_method="RANDOM")
|
|
245
|
+
link(tree, group_input, "Geometry", distribute, "Mesh")
|
|
246
|
+
link(tree, density, "Value", distribute, "Density")
|
|
247
|
+
link(tree, group_input, "Seed", distribute, "Seed")
|
|
248
|
+
|
|
249
|
+
rotation = _scatter_rotation(tree, group_input, distribute)
|
|
250
|
+
scale = _seeded_random(tree, group_input, "FLOAT", SCALE_SEED_OFFSET, 2, 3)
|
|
251
|
+
link(tree, group_input, "Scale Min", scale, "Min")
|
|
252
|
+
link(tree, group_input, "Scale Max", scale, "Max")
|
|
253
|
+
pick = _seeded_random(tree, group_input, "INT", PICK_SEED_OFFSET, 2, 4)
|
|
254
|
+
pick.inputs["Min"].default_value = 0
|
|
255
|
+
pick.inputs["Max"].default_value = PICK_INDEX_RANGE
|
|
256
|
+
|
|
257
|
+
collection = add_node(tree, "GeometryNodeCollectionInfo", 1, 5, transform_space="ORIGINAL")
|
|
258
|
+
link(tree, group_input, "Instances", collection, "Collection")
|
|
259
|
+
collection.inputs["Separate Children"].default_value = True
|
|
260
|
+
collection.inputs["Reset Children"].default_value = True
|
|
261
|
+
|
|
262
|
+
instance = add_node(tree, "GeometryNodeInstanceOnPoints", 4, 0)
|
|
263
|
+
link(tree, distribute, "Points", instance, "Points")
|
|
264
|
+
link(tree, collection, "Instances", instance, "Instance")
|
|
265
|
+
instance.inputs["Pick Instance"].default_value = True
|
|
266
|
+
link(tree, pick, "Value", instance, "Instance Index")
|
|
267
|
+
link(tree, rotation, "Rotation", instance, "Rotation")
|
|
268
|
+
link(tree, scale, "Value", instance, "Scale")
|
|
269
|
+
|
|
270
|
+
_finish_with_surface_and_realize(tree, group_input, group_output, instance, column=5)
|
|
271
|
+
return tree
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
def _seeded_random(tree, group_input, data_type: str, seed_offset: int, column: int, row: int):
|
|
275
|
+
"""A Random Value node hashed from the point id and Seed + seed_offset (an independent stream)."""
|
|
276
|
+
seed = add_node(tree, "ShaderNodeMath", column - 1, row, operation="ADD")
|
|
277
|
+
link(tree, group_input, "Seed", seed, 0)
|
|
278
|
+
seed.inputs[1].default_value = seed_offset
|
|
279
|
+
random_value = add_node(tree, "FunctionNodeRandomValue", column, row, data_type=data_type)
|
|
280
|
+
link(tree, seed, "Value", random_value, "Seed")
|
|
281
|
+
return random_value
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
def _scatter_rotation(tree, group_input, distribute):
|
|
285
|
+
"""Rotation per point: up (Z) blended towards the surface normal, then a random spin about local Z."""
|
|
286
|
+
align = add_node(tree, "FunctionNodeAlignRotationToVector", 2, 1, axis="Z", pivot_axis="AUTO")
|
|
287
|
+
link(tree, group_input, "Align To Normal", align, "Factor")
|
|
288
|
+
link(tree, distribute, "Normal", align, "Vector")
|
|
289
|
+
yaw = _seeded_random(tree, group_input, "FLOAT", YAW_SEED_OFFSET, 2, 2)
|
|
290
|
+
yaw_amount = add_node(tree, "ShaderNodeMath", 3, 2, operation="MULTIPLY")
|
|
291
|
+
link(tree, yaw, "Value", yaw_amount, 0)
|
|
292
|
+
link(tree, group_input, "Random Yaw", yaw_amount, 1)
|
|
293
|
+
yaw.inputs["Max"].default_value = FULL_TURN
|
|
294
|
+
yaw_vector = add_node(tree, "ShaderNodeCombineXYZ", 3, 3)
|
|
295
|
+
link(tree, yaw_amount, "Value", yaw_vector, "Z")
|
|
296
|
+
yaw_rotation = add_node(tree, "FunctionNodeEulerToRotation", 3, 4)
|
|
297
|
+
link(tree, yaw_vector, "Vector", yaw_rotation, "Euler")
|
|
298
|
+
spin = add_node(tree, "FunctionNodeRotateRotation", 4, 1, rotation_space="LOCAL")
|
|
299
|
+
link(tree, align, "Rotation", spin, "Rotation")
|
|
300
|
+
link(tree, yaw_rotation, "Rotation", spin, "Rotate By")
|
|
301
|
+
return spin
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def _finish_with_surface_and_realize(tree, group_input, group_output, instances_node, column: int) -> None:
|
|
305
|
+
"""Realize Instances (selection = the Realize input), joined with the source surface when kept."""
|
|
306
|
+
realize = add_node(tree, "GeometryNodeRealizeInstances", column, 0)
|
|
307
|
+
link(tree, instances_node, 0, realize, "Geometry")
|
|
308
|
+
link(tree, group_input, "Realize", realize, "Selection")
|
|
309
|
+
surface = add_node(tree, "GeometryNodeSwitch", column, 1, input_type="GEOMETRY")
|
|
310
|
+
link(tree, group_input, "Keep Surface", surface, "Switch")
|
|
311
|
+
link(tree, group_input, "Geometry", surface, "True")
|
|
312
|
+
join = add_node(tree, "GeometryNodeJoinGeometry", column + 1, 0)
|
|
313
|
+
link(tree, surface, "Output", join, "Geometry")
|
|
314
|
+
link(tree, realize, "Geometry", join, "Geometry")
|
|
315
|
+
group_output.location = ((column + 2) * NODE_COLUMN_WIDTH, 0)
|
|
316
|
+
link(tree, join, "Geometry", group_output, "Geometry")
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
def sweep_group(name: str = "BT_Sweep") -> bpy.types.GeometryNodeTree:
|
|
320
|
+
"""
|
|
321
|
+
Sweeps a profile curve along the modified path curve into a mesh with UVs in metres: roads, track
|
|
322
|
+
surfaces, kerbs, rails, gutters, cables.
|
|
323
|
+
|
|
324
|
+
Inputs: Profile (a curve object; its local X runs across the path, Y is up), Segment Length (metres
|
|
325
|
+
between rings along the path; resample for even density instead of the curve's own resolution), Texture
|
|
326
|
+
Length (metres of path per V tile), Up Is Z (orient the profile by world Z instead of minimum twist: keeps a
|
|
327
|
+
road level on a curving, climbing path), Fill Caps, Shade Smooth.
|
|
328
|
+
|
|
329
|
+
UVs: `UVMap` (corner domain) with U = the profile's own length in metres divided by Texture Length (so a
|
|
330
|
+
6 m road and a 0.3 m kerb share one texel density), V = distance along the path / Texture Length.
|
|
331
|
+
The path should be open; for a closed loop, open it at one point, sweep, then weld the two end rings
|
|
332
|
+
(positions merge, the UVs keep their seam). Bank the road with the curve's tilt.
|
|
333
|
+
|
|
334
|
+
:param name: Group name.
|
|
335
|
+
:returns: The group.
|
|
336
|
+
"""
|
|
337
|
+
tree = new_geometry_group(name, [
|
|
338
|
+
{"name": "Geometry", "type": "GEOMETRY"},
|
|
339
|
+
{"name": "Profile", "type": "OBJECT"},
|
|
340
|
+
{"name": "Segment Length", "type": "FLOAT", "default": 1.0, "min": 0.01},
|
|
341
|
+
{"name": "Texture Length", "type": "FLOAT", "default": 4.0, "min": 0.01},
|
|
342
|
+
{"name": "Up Is Z", "type": "BOOL", "default": True},
|
|
343
|
+
{"name": "Fill Caps", "type": "BOOL", "default": False},
|
|
344
|
+
{"name": "Shade Smooth", "type": "BOOL", "default": True},
|
|
345
|
+
])
|
|
346
|
+
group_input, group_output = group_nodes(tree)
|
|
347
|
+
group_input.location = (-NODE_COLUMN_WIDTH, 0)
|
|
348
|
+
|
|
349
|
+
resample = add_node(tree, "GeometryNodeResampleCurve", 0, 0)
|
|
350
|
+
resample.inputs["Mode"].default_value = "Length"
|
|
351
|
+
link(tree, group_input, "Geometry", resample, "Curve")
|
|
352
|
+
link(tree, group_input, "Segment Length", resample, "Length")
|
|
353
|
+
normal_z = add_node(tree, "GeometryNodeSetCurveNormal", 1, 0)
|
|
354
|
+
normal_z.inputs["Mode"].default_value = "Z Up"
|
|
355
|
+
choose_normal = add_node(tree, "GeometryNodeSwitch", 2, 0, input_type="GEOMETRY")
|
|
356
|
+
link(tree, resample, "Curve", normal_z, "Curve")
|
|
357
|
+
link(tree, group_input, "Up Is Z", choose_normal, "Switch")
|
|
358
|
+
link(tree, resample, "Curve", choose_normal, "False")
|
|
359
|
+
link(tree, normal_z, "Curve", choose_normal, "True")
|
|
360
|
+
along = _store_spline_length(tree, choose_normal, "Output", "bt_path_length", column=3, row=0)
|
|
361
|
+
|
|
362
|
+
profile_info = add_node(tree, "GeometryNodeObjectInfo", 0, 2, transform_space="ORIGINAL")
|
|
363
|
+
link(tree, group_input, "Profile", profile_info, "Object")
|
|
364
|
+
across = _store_spline_length(tree, profile_info, "Geometry", "bt_profile_length", column=3, row=2)
|
|
365
|
+
|
|
366
|
+
sweep = add_node(tree, "GeometryNodeCurveToMesh", 5, 0)
|
|
367
|
+
link(tree, along, "Geometry", sweep, "Curve")
|
|
368
|
+
link(tree, across, "Geometry", sweep, "Profile Curve")
|
|
369
|
+
link(tree, group_input, "Fill Caps", sweep, "Fill Caps")
|
|
370
|
+
|
|
371
|
+
uv = _store_metre_uvs(tree, group_input, sweep, column=6)
|
|
372
|
+
smooth = add_node(tree, "GeometryNodeSetShadeSmooth", 9, 0)
|
|
373
|
+
link(tree, uv, "Geometry", smooth, "Mesh")
|
|
374
|
+
link(tree, group_input, "Shade Smooth", smooth, "Shade Smooth")
|
|
375
|
+
group_output.location = (10 * NODE_COLUMN_WIDTH, 0)
|
|
376
|
+
link(tree, smooth, 0, group_output, "Geometry")
|
|
377
|
+
return tree
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
def _store_spline_length(tree, source_node, source_socket, attribute: str, column: int, row: int):
|
|
381
|
+
"""Stores the length along each spline (metres from its start) as a point attribute."""
|
|
382
|
+
parameter = add_node(tree, "GeometryNodeSplineParameter", column, row + 1)
|
|
383
|
+
store = add_node(tree, "GeometryNodeStoreNamedAttribute", column + 1, row, data_type="FLOAT", domain="POINT")
|
|
384
|
+
store.inputs["Name"].default_value = attribute
|
|
385
|
+
link(tree, source_node, source_socket, store, "Geometry")
|
|
386
|
+
link(tree, parameter, "Length", store, "Value")
|
|
387
|
+
return store
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
def _store_metre_uvs(tree, group_input, sweep, column: int):
|
|
391
|
+
"""UVMap = (profile length, path length) / Texture Length, on face corners, from the stored attributes."""
|
|
392
|
+
lengths = []
|
|
393
|
+
for row, attribute in enumerate(("bt_profile_length", "bt_path_length")):
|
|
394
|
+
read = add_node(tree, "GeometryNodeInputNamedAttribute", column, row + 1, data_type="FLOAT")
|
|
395
|
+
read.inputs["Name"].default_value = attribute
|
|
396
|
+
scaled = add_node(tree, "ShaderNodeMath", column + 1, row + 1, operation="DIVIDE")
|
|
397
|
+
link(tree, read, "Attribute", scaled, 0)
|
|
398
|
+
link(tree, group_input, "Texture Length", scaled, 1)
|
|
399
|
+
lengths.append(scaled)
|
|
400
|
+
combine = add_node(tree, "ShaderNodeCombineXYZ", column + 2, 1)
|
|
401
|
+
link(tree, lengths[0], "Value", combine, "X")
|
|
402
|
+
link(tree, lengths[1], "Value", combine, "Y")
|
|
403
|
+
store = add_node(tree, "GeometryNodeStoreNamedAttribute", column + 2, 0, data_type="FLOAT2", domain="CORNER")
|
|
404
|
+
store.inputs["Name"].default_value = "UVMap"
|
|
405
|
+
link(tree, sweep, "Mesh", store, "Geometry")
|
|
406
|
+
link(tree, combine, "Vector", store, "Value")
|
|
407
|
+
return store
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
def curve_array_group(name: str = "BT_CurveArray") -> bpy.types.GeometryNodeTree:
|
|
411
|
+
"""
|
|
412
|
+
Places one instance every Spacing metres along the modified curve, facing along it: fence posts, rail
|
|
413
|
+
sleepers, lamp posts, barriers, bollards. A straight two-point curve makes a linear array.
|
|
414
|
+
|
|
415
|
+
Inputs: Instance (object), Spacing (metres), Offset (metres sideways, along the curve normal: one side of a
|
|
416
|
+
road), Up Is Z (keep instances upright on a climbing curve), Keep Curve (also output the curve), Realize.
|
|
417
|
+
The instance's local -Y (the skill's forward) is turned to follow the curve.
|
|
418
|
+
|
|
419
|
+
:param name: Group name.
|
|
420
|
+
:returns: The group.
|
|
421
|
+
"""
|
|
422
|
+
tree = new_geometry_group(name, [
|
|
423
|
+
{"name": "Geometry", "type": "GEOMETRY"},
|
|
424
|
+
{"name": "Instance", "type": "OBJECT"},
|
|
425
|
+
{"name": "Spacing", "type": "FLOAT", "default": 2.0, "min": 0.01},
|
|
426
|
+
{"name": "Offset", "type": "FLOAT", "default": 0.0},
|
|
427
|
+
{"name": "Up Is Z", "type": "BOOL", "default": True},
|
|
428
|
+
{"name": "Keep Surface", "type": "BOOL", "default": False},
|
|
429
|
+
{"name": "Realize", "type": "BOOL", "default": False},
|
|
430
|
+
])
|
|
431
|
+
group_input, group_output = group_nodes(tree)
|
|
432
|
+
group_input.location = (-NODE_COLUMN_WIDTH, 0)
|
|
433
|
+
|
|
434
|
+
normal_z = add_node(tree, "GeometryNodeSetCurveNormal", 0, 1)
|
|
435
|
+
normal_z.inputs["Mode"].default_value = "Z Up"
|
|
436
|
+
link(tree, group_input, "Geometry", normal_z, "Curve")
|
|
437
|
+
choose_normal = add_node(tree, "GeometryNodeSwitch", 1, 0, input_type="GEOMETRY")
|
|
438
|
+
link(tree, group_input, "Up Is Z", choose_normal, "Switch")
|
|
439
|
+
link(tree, group_input, "Geometry", choose_normal, "False")
|
|
440
|
+
link(tree, normal_z, "Curve", choose_normal, "True")
|
|
441
|
+
points = add_node(tree, "GeometryNodeCurveToPoints", 2, 0, mode="LENGTH")
|
|
442
|
+
link(tree, choose_normal, "Output", points, "Curve")
|
|
443
|
+
link(tree, group_input, "Spacing", points, "Length")
|
|
444
|
+
|
|
445
|
+
offset = _sideways_offset(tree, group_input, points, column=3)
|
|
446
|
+
info = add_node(tree, "GeometryNodeObjectInfo", 3, 2, transform_space="ORIGINAL")
|
|
447
|
+
info.inputs["As Instance"].default_value = True
|
|
448
|
+
link(tree, group_input, "Instance", info, "Object")
|
|
449
|
+
# Curve to Points' rotation maps local Z to the tangent and local X to the curve normal (measured on 5.3.0).
|
|
450
|
+
# This local turn maps the asset's -Y forward onto the tangent and its +Z up onto the frame's up instead.
|
|
451
|
+
forward_fix = add_node(tree, "FunctionNodeRotateRotation", 4, 1, rotation_space="LOCAL")
|
|
452
|
+
link(tree, points, "Rotation", forward_fix, "Rotation")
|
|
453
|
+
forward_fix.inputs["Rotate By"].default_value = CURVE_FRAME_TO_ASSET_EULER
|
|
454
|
+
instance = add_node(tree, "GeometryNodeInstanceOnPoints", 5, 0)
|
|
455
|
+
link(tree, offset, "Geometry", instance, "Points")
|
|
456
|
+
link(tree, info, "Geometry", instance, "Instance")
|
|
457
|
+
link(tree, forward_fix, "Rotation", instance, "Rotation")
|
|
458
|
+
_finish_with_surface_and_realize(tree, group_input, group_output, instance, column=6)
|
|
459
|
+
return tree
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
def _sideways_offset(tree, group_input, points, column: int):
|
|
463
|
+
"""Moves the points Offset metres along their curve normal (sideways for a Z-up frame)."""
|
|
464
|
+
scale = add_node(tree, "ShaderNodeVectorMath", column, 1, operation="SCALE")
|
|
465
|
+
link(tree, points, "Normal", scale, 0)
|
|
466
|
+
link(tree, group_input, "Offset", scale, "Scale")
|
|
467
|
+
move = add_node(tree, "GeometryNodeSetPosition", column, 0)
|
|
468
|
+
link(tree, points, "Points", move, "Geometry")
|
|
469
|
+
link(tree, scale, "Vector", move, "Offset")
|
|
470
|
+
return move
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
# --- measuring ----------------------------------------------------------------------------------------------
|
|
474
|
+
|
|
475
|
+
def instance_count(obj: bpy.types.Object) -> int:
|
|
476
|
+
"""Number of top-level instances the object's evaluated geometry carries (0 once realised)."""
|
|
477
|
+
geometry = obj.evaluated_get(bpy.context.evaluated_depsgraph_get()).evaluated_geometry()
|
|
478
|
+
point_cloud = geometry.instances_pointcloud()
|
|
479
|
+
return len(point_cloud.points) if point_cloud else 0
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
def instance_transforms(obj: bpy.types.Object) -> list:
|
|
483
|
+
"""
|
|
484
|
+
Every instance the object generates, as (referenced data name, world matrix), sorted, so two runs can
|
|
485
|
+
be compared value for value.
|
|
486
|
+
|
|
487
|
+
:param obj: Object with a Geometry Nodes modifier.
|
|
488
|
+
:returns: [(name, Matrix)], sorted by name and translation.
|
|
489
|
+
"""
|
|
490
|
+
depsgraph = bpy.context.evaluated_depsgraph_get()
|
|
491
|
+
found = []
|
|
492
|
+
for instance in depsgraph.object_instances:
|
|
493
|
+
if not instance.is_instance or instance.parent is None or instance.parent.original != obj:
|
|
494
|
+
continue
|
|
495
|
+
data = instance.object.data
|
|
496
|
+
found.append((data.name if data else instance.object.name, instance.matrix_world.copy()))
|
|
497
|
+
found.sort(key=lambda entry: (entry[0], tuple(round(value, 6) for value in entry[1].translation)))
|
|
498
|
+
return found
|
|
499
|
+
|
|
500
|
+
|
|
501
|
+
# --- shipping -----------------------------------------------------------------------------------------------
|
|
502
|
+
|
|
503
|
+
def realize_group(name: str = "BT_Realize") -> bpy.types.GeometryNodeTree:
|
|
504
|
+
"""A group that only realises every instance (Group Input -> Realize Instances -> Group Output)."""
|
|
505
|
+
existing = bpy.data.node_groups.get(name)
|
|
506
|
+
if existing is not None:
|
|
507
|
+
return existing
|
|
508
|
+
tree = new_geometry_group(name, [{"name": "Geometry", "type": "GEOMETRY"}])
|
|
509
|
+
group_in, group_out = group_nodes(tree)
|
|
510
|
+
realize = add_node(tree, "GeometryNodeRealizeInstances", column=1)
|
|
511
|
+
link(tree, group_in, "Geometry", realize, "Geometry")
|
|
512
|
+
link(tree, realize, "Geometry", group_out, "Geometry")
|
|
513
|
+
return tree
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
def realize_to_mesh(obj: bpy.types.Object) -> dict:
|
|
517
|
+
"""
|
|
518
|
+
Bakes the whole modifier stack into the object's mesh with every instance realised (what an exporter that
|
|
519
|
+
does not understand instances needs), and removes the modifiers. Measured on 5.3.0: applying a stack
|
|
520
|
+
whose output still holds instances keeps only the mesh component and silently drops every instance, by
|
|
521
|
+
`modifier_apply` (which applies one modifier at a time, so a Realize modifier further down comes too late)
|
|
522
|
+
and by `meshes.new_from_object` alike. So a Realize Instances modifier goes on the end of the stack and the
|
|
523
|
+
stack is evaluated once, as a whole.
|
|
524
|
+
|
|
525
|
+
:param obj: Mesh object (curve objects: convert to mesh first).
|
|
526
|
+
:returns: {"vertices", "faces"} after baking.
|
|
527
|
+
"""
|
|
528
|
+
add_modifier(obj, realize_group(), name="Realize")
|
|
529
|
+
depsgraph = bpy.context.evaluated_depsgraph_get()
|
|
530
|
+
mesh = bpy.data.meshes.new_from_object(obj.evaluated_get(depsgraph), preserve_all_data_layers=True,
|
|
531
|
+
depsgraph=depsgraph)
|
|
532
|
+
previous = obj.data
|
|
533
|
+
obj.modifiers.clear()
|
|
534
|
+
obj.data = mesh
|
|
535
|
+
mesh.name = previous.name
|
|
536
|
+
if previous.users == 0:
|
|
537
|
+
bpy.data.meshes.remove(previous)
|
|
538
|
+
return {"vertices": len(mesh.vertices), "faces": len(mesh.polygons)}
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
def instances_to_objects(obj: bpy.types.Object, modifier_name: str, parent_name=None) -> dict:
|
|
542
|
+
"""
|
|
543
|
+
Turns the object's instances into real objects that SHARE their mesh data (linked duplicates) under one
|
|
544
|
+
new empty, and removes the modifier's instances from the source. This is the layout both engines want:
|
|
545
|
+
the glTF exporter writes EXT_mesh_gpu_instancing for objects that share a mesh and a parent
|
|
546
|
+
(`export_gpu_instances=True`; BabylonJS loads it as thin instances), and an FBX of linked duplicates
|
|
547
|
+
imports into Unity as one mesh used by many renderers.
|
|
548
|
+
|
|
549
|
+
:param obj: Object whose Geometry Nodes output is instances (Realize off).
|
|
550
|
+
:param modifier_name: The modifier that makes the instances; it is removed afterwards, so the object keeps
|
|
551
|
+
its own mesh (the surface) and the instances live on as objects.
|
|
552
|
+
:param parent_name: Name of the new parent empty (default "<object>_Instances").
|
|
553
|
+
:returns: {"parent", "objects", "meshes"}.
|
|
554
|
+
"""
|
|
555
|
+
entries = instance_transforms(obj)
|
|
556
|
+
parent = bpy.data.objects.new(parent_name or f"{obj.name}_Instances", None)
|
|
557
|
+
for collection in obj.users_collection:
|
|
558
|
+
collection.objects.link(parent)
|
|
559
|
+
parent.matrix_world = Matrix.Identity(4)
|
|
560
|
+
meshes = {}
|
|
561
|
+
for index, (data_name, matrix) in enumerate(entries):
|
|
562
|
+
mesh = meshes.get(data_name)
|
|
563
|
+
if mesh is None:
|
|
564
|
+
mesh = bpy.data.meshes.get(data_name)
|
|
565
|
+
if mesh is None:
|
|
566
|
+
raise KeyError(f"instance data '{data_name}' is not a mesh in this file")
|
|
567
|
+
meshes[data_name] = mesh
|
|
568
|
+
copy = bpy.data.objects.new(f"{data_name}_{index:04d}", mesh)
|
|
569
|
+
for collection in obj.users_collection:
|
|
570
|
+
collection.objects.link(copy)
|
|
571
|
+
copy.parent = parent
|
|
572
|
+
copy.matrix_world = matrix
|
|
573
|
+
obj.modifiers.remove(obj.modifiers[modifier_name])
|
|
574
|
+
bpy.context.view_layer.update() # new objects join view_layer.objects (selection, export) on update
|
|
575
|
+
return {"parent": parent.name, "objects": len(entries), "meshes": sorted(meshes)}
|