oda-engine 0.0.0-stage → 0.1.0
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 +151 -2
- package/bin/nowhere.mjs +352 -0
- package/lib/account.mjs +57 -0
- package/lib/agentModules.mjs +57 -0
- package/lib/devServer.mjs +351 -0
- package/lib/headless.mjs +231 -0
- package/lib/headlessPreview.mjs +212 -0
- package/lib/keys.mjs +517 -0
- package/lib/make.mjs +855 -0
- package/lib/mcp.mjs +537 -0
- package/lib/open.mjs +22 -0
- package/lib/preview.mjs +87 -0
- package/lib/previewRpc.mjs +134 -0
- package/lib/project.mjs +359 -0
- package/lib/publishFlow.mjs +90 -0
- package/lib/resources.mjs +50 -0
- package/lib/rules.mjs +60 -0
- package/lib/scaffold.mjs +42 -0
- package/lib/setup.mjs +59 -0
- package/lib/tools.mjs +668 -0
- package/package.json +51 -4
- package/template/game.config.json +9 -0
- package/template/scene.json +4 -0
- package/template/src/main.ts +11 -0
- package/vendor/DOCS.md +447 -0
- package/vendor/agent/appRun.mjs +520 -0
- package/vendor/agent/assets/brief.mjs +242 -0
- package/vendor/agent/assets/collider.mjs +531 -0
- package/vendor/agent/assets/environments/index.mjs +12 -0
- package/vendor/agent/assets/environments/worldlabs.mjs +161 -0
- package/vendor/agent/assets/fetch.mjs +168 -0
- package/vendor/agent/assets/footprint.mjs +13 -0
- package/vendor/agent/assets/gltf.mjs +547 -0
- package/vendor/agent/assets/hdr.mjs +269 -0
- package/vendor/agent/assets/imageOps.mjs +171 -0
- package/vendor/agent/assets/images/codex.mjs +307 -0
- package/vendor/agent/assets/images/fal.mjs +75 -0
- package/vendor/agent/assets/images/gemini.mjs +84 -0
- package/vendor/agent/assets/images/index.mjs +25 -0
- package/vendor/agent/assets/images/openai.mjs +75 -0
- package/vendor/agent/assets/images/stability.mjs +71 -0
- package/vendor/agent/assets/images/views.mjs +31 -0
- package/vendor/agent/assets/import.mjs +1352 -0
- package/vendor/agent/assets/inspect.mjs +465 -0
- package/vendor/agent/assets/jobs.mjs +196 -0
- package/vendor/agent/assets/ktx2.mjs +290 -0
- package/vendor/agent/assets/ledger.mjs +466 -0
- package/vendor/agent/assets/net.mjs +254 -0
- package/vendor/agent/assets/prices.json +39 -0
- package/vendor/agent/assets/providers/fal.mjs +115 -0
- package/vendor/agent/assets/providers/index.mjs +22 -0
- package/vendor/agent/assets/providers/meshy.mjs +119 -0
- package/vendor/agent/assets/providers/tripo.mjs +188 -0
- package/vendor/agent/assets/sources/ambientcg.mjs +129 -0
- package/vendor/agent/assets/sources/index.mjs +23 -0
- package/vendor/agent/assets/sources/polyhaven.mjs +237 -0
- package/vendor/agent/assets/sources/polypizza.mjs +121 -0
- package/vendor/agent/assets/splat.mjs +631 -0
- package/vendor/agent/assets/targets.mjs +122 -0
- package/vendor/agent/assets/tile.mjs +261 -0
- package/vendor/agent/assets/unzip.mjs +180 -0
- package/vendor/agent/blender/cleanup.py +360 -0
- package/vendor/agent/blender/convert.py +167 -0
- package/vendor/agent/blender/find.mjs +192 -0
- package/vendor/agent/blender/nw_export.py +104 -0
- package/vendor/agent/blender/nw_rubric.py +426 -0
- package/vendor/agent/blender/run.mjs +193 -0
- package/vendor/agent/blender/runner.py +252 -0
- package/vendor/agent/blender/sheet.py +743 -0
- package/vendor/agent/depth.mjs +134 -0
- package/vendor/agent/director/board.mjs +451 -0
- package/vendor/agent/director/contracts.mjs +663 -0
- package/vendor/agent/director/director.mjs +2365 -0
- package/vendor/agent/director/estimate.mjs +200 -0
- package/vendor/agent/director/gate.mjs +802 -0
- package/vendor/agent/director/limits.json +16 -0
- package/vendor/agent/director/methods.mjs +457 -0
- package/vendor/agent/director/policy.json +33 -0
- package/vendor/agent/director/prompts.mjs +244 -0
- package/vendor/agent/director/reading.mjs +341 -0
- package/vendor/agent/director/router.mjs +184 -0
- package/vendor/agent/director/seams.mjs +243 -0
- package/vendor/agent/director/session.mjs +643 -0
- package/vendor/agent/director/store.mjs +119 -0
- package/vendor/agent/director/taskGate.mjs +110 -0
- package/vendor/agent/director/teamLedger.mjs +118 -0
- package/vendor/agent/director/triage.mjs +63 -0
- package/vendor/agent/downloads.cjs +363 -0
- package/vendor/agent/flow.mjs +78 -0
- package/vendor/agent/guard.mjs +613 -0
- package/vendor/agent/host.mjs +1736 -0
- package/vendor/agent/language.mjs +58 -0
- package/vendor/agent/ledger.mjs +360 -0
- package/vendor/agent/locale.cjs +186 -0
- package/vendor/agent/loop/capacity.mjs +74 -0
- package/vendor/agent/loop/compose.mjs +127 -0
- package/vendor/agent/loop/gate.mjs +205 -0
- package/vendor/agent/loop/judge.mjs +189 -0
- package/vendor/agent/loop/levels.mjs +273 -0
- package/vendor/agent/loop/state.mjs +358 -0
- package/vendor/agent/notes.cjs +278 -0
- package/vendor/agent/pace.mjs +258 -0
- package/vendor/agent/panels.mjs +431 -0
- package/vendor/agent/prompt.mjs +342 -0
- package/vendor/agent/reference/board.mjs +410 -0
- package/vendor/agent/reference/compose.mjs +364 -0
- package/vendor/agent/reference/decode.mjs +56 -0
- package/vendor/agent/reference/facts.mjs +1874 -0
- package/vendor/agent/reference/png.mjs +107 -0
- package/vendor/agent/reference/records.mjs +63 -0
- package/vendor/agent/reference/web.mjs +252 -0
- package/vendor/agent/roles/artist.mjs +69 -0
- package/vendor/agent/roles/builder.mjs +79 -0
- package/vendor/agent/roles/checks.mjs +223 -0
- package/vendor/agent/roles/common.mjs +188 -0
- package/vendor/agent/roles/critic.mjs +171 -0
- package/vendor/agent/roles/index.mjs +39 -0
- package/vendor/agent/roles/modeller.mjs +142 -0
- package/vendor/agent/roles/qa.mjs +58 -0
- package/vendor/agent/roles/rubrics.mjs +79 -0
- package/vendor/agent/roles/sound.mjs +69 -0
- package/vendor/agent/rounds.cjs +333 -0
- package/vendor/agent/shared.mjs +284 -0
- package/vendor/agent/stopGate.mjs +574 -0
- package/vendor/agent/subagents.mjs +129 -0
- package/vendor/agent/tools/assets.mjs +499 -0
- package/vendor/agent/tools/blender.mjs +754 -0
- package/vendor/agent/tools/cards.mjs +479 -0
- package/vendor/agent/tools/choices.mjs +479 -0
- package/vendor/agent/tools/compose.mjs +74 -0
- package/vendor/agent/tools/environment.mjs +488 -0
- package/vendor/agent/tools/eyes.mjs +733 -0
- package/vendor/agent/tools/generate.mjs +338 -0
- package/vendor/agent/tools/guide.mjs +46 -0
- package/vendor/agent/tools/harness.mjs +193 -0
- package/vendor/agent/tools/image.mjs +643 -0
- package/vendor/agent/tools/index.mjs +162 -0
- package/vendor/agent/tools/look.mjs +578 -0
- package/vendor/agent/tools/loop.mjs +858 -0
- package/vendor/agent/tools/others.mjs +123 -0
- package/vendor/agent/tools/pictures.mjs +535 -0
- package/vendor/agent/tools/reference.mjs +366 -0
- package/vendor/agent/transcripts.mjs +118 -0
- package/vendor/agent-loop.mjs +602 -0
- package/vendor/agent-plugin/.claude-plugin/plugin.json +5 -0
- package/vendor/agent-plugin/skills/art-direction/SKILL.md +82 -0
- package/vendor/agent-plugin/skills/asset-legality/SKILL.md +101 -0
- package/vendor/agent-plugin/skills/asset-sources/SKILL.md +111 -0
- package/vendor/agent-plugin/skills/blender-modeling/SKILL.md +97 -0
- package/vendor/agent-plugin/skills/look-dev/SKILL.md +104 -0
- package/vendor/agent-plugin/skills/playtesting/SKILL.md +74 -0
- package/vendor/agent-plugin/skills/reference-loop/SKILL.md +102 -0
- package/vendor/agent-plugin/skills/sound-design/SKILL.md +60 -0
- package/vendor/agent-plugin/skills/style/SKILL.md +35 -0
- package/vendor/agent-plugin/skills/tsl-r185/SKILL.md +107 -0
- package/vendor/auth.cjs +570 -0
- package/vendor/build.cjs +953 -0
- package/vendor/catalog.json +455 -0
- package/vendor/connectors.cjs +688 -0
- package/vendor/eyes.mjs +733 -0
- package/vendor/gameExports.json +1390 -0
- package/vendor/git.cjs +222 -0
- package/vendor/models.json +57 -0
- package/vendor/publish.cjs +1018 -0
- package/vendor/refBoard.cjs +301 -0
- package/vendor/schema/dist/assets.d.ts +335 -0
- package/vendor/schema/dist/assets.js +439 -0
- package/vendor/schema/dist/brief.d.ts +357 -0
- package/vendor/schema/dist/brief.js +302 -0
- package/vendor/schema/dist/common.d.ts +9 -0
- package/vendor/schema/dist/common.js +13 -0
- package/vendor/schema/dist/config.d.ts +406 -0
- package/vendor/schema/dist/config.js +355 -0
- package/vendor/schema/dist/contract.d.ts +599 -0
- package/vendor/schema/dist/contract.js +1146 -0
- package/vendor/schema/dist/index.d.ts +14 -0
- package/vendor/schema/dist/index.js +21 -0
- package/vendor/schema/dist/look.d.ts +463 -0
- package/vendor/schema/dist/look.js +681 -0
- package/vendor/schema/dist/lookStats.d.ts +230 -0
- package/vendor/schema/dist/lookStats.js +768 -0
- package/vendor/schema/dist/pilot.d.ts +32 -0
- package/vendor/schema/dist/pilot.js +44 -0
- package/vendor/schema/dist/publish.d.ts +214 -0
- package/vendor/schema/dist/publish.js +291 -0
- package/vendor/schema/dist/scene.d.ts +75 -0
- package/vendor/schema/dist/scene.js +120 -0
- package/vendor/schema/dist/skyLight.d.ts +141 -0
- package/vendor/schema/dist/skyLight.js +436 -0
- package/vendor/schema/dist/style.d.ts +131 -0
- package/vendor/schema/dist/style.js +155 -0
- package/vendor/schema/package.json +1 -0
- package/vendor/selectionPrompt.mjs +51 -0
- package/vendor/useTools.mjs +195 -0
- package/vendor/userMessage.mjs +26 -0
- package/web-dist/assets/GaussianSplatUtils-CB77cP9U.js +1 -0
- package/web-dist/assets/KTX2Loader-6OIWUF7s.js +2 -0
- package/web-dist/assets/SPZLoader-zYj0Alfv.js +1 -0
- package/web-dist/assets/gaussianSplat-CJTI1sEh.js +1 -0
- package/web-dist/assets/index-CcjyyQco.css +1 -0
- package/web-dist/assets/index-DMyJcoZJ.js +693 -0
- package/web-dist/assets/meshopt_decoder.module-BvwdjiD3.js +1 -0
- package/web-dist/assets/zstddec.module-CfFeQW7d.js +1 -0
- package/web-dist/index.html +34 -0
|
@@ -0,0 +1,743 @@
|
|
|
1
|
+
# The render run of nw_blender: imports the GLB the export run wrote, so what
|
|
2
|
+
# the agent sees is exactly what the game gets, measures it, and renders two
|
|
3
|
+
# contact sheets (colour and clay), each a 3 x 2 grid:
|
|
4
|
+
# top row: front (from -Y), side (from +X), back (from +Y)
|
|
5
|
+
# bottom row: three-quarter, top (from +Z), close-up
|
|
6
|
+
# with a 1.75 m figure beside the model in the front, back and three-quarter
|
|
7
|
+
# cells. run.mjs starts it like runner.py, with args
|
|
8
|
+
# { glb, out_dir, size, closeup, render, engines, result } and, optional,
|
|
9
|
+
# { class, expect, budget }: the asset class the model is scored against,
|
|
10
|
+
# the size it should have, and the class's triangle budget (the schema's).
|
|
11
|
+
# The answer carries the rubric (nw_rubric.py): silhouette from three alpha
|
|
12
|
+
# masks rendered before the sheets, bevel and the checks from the geometry,
|
|
13
|
+
# materials, budget, and the fix to make first.
|
|
14
|
+
# Written for Blender 4.2 to 5.1.
|
|
15
|
+
|
|
16
|
+
import json
|
|
17
|
+
import math
|
|
18
|
+
import os
|
|
19
|
+
import sys
|
|
20
|
+
import time
|
|
21
|
+
import traceback
|
|
22
|
+
|
|
23
|
+
import bmesh
|
|
24
|
+
import bpy
|
|
25
|
+
import numpy as np
|
|
26
|
+
from mathutils import Matrix, Vector
|
|
27
|
+
|
|
28
|
+
sys.dont_write_bytecode = True
|
|
29
|
+
HERE = os.path.dirname(os.path.realpath(__file__))
|
|
30
|
+
if HERE not in sys.path:
|
|
31
|
+
sys.path.append(HERE)
|
|
32
|
+
|
|
33
|
+
import nw_rubric # noqa: E402
|
|
34
|
+
|
|
35
|
+
ARGS = json.load(open(sys.argv[sys.argv.index('--') + 1], encoding='utf-8'))
|
|
36
|
+
STARTED = time.time()
|
|
37
|
+
WARNINGS = []
|
|
38
|
+
|
|
39
|
+
CELLS = ['front', 'side', 'back', 'three_quarter', 'top', 'closeup']
|
|
40
|
+
FIGURE_CELLS = {'front', 'back', 'three_quarter'}
|
|
41
|
+
FIGURE_HEIGHT = 1.75
|
|
42
|
+
# A 50 mm lens on the 36 mm sensor Blender assumes; a square cell sees the
|
|
43
|
+
# same angle both ways.
|
|
44
|
+
LENS = 50.0
|
|
45
|
+
TAN_HALF = 18.0 / LENS
|
|
46
|
+
MARGIN = 1.1
|
|
47
|
+
# Anthropic's models see at most about 1568 pixels on the long side; a larger
|
|
48
|
+
# sheet is also kept full size on disk, and the model gets a scaled copy.
|
|
49
|
+
MODEL_LONG_EDGE = 1568
|
|
50
|
+
# In 4.2 to 4.4 the new EEVEE is BLENDER_EEVEE_NEXT; from 5.0 it is
|
|
51
|
+
# BLENDER_EEVEE again. The engine list in 5.1 shows only BLENDER_EEVEE although
|
|
52
|
+
# the others assign, so engines are chosen by assigning them, never by
|
|
53
|
+
# reading that list.
|
|
54
|
+
ENGINES = ['BLENDER_EEVEE_NEXT', 'BLENDER_EEVEE', 'BLENDER_WORKBENCH', 'CYCLES']
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def write_result(doc):
|
|
58
|
+
doc['seconds'] = round(time.time() - STARTED, 2)
|
|
59
|
+
doc['blender'] = bpy.app.version_string
|
|
60
|
+
doc['warnings'] = WARNINGS
|
|
61
|
+
with open(ARGS['result'], 'w', encoding='utf-8') as f:
|
|
62
|
+
json.dump(doc, f)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def to_srgb(c):
|
|
66
|
+
c = max(0.0, min(1.0, c))
|
|
67
|
+
return c * 12.92 if c <= 0.0031308 else 1.055 * c ** (1 / 2.4) - 0.055
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def srgb_to_linear(c):
|
|
71
|
+
return c / 12.92 if c <= 0.04045 else ((c + 0.055) / 1.055) ** 2.4
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def hex_colour(rgba):
|
|
75
|
+
return '#' + ''.join(f'{round(to_srgb(c) * 255):02x}' for c in list(rgba)[:3])
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
# ---------------------------------------------------------------- measuring
|
|
79
|
+
|
|
80
|
+
def import_glb():
|
|
81
|
+
bpy.ops.wm.read_factory_settings(use_empty=True)
|
|
82
|
+
before = set(bpy.data.objects)
|
|
83
|
+
bpy.ops.import_scene.gltf(filepath=ARGS['glb'])
|
|
84
|
+
return [o for o in bpy.data.objects if o not in before]
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def measure(objects):
|
|
88
|
+
depsgraph = bpy.context.evaluated_depsgraph_get()
|
|
89
|
+
lo = np.full(3, np.inf)
|
|
90
|
+
hi = np.full(3, -np.inf)
|
|
91
|
+
triangles = 0
|
|
92
|
+
meshes = 0
|
|
93
|
+
for obj in objects:
|
|
94
|
+
if obj.type != 'MESH':
|
|
95
|
+
continue
|
|
96
|
+
meshes += 1
|
|
97
|
+
evaluated = obj.evaluated_get(depsgraph)
|
|
98
|
+
mesh = evaluated.to_mesh()
|
|
99
|
+
try:
|
|
100
|
+
mesh.calc_loop_triangles()
|
|
101
|
+
triangles += len(mesh.loop_triangles)
|
|
102
|
+
count = len(mesh.vertices)
|
|
103
|
+
if count:
|
|
104
|
+
co = np.empty(count * 3, dtype=np.float32)
|
|
105
|
+
mesh.vertices.foreach_get('co', co)
|
|
106
|
+
co = co.reshape(-1, 3).astype(np.float64)
|
|
107
|
+
m = np.array(evaluated.matrix_world)
|
|
108
|
+
world = co @ m[:3, :3].T + m[:3, 3]
|
|
109
|
+
lo = np.minimum(lo, world.min(axis=0))
|
|
110
|
+
hi = np.maximum(hi, world.max(axis=0))
|
|
111
|
+
finally:
|
|
112
|
+
evaluated.to_mesh_clear()
|
|
113
|
+
bounds = None if not np.isfinite(lo).all() else {'min': [float(v) for v in lo], 'max': [float(v) for v in hi]}
|
|
114
|
+
return bounds, triangles, meshes
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def linked_image(socket):
|
|
118
|
+
"""The image texture feeding a socket, through whatever sits between."""
|
|
119
|
+
stack = [link.from_node for link in socket.links]
|
|
120
|
+
seen = set()
|
|
121
|
+
while stack:
|
|
122
|
+
node = stack.pop()
|
|
123
|
+
if node.name in seen:
|
|
124
|
+
continue
|
|
125
|
+
seen.add(node.name)
|
|
126
|
+
if node.type == 'TEX_IMAGE' and node.image is not None:
|
|
127
|
+
return node.image.name
|
|
128
|
+
for s in node.inputs:
|
|
129
|
+
stack.extend(link.from_node for link in s.links)
|
|
130
|
+
return None
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def materials_of(objects):
|
|
134
|
+
materials = []
|
|
135
|
+
textures = {}
|
|
136
|
+
seen = set()
|
|
137
|
+
for obj in objects:
|
|
138
|
+
for slot in getattr(obj, 'material_slots', []):
|
|
139
|
+
mat = slot.material
|
|
140
|
+
if mat is None or mat.name_full in seen:
|
|
141
|
+
continue
|
|
142
|
+
seen.add(mat.name_full)
|
|
143
|
+
info = {'name': mat.name}
|
|
144
|
+
tree = getattr(mat, 'node_tree', None)
|
|
145
|
+
if tree is not None:
|
|
146
|
+
for node in tree.nodes:
|
|
147
|
+
if node.type == 'TEX_IMAGE' and node.image is not None:
|
|
148
|
+
textures[node.image.name] = list(node.image.size)
|
|
149
|
+
bsdf = next((n for n in tree.nodes if n.type == 'BSDF_PRINCIPLED'), None)
|
|
150
|
+
if bsdf is not None:
|
|
151
|
+
base = bsdf.inputs.get('Base Color')
|
|
152
|
+
if base is not None:
|
|
153
|
+
if base.is_linked:
|
|
154
|
+
info['texture'] = linked_image(base) or 'nodes'
|
|
155
|
+
else:
|
|
156
|
+
info['color'] = hex_colour(base.default_value)
|
|
157
|
+
normal = bsdf.inputs.get('Normal')
|
|
158
|
+
if normal is not None and normal.is_linked:
|
|
159
|
+
info['normal'] = linked_image(normal) or 'nodes'
|
|
160
|
+
for key, name in (('roughness', 'Roughness'), ('metallic', 'Metallic'), ('alpha', 'Alpha')):
|
|
161
|
+
socket = bsdf.inputs.get(name)
|
|
162
|
+
if socket is not None:
|
|
163
|
+
info[key] = 'texture' if socket.is_linked else round(float(socket.default_value), 3)
|
|
164
|
+
colour = bsdf.inputs.get('Emission Color') or bsdf.inputs.get('Emission')
|
|
165
|
+
strength = bsdf.inputs.get('Emission Strength')
|
|
166
|
+
if colour is not None:
|
|
167
|
+
lit = colour.is_linked or max(list(colour.default_value)[:3]) > 0
|
|
168
|
+
power = float(strength.default_value) if strength is not None else 1.0
|
|
169
|
+
if lit and power > 0:
|
|
170
|
+
info['emission'] = round(power, 3)
|
|
171
|
+
materials.append(info)
|
|
172
|
+
return materials, [{'name': k, 'width': v[0], 'height': v[1]} for k, v in textures.items()]
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
# ---------------------------------------------------------------- staging
|
|
176
|
+
|
|
177
|
+
def make_figure(bounds):
|
|
178
|
+
"""A plain 1.75 m mannequin: two legs, a capsule body, a head."""
|
|
179
|
+
lo, hi = bounds['min'], bounds['max']
|
|
180
|
+
bm = bmesh.new()
|
|
181
|
+
for dx in (-0.09, 0.09):
|
|
182
|
+
bmesh.ops.create_cone(bm, cap_ends=True, segments=16, radius1=0.075, radius2=0.075, depth=0.5,
|
|
183
|
+
matrix=Matrix.Translation((dx, 0, 0.25)))
|
|
184
|
+
body = bmesh.new()
|
|
185
|
+
bmesh.ops.create_uvsphere(body, u_segments=24, v_segments=12, radius=0.17)
|
|
186
|
+
for v in body.verts:
|
|
187
|
+
v.co.z += 0.43 if v.co.z >= -1e-6 else -0.43
|
|
188
|
+
body.transform(Matrix.Translation((0, 0, 0.92)))
|
|
189
|
+
bmesh.ops.create_uvsphere(bm, u_segments=24, v_segments=12, radius=0.115, matrix=Matrix.Translation((0, 0, 1.635)))
|
|
190
|
+
mesh = bpy.data.meshes.new('nw_figure')
|
|
191
|
+
body.to_mesh(mesh)
|
|
192
|
+
body.free()
|
|
193
|
+
extra = bpy.data.meshes.new('nw_figure_parts')
|
|
194
|
+
bm.to_mesh(extra)
|
|
195
|
+
bm.free()
|
|
196
|
+
merged = bmesh.new()
|
|
197
|
+
merged.from_mesh(mesh)
|
|
198
|
+
merged.from_mesh(extra)
|
|
199
|
+
for f in merged.faces:
|
|
200
|
+
f.smooth = True
|
|
201
|
+
merged.to_mesh(mesh)
|
|
202
|
+
merged.free()
|
|
203
|
+
bpy.data.meshes.remove(extra)
|
|
204
|
+
mat = bpy.data.materials.new('nw_figure')
|
|
205
|
+
principled(mat, (0.32, 0.32, 0.32, 1), 0.7)
|
|
206
|
+
mesh.materials.append(mat)
|
|
207
|
+
obj = bpy.data.objects.new('nw_figure', mesh)
|
|
208
|
+
obj.location = (hi[0] + 0.6 + 0.2, (lo[1] + hi[1]) / 2, lo[2])
|
|
209
|
+
bpy.context.scene.collection.objects.link(obj)
|
|
210
|
+
return obj
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def use_nodes(datablock):
|
|
214
|
+
# From 5.0 materials and worlds always have nodes, and the switch is
|
|
215
|
+
# deprecated (it prints a warning the agent would read).
|
|
216
|
+
if bpy.app.version < (5, 0, 0):
|
|
217
|
+
datablock.use_nodes = True
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
def principled(mat, colour, roughness):
|
|
221
|
+
use_nodes(mat)
|
|
222
|
+
bsdf = next((n for n in mat.node_tree.nodes if n.type == 'BSDF_PRINCIPLED'), None) if mat.node_tree else None
|
|
223
|
+
if bsdf is not None:
|
|
224
|
+
bsdf.inputs['Base Color'].default_value = colour
|
|
225
|
+
bsdf.inputs['Roughness'].default_value = roughness
|
|
226
|
+
mat.diffuse_color = colour
|
|
227
|
+
return mat
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
WORLD_GREY = 0.25
|
|
231
|
+
# What the camera sees behind the clay model. Light-grey clay in shadow comes
|
|
232
|
+
# out close to the world's own grey, so the clay pass gets a darker backdrop;
|
|
233
|
+
# the light on the model stays the same.
|
|
234
|
+
CLAY_BACKDROP = 0.06
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def stage_world(radius, centre):
|
|
238
|
+
"""Returns the Background node the camera sees, for the clay pass."""
|
|
239
|
+
scene = bpy.context.scene
|
|
240
|
+
world = bpy.data.worlds.new('nw_world')
|
|
241
|
+
scene.world = world
|
|
242
|
+
world.color = (WORLD_GREY, WORLD_GREY, WORLD_GREY)
|
|
243
|
+
use_nodes(world)
|
|
244
|
+
tree = world.node_tree
|
|
245
|
+
backdrop = None
|
|
246
|
+
if tree is not None:
|
|
247
|
+
for node in list(tree.nodes):
|
|
248
|
+
tree.nodes.remove(node)
|
|
249
|
+
out = tree.nodes.new('ShaderNodeOutputWorld')
|
|
250
|
+
light = tree.nodes.new('ShaderNodeBackground')
|
|
251
|
+
light.inputs[0].default_value = (WORLD_GREY, WORLD_GREY, WORLD_GREY, 1)
|
|
252
|
+
light.inputs[1].default_value = 1.0
|
|
253
|
+
backdrop = tree.nodes.new('ShaderNodeBackground')
|
|
254
|
+
backdrop.inputs[0].default_value = (WORLD_GREY, WORLD_GREY, WORLD_GREY, 1)
|
|
255
|
+
backdrop.inputs[1].default_value = 1.0
|
|
256
|
+
path = tree.nodes.new('ShaderNodeLightPath')
|
|
257
|
+
mix = tree.nodes.new('ShaderNodeMixShader')
|
|
258
|
+
tree.links.new(path.outputs['Is Camera Ray'], mix.inputs[0])
|
|
259
|
+
tree.links.new(light.outputs[0], mix.inputs[1])
|
|
260
|
+
tree.links.new(backdrop.outputs[0], mix.inputs[2])
|
|
261
|
+
tree.links.new(mix.outputs[0], out.inputs[0])
|
|
262
|
+
|
|
263
|
+
# The key from the front-left, above, as a painter would light a statue.
|
|
264
|
+
sun = bpy.data.objects.new('nw_sun', bpy.data.lights.new('nw_sun', 'SUN'))
|
|
265
|
+
sun.data.energy = 3.0
|
|
266
|
+
sun.data.angle = math.radians(2)
|
|
267
|
+
sun.rotation_euler = Vector((-1.0, -1.2, 1.6)).to_track_quat('Z', 'Y').to_euler()
|
|
268
|
+
scene.collection.objects.link(sun)
|
|
269
|
+
# A soft fill from the right and a little behind, so the back cell is
|
|
270
|
+
# not lit by the grey world alone (a white back face would then match the
|
|
271
|
+
# backdrop exactly and vanish). Scaled with the model, so a 20 m tower and
|
|
272
|
+
# a 20 cm cup get the same light on them.
|
|
273
|
+
dist = max(radius * 3.0, 2.0)
|
|
274
|
+
fill = bpy.data.objects.new('nw_fill', bpy.data.lights.new('nw_fill', 'AREA'))
|
|
275
|
+
fill.data.size = dist * 0.8
|
|
276
|
+
fill.data.energy = 0.7 * math.pi * dist * dist
|
|
277
|
+
direction = Vector((1.0, 0.6, 0.5)).normalized()
|
|
278
|
+
fill.location = Vector(centre) + direction * dist
|
|
279
|
+
fill.rotation_euler = direction.to_track_quat('Z', 'Y').to_euler()
|
|
280
|
+
scene.collection.objects.link(fill)
|
|
281
|
+
|
|
282
|
+
vs = scene.view_settings
|
|
283
|
+
for attr, value in (('view_transform', 'AgX'), ('look', 'None')):
|
|
284
|
+
try:
|
|
285
|
+
setattr(vs, attr, value)
|
|
286
|
+
except Exception:
|
|
287
|
+
WARNINGS.append(f'could not set {attr} {value}')
|
|
288
|
+
scene.render.film_transparent = False
|
|
289
|
+
return backdrop
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
# ---------------------------------------------------------------- cameras
|
|
293
|
+
|
|
294
|
+
def basis(forward):
|
|
295
|
+
f = Vector(forward).normalized()
|
|
296
|
+
up = Vector((0, 0, 1)) if abs(f.z) < 0.999 else Vector((0, 1, 0))
|
|
297
|
+
r = f.cross(up).normalized()
|
|
298
|
+
u = r.cross(f)
|
|
299
|
+
return r, u, f
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
def aim(cam, eye, r, u, f):
|
|
303
|
+
rot = Matrix((r, u, -f)).transposed().to_4x4()
|
|
304
|
+
cam.matrix_world = Matrix.Translation(eye) @ rot
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
def corners(lo, hi):
|
|
308
|
+
return [Vector((x, y, z)) for x in (lo[0], hi[0]) for y in (lo[1], hi[1]) for z in (lo[2], hi[2])]
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
def union(a, b):
|
|
312
|
+
return [min(a[0][i], b[0][i]) for i in range(3)], [max(a[1][i], b[1][i]) for i in range(3)]
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
def ortho(cam, box, forward):
|
|
316
|
+
r, u, f = basis(forward)
|
|
317
|
+
pts = corners(*box)
|
|
318
|
+
pr = [p.dot(r) for p in pts]
|
|
319
|
+
pu = [p.dot(u) for p in pts]
|
|
320
|
+
pf = [p.dot(f) for p in pts]
|
|
321
|
+
target = r * (min(pr) + max(pr)) / 2 + u * (min(pu) + max(pu)) / 2 + f * (min(pf) + max(pf)) / 2
|
|
322
|
+
depth = max(pf) - min(pf)
|
|
323
|
+
back = depth / 2 + max(1.0, depth * 0.5)
|
|
324
|
+
cam.data.type = 'ORTHO'
|
|
325
|
+
cam.data.ortho_scale = max(max(pr) - min(pr), max(pu) - min(pu), 1e-3) * MARGIN
|
|
326
|
+
cam.data.clip_start = 0.001 * max(1.0, depth)
|
|
327
|
+
cam.data.clip_end = back * 2 + depth + 10
|
|
328
|
+
aim(cam, target - f * back, r, u, f)
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
def perspective_fit(cam, box, toward_camera):
|
|
332
|
+
r, u, f = basis(-Vector(toward_camera))
|
|
333
|
+
lo, hi = box
|
|
334
|
+
centre = Vector([(lo[i] + hi[i]) / 2 for i in range(3)])
|
|
335
|
+
t = TAN_HALF / MARGIN
|
|
336
|
+
dist = 0.0
|
|
337
|
+
for p in corners(lo, hi):
|
|
338
|
+
rel = p - centre
|
|
339
|
+
z = -rel.dot(f) # toward the camera
|
|
340
|
+
dist = max(dist, z + abs(rel.dot(r)) / t, z + abs(rel.dot(u)) / t)
|
|
341
|
+
perspective(cam, centre, dist, r, u, f, (Vector(hi) - Vector(lo)).length)
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
def perspective(cam, target, dist, r, u, f, extent):
|
|
345
|
+
dist = max(dist, 0.05)
|
|
346
|
+
cam.data.type = 'PERSP'
|
|
347
|
+
cam.data.lens = LENS
|
|
348
|
+
cam.data.sensor_width = 36.0
|
|
349
|
+
cam.data.sensor_fit = 'AUTO'
|
|
350
|
+
cam.data.clip_start = max(0.001, dist * 0.002)
|
|
351
|
+
cam.data.clip_end = dist + extent * 2 + 10
|
|
352
|
+
aim(cam, Vector(target) - f * dist, r, u, f)
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
def three_quarter_direction():
|
|
356
|
+
az, el = math.radians(45), math.radians(25)
|
|
357
|
+
# From the target toward the camera: halfway between the front (-Y) and +X, raised.
|
|
358
|
+
return Vector((math.sin(az) * math.cos(el), -math.cos(az) * math.cos(el), math.sin(el)))
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
def place_camera(cam, cell, model_box, figure_box):
|
|
362
|
+
box = union(model_box, figure_box) if figure_box and cell in FIGURE_CELLS else model_box
|
|
363
|
+
if cell == 'front':
|
|
364
|
+
ortho(cam, box, (0, 1, 0))
|
|
365
|
+
elif cell == 'side':
|
|
366
|
+
ortho(cam, box, (-1, 0, 0))
|
|
367
|
+
elif cell == 'back':
|
|
368
|
+
ortho(cam, box, (0, -1, 0))
|
|
369
|
+
elif cell == 'top':
|
|
370
|
+
ortho(cam, box, (0, 0, -1))
|
|
371
|
+
elif cell == 'three_quarter':
|
|
372
|
+
perspective_fit(cam, box, three_quarter_direction())
|
|
373
|
+
else:
|
|
374
|
+
lo, hi = model_box
|
|
375
|
+
size = [hi[i] - lo[i] for i in range(3)]
|
|
376
|
+
spec = ARGS.get('closeup') or {}
|
|
377
|
+
target = spec.get('target') or [(lo[0] + hi[0]) / 2, (lo[1] + hi[1]) / 2, hi[2] - size[2] / 6]
|
|
378
|
+
radius = spec.get('radius') or max(max(size) / 3, 1e-3)
|
|
379
|
+
r, u, f = basis(-three_quarter_direction())
|
|
380
|
+
perspective(cam, target, radius / math.sin(math.atan(TAN_HALF)), r, u, f, radius * 2)
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
# ---------------------------------------------------------------- rendering
|
|
384
|
+
|
|
385
|
+
def set_engine(engine):
|
|
386
|
+
scene = bpy.context.scene
|
|
387
|
+
try:
|
|
388
|
+
scene.render.engine = engine
|
|
389
|
+
except Exception:
|
|
390
|
+
return False
|
|
391
|
+
if engine.startswith('BLENDER_EEVEE'):
|
|
392
|
+
scene.eevee.taa_render_samples = 32
|
|
393
|
+
elif engine == 'BLENDER_WORKBENCH':
|
|
394
|
+
shading = scene.display.shading
|
|
395
|
+
shading.light = 'STUDIO'
|
|
396
|
+
shading.color_type = 'MATERIAL'
|
|
397
|
+
elif engine == 'CYCLES':
|
|
398
|
+
scene.cycles.device = 'CPU'
|
|
399
|
+
scene.cycles.samples = 16
|
|
400
|
+
return True
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
def render_cells(engine, cam, model_box, figure, clay, backdrop):
|
|
404
|
+
scene = bpy.context.scene
|
|
405
|
+
size = int(ARGS['size'])
|
|
406
|
+
scene.render.resolution_x = size
|
|
407
|
+
scene.render.resolution_y = size
|
|
408
|
+
scene.render.resolution_percentage = 100
|
|
409
|
+
settings = scene.render.image_settings
|
|
410
|
+
settings.file_format = 'JPEG'
|
|
411
|
+
settings.color_mode = 'RGB'
|
|
412
|
+
settings.quality = 92
|
|
413
|
+
figure_box = None
|
|
414
|
+
if figure is not None:
|
|
415
|
+
bpy.context.view_layer.update()
|
|
416
|
+
pts = [figure.matrix_world @ Vector(c) for c in figure.bound_box]
|
|
417
|
+
figure_box = ([min(p[i] for p in pts) for i in range(3)], [max(p[i] for p in pts) for i in range(3)])
|
|
418
|
+
paths = {}
|
|
419
|
+
for kind, override in (('color', None), ('clay', clay)):
|
|
420
|
+
bpy.context.view_layer.material_override = override
|
|
421
|
+
if backdrop is not None:
|
|
422
|
+
grey = CLAY_BACKDROP if override is not None else WORLD_GREY
|
|
423
|
+
backdrop.inputs[0].default_value = (grey, grey, grey, 1)
|
|
424
|
+
for cell in CELLS:
|
|
425
|
+
if figure is not None:
|
|
426
|
+
figure.hide_render = cell not in FIGURE_CELLS
|
|
427
|
+
place_camera(cam, cell, model_box, figure_box)
|
|
428
|
+
path = os.path.join(ARGS['out_dir'], f'{kind}-{cell}.jpg')
|
|
429
|
+
scene.render.filepath = path
|
|
430
|
+
bpy.ops.render.render(write_still=True)
|
|
431
|
+
if not os.path.isfile(path):
|
|
432
|
+
raise RuntimeError(f'{engine} wrote no image for the {cell} cell')
|
|
433
|
+
paths[f'{kind}-{cell}'] = path
|
|
434
|
+
bpy.context.view_layer.material_override = None
|
|
435
|
+
return paths
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
def compose(cells, kind, size):
|
|
439
|
+
"""One 3 x 2 sheet from six cell renders. Blender keeps pixels bottom-up,
|
|
440
|
+
so the sheet's top row goes at the high end of the array."""
|
|
441
|
+
sheet = np.zeros((2 * size, 3 * size, 4), dtype=np.float32)
|
|
442
|
+
for i, cell in enumerate(CELLS):
|
|
443
|
+
img = bpy.data.images.load(cells[f'{kind}-{cell}'], check_existing=False)
|
|
444
|
+
w, h = img.size
|
|
445
|
+
px = np.empty(w * h * 4, dtype=np.float32)
|
|
446
|
+
img.pixels.foreach_get(px)
|
|
447
|
+
bpy.data.images.remove(img)
|
|
448
|
+
px = px.reshape(h, w, 4)[:size, :size]
|
|
449
|
+
row = 1 if i < 3 else 0
|
|
450
|
+
col = i % 3
|
|
451
|
+
sheet[row * size:row * size + px.shape[0], col * size:col * size + px.shape[1]] = px
|
|
452
|
+
# Thin dark lines between the cells, so six views on one grey read as six.
|
|
453
|
+
line = 0.08
|
|
454
|
+
for col in (1, 2):
|
|
455
|
+
sheet[:, col * size - 1:col * size + 1, :3] = line
|
|
456
|
+
sheet[size - 1:size + 1, :, :3] = line
|
|
457
|
+
sheet[..., 3] = 1.0
|
|
458
|
+
out = os.path.join(ARGS['out_dir'], f'sheet-{kind}.jpg')
|
|
459
|
+
img = bpy.data.images.new(f'nw_sheet_{kind}', 3 * size, 2 * size, alpha=False)
|
|
460
|
+
img.pixels.foreach_set(sheet.ravel())
|
|
461
|
+
save_jpeg(img, out)
|
|
462
|
+
small = None
|
|
463
|
+
if 3 * size > MODEL_LONG_EDGE:
|
|
464
|
+
small = os.path.join(ARGS['out_dir'], f'sheet-{kind}-model.jpg')
|
|
465
|
+
img.scale(MODEL_LONG_EDGE, round(MODEL_LONG_EDGE * 2 / 3))
|
|
466
|
+
save_jpeg(img, small)
|
|
467
|
+
bpy.data.images.remove(img)
|
|
468
|
+
return out, small
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
def save_jpeg(img, path):
|
|
472
|
+
img.file_format = 'JPEG'
|
|
473
|
+
img.filepath_raw = path
|
|
474
|
+
try:
|
|
475
|
+
img.save(filepath=path, quality=85)
|
|
476
|
+
except TypeError:
|
|
477
|
+
img.save()
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
# ---------------------------------------------------------------- the rubric
|
|
481
|
+
|
|
482
|
+
MASK_VIEWS = (('front', (0, 1, 0)), ('side', (-1, 0, 0)), ('top', (0, 0, -1)))
|
|
483
|
+
MASK_SIZE = 128
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
def set_mask_engine(engine):
|
|
487
|
+
scene = bpy.context.scene
|
|
488
|
+
try:
|
|
489
|
+
scene.render.engine = engine
|
|
490
|
+
except Exception:
|
|
491
|
+
return False
|
|
492
|
+
if engine == 'BLENDER_WORKBENCH':
|
|
493
|
+
shading = scene.display.shading
|
|
494
|
+
shading.light = 'FLAT'
|
|
495
|
+
shading.color_type = 'SINGLE'
|
|
496
|
+
elif engine == 'CYCLES':
|
|
497
|
+
scene.cycles.device = 'CPU'
|
|
498
|
+
scene.cycles.samples = 1
|
|
499
|
+
return True
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
def render_masks(model_box):
|
|
503
|
+
"""Alpha masks of the model alone (no figure), front, side and top,
|
|
504
|
+
orthographic at 128 px: what the silhouette score reads."""
|
|
505
|
+
scene = bpy.context.scene
|
|
506
|
+
cam = bpy.data.objects.new('nw_mask_camera', bpy.data.cameras.new('nw_mask_camera'))
|
|
507
|
+
scene.collection.objects.link(cam)
|
|
508
|
+
scene.camera = cam
|
|
509
|
+
render = scene.render
|
|
510
|
+
saved = (render.engine, render.film_transparent, render.resolution_x, render.resolution_y, render.resolution_percentage,
|
|
511
|
+
render.image_settings.file_format, render.image_settings.color_mode)
|
|
512
|
+
render.film_transparent = True
|
|
513
|
+
render.resolution_x = render.resolution_y = MASK_SIZE
|
|
514
|
+
render.resolution_percentage = 100
|
|
515
|
+
render.image_settings.file_format = 'PNG'
|
|
516
|
+
render.image_settings.color_mode = 'RGBA'
|
|
517
|
+
masks = {}
|
|
518
|
+
# A run that was told which engines to use (the CPU retry after a GPU
|
|
519
|
+
# engine took Blender down) draws its masks with those too.
|
|
520
|
+
wanted = [e for e in (ARGS.get('engines') or ['BLENDER_WORKBENCH', 'CYCLES']) if e in ('BLENDER_WORKBENCH', 'CYCLES')] or ['CYCLES']
|
|
521
|
+
try:
|
|
522
|
+
for engine in wanted:
|
|
523
|
+
if not set_mask_engine(engine):
|
|
524
|
+
continue
|
|
525
|
+
try:
|
|
526
|
+
for name, forward in MASK_VIEWS:
|
|
527
|
+
ortho(cam, model_box, forward)
|
|
528
|
+
path = os.path.join(ARGS['out_dir'], f'mask-{name}.png')
|
|
529
|
+
render.filepath = path
|
|
530
|
+
bpy.ops.render.render(write_still=True)
|
|
531
|
+
img = bpy.data.images.load(path, check_existing=False)
|
|
532
|
+
w, h = img.size
|
|
533
|
+
px = np.empty(w * h * 4, dtype=np.float32)
|
|
534
|
+
img.pixels.foreach_get(px)
|
|
535
|
+
bpy.data.images.remove(img)
|
|
536
|
+
masks[name] = px[3::4].reshape(h, w)
|
|
537
|
+
break
|
|
538
|
+
except Exception as e:
|
|
539
|
+
WARNINGS.append(f'the silhouette masks could not be rendered with {engine} ({e})')
|
|
540
|
+
masks = {}
|
|
541
|
+
finally:
|
|
542
|
+
(render.engine, render.film_transparent, render.resolution_x, render.resolution_y, render.resolution_percentage,
|
|
543
|
+
render.image_settings.file_format, render.image_settings.color_mode) = saved
|
|
544
|
+
bpy.data.objects.remove(cam, do_unlink=True)
|
|
545
|
+
return masks
|
|
546
|
+
|
|
547
|
+
|
|
548
|
+
def texture_mean(name):
|
|
549
|
+
"""The average sRGB colour of an image (every 7th pixel is plenty)."""
|
|
550
|
+
img = bpy.data.images.get(name)
|
|
551
|
+
if img is None or not img.size[0]:
|
|
552
|
+
return None
|
|
553
|
+
px = np.empty(img.size[0] * img.size[1] * 4, dtype=np.float32)
|
|
554
|
+
img.pixels.foreach_get(px)
|
|
555
|
+
rgb = px.reshape(-1, 4)[::7, :3].mean(axis=0)
|
|
556
|
+
if img.is_float:
|
|
557
|
+
rgb = np.array([to_srgb(float(c)) for c in rgb])
|
|
558
|
+
return [float(c) for c in np.clip(rgb, 0, 1)]
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
def material_infos(materials):
|
|
562
|
+
out = []
|
|
563
|
+
for m in materials:
|
|
564
|
+
rgb = None
|
|
565
|
+
if m.get('color'):
|
|
566
|
+
h = m['color']
|
|
567
|
+
rgb = [int(h[i:i + 2], 16) / 255 for i in (1, 3, 5)]
|
|
568
|
+
elif m.get('texture') and m['texture'] != 'nodes':
|
|
569
|
+
rgb = texture_mean(m['texture'])
|
|
570
|
+
out.append({'name': m['name'], 'rgb': rgb, 'textured': bool(m.get('texture')), 'normal': bool(m.get('normal')),
|
|
571
|
+
'roughness': m.get('roughness'), 'emission': bool(m.get('emission'))})
|
|
572
|
+
return out
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+
def uv_and_texel(objects):
|
|
576
|
+
"""Meshes whose materials use textures but that have no UVs, and the texel
|
|
577
|
+
density (px per metre) range over the textured ones."""
|
|
578
|
+
depsgraph = bpy.context.evaluated_depsgraph_get()
|
|
579
|
+
missing = []
|
|
580
|
+
densities = []
|
|
581
|
+
for obj in objects:
|
|
582
|
+
if obj.type != 'MESH':
|
|
583
|
+
continue
|
|
584
|
+
px = 0
|
|
585
|
+
for slot in obj.material_slots:
|
|
586
|
+
tree = getattr(slot.material, 'node_tree', None) if slot.material else None
|
|
587
|
+
if tree is not None:
|
|
588
|
+
for node in tree.nodes:
|
|
589
|
+
if node.type == 'TEX_IMAGE' and node.image is not None:
|
|
590
|
+
px = max(px, node.image.size[0])
|
|
591
|
+
if not px:
|
|
592
|
+
continue
|
|
593
|
+
if not obj.data.uv_layers:
|
|
594
|
+
missing.append(obj.name)
|
|
595
|
+
continue
|
|
596
|
+
ev = obj.evaluated_get(depsgraph)
|
|
597
|
+
mesh = ev.to_mesh()
|
|
598
|
+
try:
|
|
599
|
+
mesh.calc_loop_triangles()
|
|
600
|
+
n = len(mesh.loop_triangles)
|
|
601
|
+
if not n or mesh.uv_layers.active is None:
|
|
602
|
+
continue
|
|
603
|
+
loops = np.empty(n * 3, dtype=np.int64)
|
|
604
|
+
mesh.loop_triangles.foreach_get('loops', loops)
|
|
605
|
+
verts = np.empty(n * 3, dtype=np.int64)
|
|
606
|
+
mesh.loop_triangles.foreach_get('vertices', verts)
|
|
607
|
+
uv = np.empty(len(mesh.loops) * 2)
|
|
608
|
+
mesh.uv_layers.active.data.foreach_get('uv', uv)
|
|
609
|
+
uv = uv.reshape(-1, 2)[loops].reshape(-1, 3, 2)
|
|
610
|
+
co = np.empty(len(mesh.vertices) * 3)
|
|
611
|
+
mesh.vertices.foreach_get('co', co)
|
|
612
|
+
m = np.array(ev.matrix_world)
|
|
613
|
+
co = (co.reshape(-1, 3) @ m[:3, :3].T)[verts].reshape(-1, 3, 3)
|
|
614
|
+
e1, e2 = uv[:, 1] - uv[:, 0], uv[:, 2] - uv[:, 0]
|
|
615
|
+
uv_area = np.abs(e1[:, 0] * e2[:, 1] - e1[:, 1] * e2[:, 0]).sum() / 2
|
|
616
|
+
area = np.linalg.norm(np.cross(co[:, 1] - co[:, 0], co[:, 2] - co[:, 0]), axis=1).sum() / 2
|
|
617
|
+
if area > 0 and uv_area > 0:
|
|
618
|
+
densities.append(math.sqrt(uv_area / area) * px)
|
|
619
|
+
finally:
|
|
620
|
+
ev.to_mesh_clear()
|
|
621
|
+
if densities:
|
|
622
|
+
lo, hi = min(densities), max(densities)
|
|
623
|
+
texel = {'min': round(lo), 'max': round(hi), 'note': f'{round(lo)} px/m' if round(lo) == round(hi) else f'{round(lo)} to {round(hi)} px/m'}
|
|
624
|
+
else:
|
|
625
|
+
texel = {'min': None, 'max': None, 'note': 'no textures'}
|
|
626
|
+
return missing, texel
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
def score(objects, doc, masks):
|
|
630
|
+
klass = ARGS.get('class') or 'prop'
|
|
631
|
+
expect = ARGS.get('expect') or None
|
|
632
|
+
depsgraph = bpy.context.evaluated_depsgraph_get()
|
|
633
|
+
geo = nw_rubric.geometry(objects, depsgraph)
|
|
634
|
+
if masks:
|
|
635
|
+
sil = nw_rubric.silhouette({name: nw_rubric.mask_stats(masks[name]) for name, _ in MASK_VIEWS if name in masks})
|
|
636
|
+
else:
|
|
637
|
+
why = 'render: false' if not ARGS.get('render', True) else 'the masks could not be rendered'
|
|
638
|
+
sil = {'score': None, 'note': why}
|
|
639
|
+
parts = {
|
|
640
|
+
'silhouette': sil,
|
|
641
|
+
'bevel': nw_rubric.bevel(geo),
|
|
642
|
+
'materials': nw_rubric.materials(material_infos(doc['materials']), klass),
|
|
643
|
+
'budget': nw_rubric.budget(doc['triangles'], klass, ARGS.get('budget')),
|
|
644
|
+
}
|
|
645
|
+
missing, texel = uv_and_texel(objects)
|
|
646
|
+
rubric = dict(parts)
|
|
647
|
+
rubric['class'] = klass
|
|
648
|
+
rubric['expect'] = expect
|
|
649
|
+
rubric['geometry'] = geo
|
|
650
|
+
rubric['checks'] = nw_rubric.checks(doc['size_m'], doc['bounds'], expect, geo, missing, texel)
|
|
651
|
+
rubric['fixFirst'] = nw_rubric.fix_first(parts, klass)
|
|
652
|
+
return rubric
|
|
653
|
+
|
|
654
|
+
|
|
655
|
+
def main():
|
|
656
|
+
objects = import_glb()
|
|
657
|
+
bounds, triangles, meshes = measure(objects)
|
|
658
|
+
materials, textures = materials_of(objects)
|
|
659
|
+
doc = {
|
|
660
|
+
'ok': True,
|
|
661
|
+
'bounds': bounds,
|
|
662
|
+
'triangles': triangles,
|
|
663
|
+
'meshes': meshes,
|
|
664
|
+
'objects': len(objects),
|
|
665
|
+
'materials': materials,
|
|
666
|
+
'textures': textures,
|
|
667
|
+
'animations': [a.name for a in bpy.data.actions],
|
|
668
|
+
'engine': None,
|
|
669
|
+
'sheets': None,
|
|
670
|
+
'cells': [],
|
|
671
|
+
'rubric': None,
|
|
672
|
+
}
|
|
673
|
+
if bounds is not None:
|
|
674
|
+
lo, hi = bounds['min'], bounds['max']
|
|
675
|
+
doc['size_m'] = {'width': hi[0] - lo[0], 'height': hi[2] - lo[2], 'depth': hi[1] - lo[1]}
|
|
676
|
+
rendering = bounds is not None and ARGS.get('render', True)
|
|
677
|
+
if rendering:
|
|
678
|
+
os.makedirs(ARGS['out_dir'], exist_ok=True)
|
|
679
|
+
if bounds is not None:
|
|
680
|
+
# The masks first, before the figure and the studio light join the scene.
|
|
681
|
+
try:
|
|
682
|
+
masks = render_masks((bounds['min'], bounds['max'])) if rendering else {}
|
|
683
|
+
doc['rubric'] = score(objects, doc, masks)
|
|
684
|
+
except Exception as e:
|
|
685
|
+
WARNINGS.append(f'the rubric could not be scored ({type(e).__name__}: {e})')
|
|
686
|
+
if not rendering:
|
|
687
|
+
if bounds is None:
|
|
688
|
+
WARNINGS.append('the GLB has no mesh to measure or render')
|
|
689
|
+
write_result(doc)
|
|
690
|
+
return
|
|
691
|
+
|
|
692
|
+
model_box = (bounds['min'], bounds['max'])
|
|
693
|
+
centre = [(bounds['min'][i] + bounds['max'][i]) / 2 for i in range(3)]
|
|
694
|
+
radius = (Vector(bounds['max']) - Vector(bounds['min'])).length / 2
|
|
695
|
+
figure = make_figure(bounds)
|
|
696
|
+
backdrop = stage_world(radius, centre)
|
|
697
|
+
clay = principled(bpy.data.materials.new('nw_clay'), (0.8, 0.8, 0.8, 1), 0.6)
|
|
698
|
+
cam = bpy.data.objects.new('nw_camera', bpy.data.cameras.new('nw_camera'))
|
|
699
|
+
bpy.context.scene.collection.objects.link(cam)
|
|
700
|
+
bpy.context.scene.camera = cam
|
|
701
|
+
# Workbench draws a material's viewport colour, not its node tree.
|
|
702
|
+
for mat in bpy.data.materials:
|
|
703
|
+
info = next((m for m in materials if m['name'] == mat.name and m.get('color')), None)
|
|
704
|
+
if info:
|
|
705
|
+
h = info['color']
|
|
706
|
+
mat.diffuse_color = tuple(srgb_to_linear(int(h[i:i + 2], 16) / 255) for i in (1, 3, 5)) + (1.0,)
|
|
707
|
+
|
|
708
|
+
tried = []
|
|
709
|
+
for engine in ARGS.get('engines') or ENGINES:
|
|
710
|
+
# The first EEVEE id the scene accepts is the one this Blender has;
|
|
711
|
+
# when it cannot render, the other id will not either.
|
|
712
|
+
if engine.startswith('BLENDER_EEVEE') and any(t.startswith('BLENDER_EEVEE') for t in tried):
|
|
713
|
+
continue
|
|
714
|
+
if not set_engine(engine):
|
|
715
|
+
continue
|
|
716
|
+
tried.append(engine)
|
|
717
|
+
try:
|
|
718
|
+
cells = render_cells(engine, cam, model_box, figure, clay, backdrop)
|
|
719
|
+
except Exception as e:
|
|
720
|
+
WARNINGS.append(f'{engine} could not render ({e}); trying the next engine')
|
|
721
|
+
continue
|
|
722
|
+
size = int(ARGS['size'])
|
|
723
|
+
color, color_small = compose(cells, 'color', size)
|
|
724
|
+
clay_sheet, clay_small = compose(cells, 'clay', size)
|
|
725
|
+
doc['engine'] = engine
|
|
726
|
+
doc['sheets'] = {'color': color, 'clay': clay_sheet, 'color_model': color_small, 'clay_model': clay_small}
|
|
727
|
+
doc['cells'] = list(cells.values())
|
|
728
|
+
write_result(doc)
|
|
729
|
+
return
|
|
730
|
+
doc['ok'] = False
|
|
731
|
+
doc['stage'] = 'render'
|
|
732
|
+
doc['error'] = 'no render engine worked: ' + '; '.join(WARNINGS[-3:])
|
|
733
|
+
write_result(doc)
|
|
734
|
+
sys.exit(3)
|
|
735
|
+
|
|
736
|
+
|
|
737
|
+
try:
|
|
738
|
+
main()
|
|
739
|
+
except SystemExit:
|
|
740
|
+
raise
|
|
741
|
+
except BaseException as e:
|
|
742
|
+
write_result({'ok': False, 'stage': 'sheet', 'error': f'{type(e).__name__}: {e}', 'traceback': traceback.format_exc()})
|
|
743
|
+
raise
|