@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,370 @@
|
|
|
1
|
+
"""
|
|
2
|
+
bt-blender vehicle parts: wheels, the pivot hierarchy game engines animate, body stations from traced
|
|
3
|
+
outlines, the stance numbers and LOD copies, headless.
|
|
4
|
+
|
|
5
|
+
import construction_kit as kit
|
|
6
|
+
import vehicle_parts as vehicle
|
|
7
|
+
|
|
8
|
+
stations = vehicle.stations_from_outlines(positions, plan, top, bottom, shoulder, lower)
|
|
9
|
+
body = kit.loft("Body", stations)
|
|
10
|
+
wheel = vehicle.wheel("Wheel_FL", radius=0.30, width=0.22, rim_radius=0.19, steps=48, tread_blocks=24)
|
|
11
|
+
vehicle.mount_wheel(wheel, body, hub=(0.78, -1.30, 0.30), steer=True)
|
|
12
|
+
print(vehicle.stance_report([body], vehicle.wheels_of(body)))
|
|
13
|
+
|
|
14
|
+
Conventions (glTF / BabylonJS and Unity agree once exported): the vehicle faces -Y in Blender (+Z after
|
|
15
|
+
export), Z up, ground at z = 0. Every wheel is its own object with its origin at the hub centre and spins
|
|
16
|
+
about its local X (the axle). A steered wheel hangs under a steering node (an empty at the hub) that turns
|
|
17
|
+
about its local Z (up), so steering and spin never fight in one rotation. Names end in the corner:
|
|
18
|
+
FL, FR, RL, RR.
|
|
19
|
+
"""
|
|
20
|
+
import math
|
|
21
|
+
|
|
22
|
+
import bmesh
|
|
23
|
+
import bpy
|
|
24
|
+
import numpy as np
|
|
25
|
+
from mathutils import Matrix, Vector
|
|
26
|
+
|
|
27
|
+
import construction_kit as kit
|
|
28
|
+
|
|
29
|
+
# Wheel corners: name suffix, side sign (+1 = +X = the vehicle's left when it faces -Y), axle (front / rear).
|
|
30
|
+
CORNERS = {"FL": (1.0, "front"), "FR": (-1.0, "front"), "RL": (1.0, "rear"), "RR": (-1.0, "rear")}
|
|
31
|
+
|
|
32
|
+
# A hub is "centred" when the origin is within this distance of the wheel's geometric centre, metres.
|
|
33
|
+
HUB_TOLERANCE = 0.001
|
|
34
|
+
|
|
35
|
+
# A wheel is round when its height and length (across the axle) agree within this share.
|
|
36
|
+
ROUND_TOLERANCE = 0.01
|
|
37
|
+
|
|
38
|
+
# Rim proportions of the lathed wheel: hub radius and rim-face radius as shares of the rim radius.
|
|
39
|
+
HUB_RADIUS_SHARE = 0.35
|
|
40
|
+
RIM_FACE_SHARE = 0.85
|
|
41
|
+
|
|
42
|
+
# Tread lug height above the tread, as a share of the tyre radius.
|
|
43
|
+
TREAD_LUG_HEIGHT_SHARE = 0.03
|
|
44
|
+
|
|
45
|
+
# A crown (tread) face's normal is radial: its axial (X) component stays under this.
|
|
46
|
+
CROWN_AXIAL_LIMIT = 0.05
|
|
47
|
+
|
|
48
|
+
# Crown faces lie within this share of the radius from the tyre's outer radius.
|
|
49
|
+
CROWN_RADIUS_SHARE = 0.02
|
|
50
|
+
|
|
51
|
+
# Faces whose centre lies inside this share of the rim radius get the Rim material.
|
|
52
|
+
RIM_BOUNDARY_SHARE = 0.999
|
|
53
|
+
|
|
54
|
+
# Display size of the steering empties in the viewport, metres.
|
|
55
|
+
STEER_DISPLAY_SIZE = 0.1
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
# --- wheels ----------------------------------------------------------------------------------------------------
|
|
59
|
+
|
|
60
|
+
def wheel_profile(radius: float, width: float, rim_radius: float, shoulder_radius: float, dish_depth: float,
|
|
61
|
+
shoulder_segments: int = 4) -> list:
|
|
62
|
+
"""
|
|
63
|
+
The (radius, axial position) lathe profile of a whole wheel as one closed solid: from the hub on one
|
|
64
|
+
side, across the dished rim face, up the sidewall, round the tyre shoulder, along the tread, and back
|
|
65
|
+
down the other side to the hub. Spin it about X with construction_kit.profile_spin.
|
|
66
|
+
|
|
67
|
+
:param radius: Tyre outer radius, metres.
|
|
68
|
+
:param width: Tyre width (tread width plus shoulders), metres.
|
|
69
|
+
:param rim_radius: Radius where the rim meets the tyre sidewall, metres.
|
|
70
|
+
:param shoulder_radius: Roll of the tyre shoulder, metres (big and soft for karts and cartoon cars,
|
|
71
|
+
small for road tyres).
|
|
72
|
+
:param dish_depth: How far the rim face sits inside the tyre side, metres.
|
|
73
|
+
:param shoulder_segments: Arc segments per shoulder.
|
|
74
|
+
:returns: [(radius, x)] from the x = -width / 2 side to the +width / 2 side, both ends on the axis.
|
|
75
|
+
:raises ValueError: when the proportions do not fit.
|
|
76
|
+
"""
|
|
77
|
+
half = width / 2.0
|
|
78
|
+
if not 0 < rim_radius < radius - shoulder_radius or shoulder_radius <= 0 or not 0 <= dish_depth < half:
|
|
79
|
+
raise ValueError("wheel_profile needs 0 < rim_radius < radius - shoulder_radius and 0 <= dish_depth < width / 2")
|
|
80
|
+
near_side = [(0.0, -half + dish_depth), (rim_radius * HUB_RADIUS_SHARE, -half + dish_depth),
|
|
81
|
+
(rim_radius * RIM_FACE_SHARE, -half + dish_depth), (rim_radius, -half)]
|
|
82
|
+
far_side = [(profile_radius, -x) for profile_radius, x in reversed(near_side)]
|
|
83
|
+
return near_side + _shoulder_points(radius, half, shoulder_radius, shoulder_segments) + far_side
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _shoulder_points(radius: float, half: float, shoulder_radius: float, segments: int) -> list:
|
|
87
|
+
"""Both tyre shoulders with the tread between them, as (radius, x) from the -X side to the +X side."""
|
|
88
|
+
near = [(radius - shoulder_radius + shoulder_radius * math.sin(math.radians(90.0 * step / segments)),
|
|
89
|
+
-half + shoulder_radius - shoulder_radius * math.cos(math.radians(90.0 * step / segments)))
|
|
90
|
+
for step in range(segments + 1)]
|
|
91
|
+
far = [(profile_radius, -x) for profile_radius, x in reversed(near)]
|
|
92
|
+
return near + far
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
# Copies of a part round an axis; shared with construction_kit (one copy). It reads a fresh matrix_world.
|
|
96
|
+
radial_array = kit.radial_array
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _crown_faces(edit_mesh: bmesh.types.BMesh, radius: float) -> list:
|
|
100
|
+
"""The tread row of a lathed wheel (radial normals on the outer radius), ordered round the axle."""
|
|
101
|
+
crown = [face for face in edit_mesh.faces
|
|
102
|
+
if abs(face.normal.x) < CROWN_AXIAL_LIMIT
|
|
103
|
+
and math.hypot(face.calc_center_median().y, face.calc_center_median().z) > radius * (1.0 - CROWN_RADIUS_SHARE)]
|
|
104
|
+
return sorted(crown, key=lambda face: math.atan2(face.calc_center_median().z, face.calc_center_median().y))
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def tread_lugs(obj: bpy.types.Object, radius: float, lugs: int, height: float) -> int:
|
|
108
|
+
"""
|
|
109
|
+
Raises tread lugs out of the tyre's own crown faces (every n-th face round the axle, extruded along its
|
|
110
|
+
normal), so the tread stays one closed, connected solid: no floating blocks, no hidden faces under them.
|
|
111
|
+
The lathe's `steps` must be a multiple of `lugs`, at least two faces per lug (a lug and a groove).
|
|
112
|
+
|
|
113
|
+
:param obj: A lathed wheel (axle along local X) built with wheel_profile.
|
|
114
|
+
:param radius: The tyre radius the profile was built with.
|
|
115
|
+
:param lugs: Lugs round the tyre.
|
|
116
|
+
:param height: Lug height, metres.
|
|
117
|
+
:returns: Lugs made.
|
|
118
|
+
:raises ValueError: when the crown row does not divide into `lugs` lugs with grooves between them.
|
|
119
|
+
"""
|
|
120
|
+
edit_mesh = bmesh.new()
|
|
121
|
+
edit_mesh.from_mesh(obj.data)
|
|
122
|
+
crown = _crown_faces(edit_mesh, radius)
|
|
123
|
+
if not crown or len(crown) % lugs or len(crown) // lugs < 2:
|
|
124
|
+
edit_mesh.free()
|
|
125
|
+
raise ValueError(f"{len(crown)} crown faces cannot carry {lugs} lugs: use steps = 2, 3 .. x lugs")
|
|
126
|
+
chosen = crown[::len(crown) // lugs]
|
|
127
|
+
extruded = bmesh.ops.extrude_discrete_faces(edit_mesh, faces=chosen)["faces"]
|
|
128
|
+
for face in extruded:
|
|
129
|
+
bmesh.ops.translate(edit_mesh, verts=list(face.verts), vec=face.normal * height)
|
|
130
|
+
bmesh.ops.recalc_face_normals(edit_mesh, faces=edit_mesh.faces[:])
|
|
131
|
+
edit_mesh.to_mesh(obj.data)
|
|
132
|
+
edit_mesh.free()
|
|
133
|
+
obj.data.update()
|
|
134
|
+
return len(chosen)
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def wheel(name: str, radius: float, width: float, rim_radius: float, shoulder_radius=None, dish_depth=None,
|
|
138
|
+
steps: int = 32, tread_blocks: int = 0) -> bpy.types.Object:
|
|
139
|
+
"""
|
|
140
|
+
A complete game wheel at the origin, axle along X, origin at the hub centre: one lathed closed solid
|
|
141
|
+
(rim face, sidewalls, rounded shoulders, tread), with optional tread lugs raised out of its crown
|
|
142
|
+
(tread_lugs), so it stays one connected solid. Rim faces get material slot 1 ("Rim"), the tyre slot 0
|
|
143
|
+
("Tyre").
|
|
144
|
+
|
|
145
|
+
Pick `steps` from the wheel's size on screen: the silhouette error of a polygon of n sides on radius r
|
|
146
|
+
is r (1 - cos(180 / n)); keep it under a pixel at the closest view (24-32 for most game wheels, 48+ for
|
|
147
|
+
a hero close-up, 12-16 for a background LOD).
|
|
148
|
+
|
|
149
|
+
:param name: Object name (end it in the corner: Wheel_FL).
|
|
150
|
+
:param radius: Tyre radius, metres.
|
|
151
|
+
:param width: Tyre width, metres.
|
|
152
|
+
:param rim_radius: Visible rim radius, metres.
|
|
153
|
+
:param shoulder_radius: Tyre shoulder roll, metres (default 25 % of the width).
|
|
154
|
+
:param dish_depth: Rim face inset, metres (default 15 % of the width).
|
|
155
|
+
:param steps: Sides around the axle (a multiple of tread_blocks, at least twice it, when there is tread).
|
|
156
|
+
:param tread_blocks: Number of tread lugs (0 for a slick or a normal-mapped tread).
|
|
157
|
+
:returns: The wheel object.
|
|
158
|
+
"""
|
|
159
|
+
shoulder = shoulder_radius if shoulder_radius is not None else 0.25 * width
|
|
160
|
+
dish = dish_depth if dish_depth is not None else 0.15 * width
|
|
161
|
+
obj = kit.profile_spin(name, wheel_profile(radius, width, rim_radius, shoulder, dish), steps=steps, axis="X")
|
|
162
|
+
if tread_blocks:
|
|
163
|
+
tread_lugs(obj, radius, tread_blocks, radius * TREAD_LUG_HEIGHT_SHARE)
|
|
164
|
+
_assign_wheel_materials(obj, rim_radius)
|
|
165
|
+
return obj
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _assign_wheel_materials(obj: bpy.types.Object, rim_radius: float) -> None:
|
|
169
|
+
"""Slot 0 Tyre, slot 1 Rim: faces whose centre lies inside the rim radius belong to the rim."""
|
|
170
|
+
for slot_name in ("Tyre", "Rim"):
|
|
171
|
+
obj.data.materials.append(bpy.data.materials.get(slot_name) or bpy.data.materials.new(slot_name))
|
|
172
|
+
for polygon in obj.data.polygons:
|
|
173
|
+
inside_rim = math.hypot(polygon.center.y, polygon.center.z) < rim_radius * RIM_BOUNDARY_SHARE
|
|
174
|
+
polygon.material_index = 1 if inside_rim else 0
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def rolling_radius(wheel_object: bpy.types.Object) -> float:
|
|
178
|
+
"""
|
|
179
|
+
The wheel's real outer radius about its axle (local X), tread blocks included: the hub height that puts
|
|
180
|
+
the tyre on the ground at z = 0.
|
|
181
|
+
|
|
182
|
+
:param wheel_object: A wheel built about its local X axis.
|
|
183
|
+
:returns: Metres.
|
|
184
|
+
"""
|
|
185
|
+
coordinates = np.empty(len(wheel_object.data.vertices) * 3)
|
|
186
|
+
wheel_object.data.vertices.foreach_get("co", coordinates)
|
|
187
|
+
local = coordinates.reshape(-1, 3)
|
|
188
|
+
return float(np.max(np.hypot(local[:, 1], local[:, 2])))
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
# --- pivots and hierarchy ----------------------------------------------------------------------------------
|
|
192
|
+
|
|
193
|
+
def set_origin(obj: bpy.types.Object, world_point) -> None:
|
|
194
|
+
"""
|
|
195
|
+
Moves an object's origin to a world point without moving its geometry (a hinge line, a hub, a
|
|
196
|
+
steering axis). Rotation and scale must already be applied. The view layer is updated first:
|
|
197
|
+
matrix_world is stale after a script sets .location, and an origin computed from it moved the
|
|
198
|
+
geometry by the whole location (3 m in the test) on Blender 5.3.0.
|
|
199
|
+
|
|
200
|
+
:param obj: Mesh object.
|
|
201
|
+
:param world_point: The new origin, world space.
|
|
202
|
+
"""
|
|
203
|
+
bpy.context.view_layer.update()
|
|
204
|
+
local = obj.matrix_world.inverted() @ Vector(world_point)
|
|
205
|
+
obj.data.transform(Matrix.Translation(-local))
|
|
206
|
+
obj.matrix_world = obj.matrix_world @ Matrix.Translation(local)
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def _parent_keep_local(child: bpy.types.Object, parent: bpy.types.Object, local_location) -> None:
|
|
210
|
+
"""Parents child under parent with an identity parent inverse and the given local location."""
|
|
211
|
+
child.parent = parent
|
|
212
|
+
child.matrix_parent_inverse = Matrix.Identity(4)
|
|
213
|
+
child.location = local_location
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def mount_wheel(wheel_object: bpy.types.Object, body: bpy.types.Object, hub, steer: bool = False) -> bpy.types.Object:
|
|
217
|
+
"""
|
|
218
|
+
Places a wheel (built at the origin) at its hub under the body. A steered wheel goes under a
|
|
219
|
+
`Steer_<corner>` empty at the hub that turns about Z; the wheel itself only ever spins about X. Every
|
|
220
|
+
node keeps an identity parent inverse, so the transforms exported are the ones an engine animates.
|
|
221
|
+
|
|
222
|
+
:param wheel_object: Wheel built at the origin (wheel()), named Wheel_<corner>.
|
|
223
|
+
:param body: The body object (root of the vehicle, origin on the ground under its centre).
|
|
224
|
+
:param hub: Hub centre, world space (the body is assumed unrotated at the origin).
|
|
225
|
+
:param steer: Add the steering node.
|
|
226
|
+
:returns: The node the wheel hangs from (the steering empty, or the body).
|
|
227
|
+
"""
|
|
228
|
+
corner = wheel_object.name.rsplit("_", 1)[-1]
|
|
229
|
+
if not steer:
|
|
230
|
+
_parent_keep_local(wheel_object, body, Vector(hub) - body.location)
|
|
231
|
+
return body
|
|
232
|
+
knuckle = bpy.data.objects.new(f"Steer_{corner}", None)
|
|
233
|
+
knuckle.empty_display_type, knuckle.empty_display_size = "ARROWS", STEER_DISPLAY_SIZE
|
|
234
|
+
bpy.context.scene.collection.objects.link(knuckle)
|
|
235
|
+
_parent_keep_local(knuckle, body, Vector(hub) - body.location)
|
|
236
|
+
_parent_keep_local(wheel_object, knuckle, (0.0, 0.0, 0.0))
|
|
237
|
+
return knuckle
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def wheels_of(body: bpy.types.Object) -> list:
|
|
241
|
+
"""Every wheel object (name starting Wheel_) under the body, at any depth."""
|
|
242
|
+
return sorted((child for child in body.children_recursive if child.name.startswith("Wheel_")), key=lambda obj: obj.name)
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def pivot_report(wheels: list) -> dict:
|
|
246
|
+
"""
|
|
247
|
+
Proves the pivots an engine will animate: each wheel's origin at its geometric hub centre, the wheel
|
|
248
|
+
round about its local X, its X axis horizontal and across the vehicle, and steered wheels under a
|
|
249
|
+
steering node whose origin is the same hub.
|
|
250
|
+
|
|
251
|
+
:param wheels: Wheel objects.
|
|
252
|
+
:returns: {"pass", "wheels": {name: {"hub_offset_m", "round", "axle_world", "steered", "errors"}}}.
|
|
253
|
+
"""
|
|
254
|
+
report = {}
|
|
255
|
+
for obj in wheels:
|
|
256
|
+
local = kit.world_vertices(obj) @ np.array(obj.matrix_world.inverted())[:3, :3].T + np.array(obj.matrix_world.inverted())[:3, 3]
|
|
257
|
+
low, high = local.min(axis=0), local.max(axis=0)
|
|
258
|
+
centre = (low + high) / 2.0
|
|
259
|
+
hub_offset = float(np.linalg.norm(centre))
|
|
260
|
+
across = high - low
|
|
261
|
+
is_round = abs(across[1] - across[2]) <= ROUND_TOLERANCE * max(across[1], across[2])
|
|
262
|
+
axle = obj.matrix_world.to_3x3() @ Vector((1.0, 0.0, 0.0))
|
|
263
|
+
steered = obj.parent is not None and obj.parent.name.startswith("Steer_")
|
|
264
|
+
errors = []
|
|
265
|
+
if hub_offset > HUB_TOLERANCE:
|
|
266
|
+
errors.append(f"origin is {hub_offset:.4f} m from the hub centre")
|
|
267
|
+
if not is_round:
|
|
268
|
+
errors.append("not round about local X: the axle is not the local X axis")
|
|
269
|
+
if abs(axle.z) > ROUND_TOLERANCE or abs(axle.y) > ROUND_TOLERANCE:
|
|
270
|
+
errors.append(f"local X points {tuple(round(value, 3) for value in axle)}, not across the vehicle")
|
|
271
|
+
if steered and (obj.parent.matrix_world.translation - obj.matrix_world.translation).length > HUB_TOLERANCE:
|
|
272
|
+
errors.append("steering node is not at the hub")
|
|
273
|
+
report[obj.name] = {"hub_offset_m": round(hub_offset, 6), "round": bool(is_round),
|
|
274
|
+
"axle_world": [round(value, 4) for value in axle], "steered": steered, "errors": errors}
|
|
275
|
+
return {"pass": not any(entry["errors"] for entry in report.values()), "wheels": report}
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
# --- body and proportions ----------------------------------------------------------------------------------
|
|
279
|
+
|
|
280
|
+
def outline_kinks(*outlines) -> list:
|
|
281
|
+
"""
|
|
282
|
+
Every Y position at which any traced outline has a point: the stations a loft needs, because a form
|
|
283
|
+
can only change where some view's outline bends.
|
|
284
|
+
|
|
285
|
+
:param outlines: [(y, value)] polylines.
|
|
286
|
+
:returns: Sorted unique Y positions.
|
|
287
|
+
"""
|
|
288
|
+
return sorted({round(float(point[0]), 6) for outline in outlines for point in outline})
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
def stations_from_outlines(positions: list, plan_half_width: list, side_top: list, side_bottom: list,
|
|
292
|
+
corner_radius_top: list, corner_radius_bottom: list, segments: int = 5) -> list:
|
|
293
|
+
"""
|
|
294
|
+
Loft stations read from traced outlines, never from a free table of widths: each outline is a
|
|
295
|
+
polyline of (position along Y, value) points traced on its view, evaluated with LINEAR interpolation
|
|
296
|
+
(a smooth curve overshoots a waist), and every section is built inside the plan envelope (its widest
|
|
297
|
+
point is the plan half-width), so no station can be fatter than the top view.
|
|
298
|
+
|
|
299
|
+
:param positions: Y positions of the sections (outline_kinks of all outlines, plus any extra).
|
|
300
|
+
:param plan_half_width: [(y, half width)] from the top view.
|
|
301
|
+
:param side_top: [(y, top z)] from the side view.
|
|
302
|
+
:param side_bottom: [(y, bottom z)] from the side view.
|
|
303
|
+
:param corner_radius_top: [(y, shoulder radius)] from the front view's corner measurements.
|
|
304
|
+
:param corner_radius_bottom: [(y, lower corner radius)].
|
|
305
|
+
:param segments: Arc segments per corner (keep the sagitta under a pixel; see the kit).
|
|
306
|
+
:returns: [(y, section)] ready for construction_kit.loft.
|
|
307
|
+
"""
|
|
308
|
+
def at(outline: list, y: float) -> float:
|
|
309
|
+
"""Linear interpolation of a traced outline at y."""
|
|
310
|
+
points = sorted(outline)
|
|
311
|
+
return float(np.interp(y, [point[0] for point in points], [point[1] for point in points]))
|
|
312
|
+
|
|
313
|
+
built = []
|
|
314
|
+
for y in sorted(positions):
|
|
315
|
+
bottom, top = at(side_bottom, y), at(side_top, y)
|
|
316
|
+
section = kit.rounded_section(2.0 * at(plan_half_width, y), top - bottom, at(corner_radius_top, y),
|
|
317
|
+
at(corner_radius_bottom, y), segments=segments)
|
|
318
|
+
built.append((y, kit.translate_section(section, z=bottom)))
|
|
319
|
+
return built
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
def stance_report(body_objects: list, wheels: list) -> dict:
|
|
323
|
+
"""
|
|
324
|
+
The proportion numbers that decide how a vehicle stands, from world-space vertices: wheelbase, track,
|
|
325
|
+
front and rear overhang, wheel diameter, ride height, and the ratios a reference is compared on.
|
|
326
|
+
Compare each with the same number measured on the reference (measure-stations grades the silhouette;
|
|
327
|
+
this explains WHY a station is wrong).
|
|
328
|
+
|
|
329
|
+
:param body_objects: The body parts (everything that is not a wheel).
|
|
330
|
+
:param wheels: Wheel objects named Wheel_FL / FR / RL / RR.
|
|
331
|
+
:returns: Metres and ratios, rounded to the millimetre / 0.001.
|
|
332
|
+
"""
|
|
333
|
+
body = np.concatenate([kit.world_vertices(obj) for obj in body_objects])
|
|
334
|
+
hubs = {obj.name.rsplit("_", 1)[-1]: np.array(obj.matrix_world.translation) for obj in wheels}
|
|
335
|
+
diameters = [float(np.ptp(kit.world_vertices(obj)[:, 2])) for obj in wheels]
|
|
336
|
+
front_y = (hubs["FL"][1] + hubs["FR"][1]) / 2.0
|
|
337
|
+
rear_y = (hubs["RL"][1] + hubs["RR"][1]) / 2.0
|
|
338
|
+
everything = np.concatenate([body] + [kit.world_vertices(obj) for obj in wheels])
|
|
339
|
+
length = float(np.ptp(everything[:, 1]))
|
|
340
|
+
height = float(np.ptp(everything[:, 2]))
|
|
341
|
+
wheelbase = float(abs(rear_y - front_y))
|
|
342
|
+
report = {
|
|
343
|
+
"length_m": length, "width_m": float(np.ptp(everything[:, 0])), "height_m": height,
|
|
344
|
+
"wheelbase_m": wheelbase,
|
|
345
|
+
"track_front_m": float(abs(hubs["FL"][0] - hubs["FR"][0])), "track_rear_m": float(abs(hubs["RL"][0] - hubs["RR"][0])),
|
|
346
|
+
"overhang_front_m": float(front_y - everything[:, 1].min()), "overhang_rear_m": float(everything[:, 1].max() - rear_y),
|
|
347
|
+
"wheel_diameter_m": float(np.mean(diameters)), "ride_height_m": float(body[:, 2].min()),
|
|
348
|
+
}
|
|
349
|
+
report["wheelbase_to_length"] = wheelbase / length
|
|
350
|
+
report["wheel_to_height"] = report["wheel_diameter_m"] / height
|
|
351
|
+
return {key: round(value, 3) for key, value in report.items()}
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
def lod_chain(obj: bpy.types.Object, ratios: list) -> list:
|
|
355
|
+
"""
|
|
356
|
+
LOD copies by collapse decimation, named <name>_LOD1, _LOD2, ... beside the source (name the source
|
|
357
|
+
<name>_LOD0 yourself: Unity builds a LOD Group from sibling meshes named _LOD0.._LODn on import). Built by
|
|
358
|
+
construction_kit.lod_chain, so every copy carries LOD0's custom normals instead of re-smoothing its
|
|
359
|
+
coarser faces (decimation bends shading, and a level that re-shades pops when it switches).
|
|
360
|
+
|
|
361
|
+
:param obj: The full-detail mesh object (modifiers applied).
|
|
362
|
+
:param ratios: Triangle ratio per LOD against the source, e.g. [0.5, 0.25, 0.12].
|
|
363
|
+
:returns: [{"name", "ratio", "triangles"}] for LOD1 onwards.
|
|
364
|
+
"""
|
|
365
|
+
levels = kit.lod_chain(obj, ratios, parent=False, name_source=False)[1:]
|
|
366
|
+
report = []
|
|
367
|
+
for level, ratio in zip(levels, ratios):
|
|
368
|
+
level.data.calc_loop_triangles()
|
|
369
|
+
report.append({"name": level.name, "ratio": ratio, "triangles": len(level.data.loop_triangles)})
|
|
370
|
+
return report
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"""
|
|
2
|
+
view_consistency -- check that the reference views agree with each other before fitting anything to them.
|
|
3
|
+
|
|
4
|
+
Generated and hand-drawn view sheets rarely agree exactly: the side view's length differs from the top
|
|
5
|
+
view's, or the front view is a little taller than the side view. Fitting a model to views that disagree
|
|
6
|
+
makes every station argue with another one. This tool reads every view of a stations spec (the same
|
|
7
|
+
file measure_stations uses), converts each subject's extent into world metres with that view's own
|
|
8
|
+
pixels-per-metre and origin pixel, and compares every pair of views that shows the same world axis:
|
|
9
|
+
- size along the shared axis, graded MATCH / CLOSE / WRONG like a station;
|
|
10
|
+
- registration: where each view puts the subject's ends on that axis (a wrong origin pixel shows here);
|
|
11
|
+
- optionally the brief's known dimensions ("known_dimensions": {"x": 1.2, "y": 2.4, "z": 0.8}).
|
|
12
|
+
Exits 1 while any size comparison is WRONG. Decide the split once, record why, and grade against it.
|
|
13
|
+
|
|
14
|
+
blender --background --factory-startup --python-exit-code 1 --python view_consistency.py -- \
|
|
15
|
+
--spec stations_spec.json [--out evidence/]
|
|
16
|
+
|
|
17
|
+
Idea credit: the view-inconsistency gate before fitting comes from RobLe3/cc-blender-skill (re-expressed,
|
|
18
|
+
no code copied).
|
|
19
|
+
"""
|
|
20
|
+
import itertools
|
|
21
|
+
import os
|
|
22
|
+
import sys
|
|
23
|
+
|
|
24
|
+
sys.dont_write_bytecode = True
|
|
25
|
+
SCRIPT_DIRECTORY = os.path.dirname(os.path.abspath(__file__))
|
|
26
|
+
if SCRIPT_DIRECTORY not in sys.path:
|
|
27
|
+
sys.path.insert(0, SCRIPT_DIRECTORY)
|
|
28
|
+
|
|
29
|
+
import image_measure # noqa: E402
|
|
30
|
+
import ortho_views # noqa: E402
|
|
31
|
+
import tool_common # noqa: E402
|
|
32
|
+
|
|
33
|
+
TOOL_NAME = "view-consistency"
|
|
34
|
+
|
|
35
|
+
# Ends of the subject on a shared axis may differ by this much (metres) before registration is flagged.
|
|
36
|
+
DEFAULT_REGISTRATION_TOLERANCE = 0.01
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def build_parser() -> tool_common.ToolArgumentParser:
|
|
40
|
+
"""The tool's command-line options."""
|
|
41
|
+
parser = tool_common.ToolArgumentParser(prog=TOOL_NAME, description=__doc__.strip().splitlines()[0])
|
|
42
|
+
parser.add_argument("--spec", required=True, help="stations spec JSON (views with images and frames)")
|
|
43
|
+
parser.add_argument("--registration-tolerance", type=float, default=DEFAULT_REGISTRATION_TOLERANCE,
|
|
44
|
+
help="metres the subject's ends may disagree between views")
|
|
45
|
+
parser.add_argument("--out", help="where to write view_consistency.json (default: the spec's folder)")
|
|
46
|
+
return parser
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def view_extents(view_spec: dict, spec_directory: str) -> dict:
|
|
50
|
+
"""
|
|
51
|
+
The subject's world interval along both of a view's image axes.
|
|
52
|
+
|
|
53
|
+
:param view_spec: A view entry of the spec.
|
|
54
|
+
:param spec_directory: Folder relative paths start from.
|
|
55
|
+
:returns: {axis name: [minimum, maximum]} in metres.
|
|
56
|
+
:raises ValueError: when the image holds no subject.
|
|
57
|
+
"""
|
|
58
|
+
image_path = os.path.normpath(os.path.join(spec_directory, view_spec["image"]))
|
|
59
|
+
rgba = tool_common.load_image(image_path)
|
|
60
|
+
height, width = rgba.shape[:2]
|
|
61
|
+
frame = ortho_views.frame_from_spec(view_spec, width, height)
|
|
62
|
+
mask = image_measure.apply_crop(
|
|
63
|
+
image_measure.subject_mask(rgba, float(view_spec.get("background_tolerance", image_measure.DEFAULT_BACKGROUND_TOLERANCE))), view_spec.get("crop"))
|
|
64
|
+
bounds = image_measure.subject_bounds(mask)
|
|
65
|
+
if bounds is None:
|
|
66
|
+
raise ValueError(f"view '{view_spec['name']}': no subject found in {image_path}")
|
|
67
|
+
basis = ortho_views.view_basis(view_spec["view"])
|
|
68
|
+
extents = {}
|
|
69
|
+
for image_axis, direction, pixels in (("right", basis.right, (bounds[0], bounds[2])),
|
|
70
|
+
("up", basis.up, (bounds[1], bounds[3]))):
|
|
71
|
+
axis, sign = ortho_views.axis_of(direction)
|
|
72
|
+
extents[axis] = sorted(round(ortho_views.pixel_to_coordinate(pixel, image_axis, frame, sign), 5)
|
|
73
|
+
for pixel in pixels)
|
|
74
|
+
return extents
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def compare_pair(first: tuple, second: tuple, axis: str, tolerance: float) -> dict:
|
|
78
|
+
"""
|
|
79
|
+
Compares two views' extents on a shared axis.
|
|
80
|
+
|
|
81
|
+
:param first: (view name, {axis: [min, max]}).
|
|
82
|
+
:param second: (view name, {axis: [min, max]}).
|
|
83
|
+
:param axis: The shared axis.
|
|
84
|
+
:param tolerance: Registration tolerance in metres.
|
|
85
|
+
:returns: Size grade plus the end offsets.
|
|
86
|
+
"""
|
|
87
|
+
first_name, first_extents = first
|
|
88
|
+
second_name, second_extents = second
|
|
89
|
+
first_low, first_high = first_extents[axis]
|
|
90
|
+
second_low, second_high = second_extents[axis]
|
|
91
|
+
record = {"axis": axis, "views": [first_name, second_name],
|
|
92
|
+
"sizes_m": [round(first_high - first_low, 5), round(second_high - second_low, 5)],
|
|
93
|
+
"end_offsets_m": [round(second_low - first_low, 5), round(second_high - first_high, 5)]}
|
|
94
|
+
record.update(image_measure.grade_delta(record["sizes_m"][0], record["sizes_m"][1]))
|
|
95
|
+
record["registered"] = max(abs(offset) for offset in record["end_offsets_m"]) <= tolerance
|
|
96
|
+
return record
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def run(options) -> dict:
|
|
100
|
+
"""
|
|
101
|
+
Compares every pair of views that shares a world axis, and each view against known dimensions.
|
|
102
|
+
|
|
103
|
+
:param options: Parsed options.
|
|
104
|
+
:returns: {"pass", "pairs", "known_dimensions", "views"}.
|
|
105
|
+
"""
|
|
106
|
+
spec_path = os.path.abspath(options.spec)
|
|
107
|
+
spec_directory = os.path.dirname(spec_path)
|
|
108
|
+
spec = tool_common.load_json(spec_path)
|
|
109
|
+
views = [(view["name"], view_extents(view, spec_directory)) for view in spec.get("views") or []]
|
|
110
|
+
if len(views) < 2 and not spec.get("known_dimensions"):
|
|
111
|
+
raise ValueError("need at least two views, or known_dimensions, to compare")
|
|
112
|
+
pairs = []
|
|
113
|
+
for first, second in itertools.combinations(views, 2):
|
|
114
|
+
for axis in sorted(set(first[1]) & set(second[1])):
|
|
115
|
+
pairs.append(compare_pair(first, second, axis, options.registration_tolerance))
|
|
116
|
+
known = []
|
|
117
|
+
for axis, size in (spec.get("known_dimensions") or {}).items():
|
|
118
|
+
for name, extents in views:
|
|
119
|
+
if axis in extents:
|
|
120
|
+
measured = extents[axis][1] - extents[axis][0]
|
|
121
|
+
known.append({"axis": axis, "view": name, "known_m": size, "measured_m": round(measured, 5),
|
|
122
|
+
**image_measure.grade_delta(float(size), measured)})
|
|
123
|
+
wrong = [record for record in pairs + known if record["grade"] == "WRONG"]
|
|
124
|
+
report = {"pass": not wrong, "pairs": pairs, "known_dimensions": known,
|
|
125
|
+
"unregistered_pairs": [record for record in pairs if not record["registered"]],
|
|
126
|
+
"views": {name: extents for name, extents in views}}
|
|
127
|
+
out_directory = os.path.abspath(options.out or spec_directory)
|
|
128
|
+
report["report_json"] = tool_common.write_json(os.path.join(out_directory, "view_consistency.json"), report)
|
|
129
|
+
return report
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
if __name__ == "__main__":
|
|
133
|
+
tool_common.run_tool(TOOL_NAME, build_parser(), run)
|