py-materials 3.6.0__tar.gz → 3.7.0__tar.gz
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.
- {py_materials-3.6.0 → py_materials-3.7.0}/PKG-INFO +38 -1
- {py_materials-3.6.0 → py_materials-3.7.0}/README.md +37 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/pyproject.toml +1 -1
- {py_materials-3.6.0 → py_materials-3.7.0}/src/pymat/__init__.py +1 -1
- {py_materials-3.6.0 → py_materials-3.7.0}/src/pymat/core.py +115 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/src/pymat/vis/_model.py +47 -19
- py_materials-3.7.0/tests/test_material_copy.py +325 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/tests/test_readme_examples.py +42 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/tests/test_vis_override.py +205 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/.gitignore +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/LICENSE +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/docs/catalog/README.md +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/docs/catalog/ceramics/README.md +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/docs/catalog/electronics/README.md +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/docs/catalog/gases/README.md +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/docs/catalog/liquids/README.md +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/docs/catalog/metals/README.md +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/docs/catalog/plastics/README.md +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/docs/catalog/scintillators/README.md +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/docs/decisions/README.md +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/mat-rs/data/README.md +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/src/pymat/data/ceramics.toml +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/src/pymat/data/electronics.toml +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/src/pymat/data/gases.toml +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/src/pymat/data/liquids.toml +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/src/pymat/data/metals.toml +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/src/pymat/data/plastics.toml +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/src/pymat/data/scintillators.toml +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/src/pymat/elements.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/src/pymat/enrichers.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/src/pymat/factories.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/src/pymat/loader.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/src/pymat/properties.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/src/pymat/registry.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/src/pymat/search.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/src/pymat/units.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/src/pymat/vis/__init__.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/src/pymat/vis/adapters.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/tests/__init__.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/tests/_visual_compare.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/tests/baselines/README.md +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/tests/conftest.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/tests/material_preview.html +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/tests/test_adapters.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/tests/test_apply_to.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/tests/test_build123d_integration_examples.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/tests/test_catalog.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/tests/test_core.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/tests/test_e2e_vis.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/tests/test_enrichers.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/tests/test_integration.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/tests/test_loader.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/tests/test_lookup.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/tests/test_properties.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/tests/test_search.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/tests/test_toml_integrity.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/tests/test_uncertainties.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/tests/test_vis.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/tests/test_vis_inheritance.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/tests/test_visual_compare.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/tests/test_visual_regression.py +0 -0
- {py_materials-3.6.0 → py_materials-3.7.0}/tests/visual_render.html +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: py-materials
|
|
3
|
-
Version: 3.
|
|
3
|
+
Version: 3.7.0
|
|
4
4
|
Summary: Hierarchical material library for CAD applications with build123d integration
|
|
5
5
|
Project-URL: Homepage, https://github.com/MorePet/py-mat
|
|
6
6
|
Project-URL: Repository, https://github.com/MorePet/py-mat
|
|
@@ -245,6 +245,43 @@ lyso_crystal = lyso
|
|
|
245
245
|
assert "LYSO" in lyso_crystal.name
|
|
246
246
|
```
|
|
247
247
|
|
|
248
|
+
## Customizing Appearance Safely
|
|
249
|
+
|
|
250
|
+
Materials reached via `pymat["..."]` or category imports
|
|
251
|
+
(`from pymat import stainless`) are *shared instances* — the
|
|
252
|
+
same object every caller in the process sees. Mutating
|
|
253
|
+
`m.vis.roughness = 0.6` or flipping `m.vis.finish` on those
|
|
254
|
+
leaks into every other consumer.
|
|
255
|
+
|
|
256
|
+
The safe pattern uses two methods that pair cleanly:
|
|
257
|
+
|
|
258
|
+
```python
|
|
259
|
+
import pymat
|
|
260
|
+
from pymat.vis import to_threejs
|
|
261
|
+
|
|
262
|
+
steel = pymat["Stainless Steel 304"] # registry singleton — DON'T mutate
|
|
263
|
+
|
|
264
|
+
# Derive an independent variant (1) and attach it to a fresh Material (2)
|
|
265
|
+
polished_vis = steel.vis.override(roughness=0.05, finish="polished")
|
|
266
|
+
shiny = steel.with_vis(polished_vis)
|
|
267
|
+
|
|
268
|
+
three = to_threejs(shiny) # uses the polished values
|
|
269
|
+
assert three["roughness"] == 0.05
|
|
270
|
+
assert steel.vis.material_id == "Metal012" # registry untouched
|
|
271
|
+
|
|
272
|
+
# ``Vis.override(**deltas) -> Vis`` → deep-copied Vis with deltas
|
|
273
|
+
# (finishes deep-copied; identity changes invalidate the texture
|
|
274
|
+
# cache atomically; unknown kwargs raise TypeError).
|
|
275
|
+
# ``Material.with_vis(vis) -> Material`` → registry-detached Material
|
|
276
|
+
# with the supplied Vis attached. Equivalent to ``Material.copy()``
|
|
277
|
+
# followed by slotting the new vis in.
|
|
278
|
+
# ``Material.copy() -> Material`` → generic detach if you need to
|
|
279
|
+
# tweak more than just the Vis.
|
|
280
|
+
|
|
281
|
+
# Materials *you* construct directly (Material(name="custom",
|
|
282
|
+
# vis={...})) are not shared and can be mutated freely.
|
|
283
|
+
```
|
|
284
|
+
|
|
248
285
|
## Physics Properties vs Visualization
|
|
249
286
|
|
|
250
287
|
Understand the difference between measured optical properties (physics)
|
|
@@ -211,6 +211,43 @@ lyso_crystal = lyso
|
|
|
211
211
|
assert "LYSO" in lyso_crystal.name
|
|
212
212
|
```
|
|
213
213
|
|
|
214
|
+
## Customizing Appearance Safely
|
|
215
|
+
|
|
216
|
+
Materials reached via `pymat["..."]` or category imports
|
|
217
|
+
(`from pymat import stainless`) are *shared instances* — the
|
|
218
|
+
same object every caller in the process sees. Mutating
|
|
219
|
+
`m.vis.roughness = 0.6` or flipping `m.vis.finish` on those
|
|
220
|
+
leaks into every other consumer.
|
|
221
|
+
|
|
222
|
+
The safe pattern uses two methods that pair cleanly:
|
|
223
|
+
|
|
224
|
+
```python
|
|
225
|
+
import pymat
|
|
226
|
+
from pymat.vis import to_threejs
|
|
227
|
+
|
|
228
|
+
steel = pymat["Stainless Steel 304"] # registry singleton — DON'T mutate
|
|
229
|
+
|
|
230
|
+
# Derive an independent variant (1) and attach it to a fresh Material (2)
|
|
231
|
+
polished_vis = steel.vis.override(roughness=0.05, finish="polished")
|
|
232
|
+
shiny = steel.with_vis(polished_vis)
|
|
233
|
+
|
|
234
|
+
three = to_threejs(shiny) # uses the polished values
|
|
235
|
+
assert three["roughness"] == 0.05
|
|
236
|
+
assert steel.vis.material_id == "Metal012" # registry untouched
|
|
237
|
+
|
|
238
|
+
# ``Vis.override(**deltas) -> Vis`` → deep-copied Vis with deltas
|
|
239
|
+
# (finishes deep-copied; identity changes invalidate the texture
|
|
240
|
+
# cache atomically; unknown kwargs raise TypeError).
|
|
241
|
+
# ``Material.with_vis(vis) -> Material`` → registry-detached Material
|
|
242
|
+
# with the supplied Vis attached. Equivalent to ``Material.copy()``
|
|
243
|
+
# followed by slotting the new vis in.
|
|
244
|
+
# ``Material.copy() -> Material`` → generic detach if you need to
|
|
245
|
+
# tweak more than just the Vis.
|
|
246
|
+
|
|
247
|
+
# Materials *you* construct directly (Material(name="custom",
|
|
248
|
+
# vis={...})) are not shared and can be mutated freely.
|
|
249
|
+
```
|
|
250
|
+
|
|
214
251
|
## Physics Properties vs Visualization
|
|
215
252
|
|
|
216
253
|
Understand the difference between measured optical properties (physics)
|
|
@@ -410,6 +410,121 @@ class _MaterialInternal:
|
|
|
410
410
|
|
|
411
411
|
return obj
|
|
412
412
|
|
|
413
|
+
def copy(self) -> "Material":
|
|
414
|
+
"""Return a detached copy of this Material.
|
|
415
|
+
|
|
416
|
+
Materials reached via ``pymat["..."]`` or category imports
|
|
417
|
+
(``from pymat import stainless``) are *shared instances* — the
|
|
418
|
+
same object every caller in the process sees. Mutating
|
|
419
|
+
``m.properties.mechanical.density``, reassigning ``m.vis``, or
|
|
420
|
+
flipping ``m.vis.finish`` on those leaks into every other
|
|
421
|
+
consumer. ``copy`` returns an independent instance — same name
|
|
422
|
+
/ grade / properties / vis — that can be tweaked freely.
|
|
423
|
+
|
|
424
|
+
Materials *you* construct (``Material(name="custom", ...)``)
|
|
425
|
+
are not shared and need no copy. The hazard only applies to
|
|
426
|
+
registry instances.
|
|
427
|
+
|
|
428
|
+
Detached:
|
|
429
|
+
|
|
430
|
+
- ``_vis`` deep-copied AND its texture cache zeroed (deepcopy
|
|
431
|
+
bypasses ``Vis.__post_init__``, so the cache is cleared
|
|
432
|
+
explicitly here)
|
|
433
|
+
- ``properties`` deep-copied
|
|
434
|
+
- all property-group kwarg dicts (mechanical, thermal, …) deep-copied
|
|
435
|
+
|
|
436
|
+
Preserved by reference (intentionally — these are part of the
|
|
437
|
+
registry hierarchy, not per-instance state):
|
|
438
|
+
|
|
439
|
+
- ``parent`` (the registry parent the copy still belongs under).
|
|
440
|
+
Note: mutating ``c.parent.<anything>`` still affects the
|
|
441
|
+
registry. Don't.
|
|
442
|
+
- ``_children`` dict — empty for the copy (a detached node has
|
|
443
|
+
no children of its own; it's a leaf variant). For parent-level
|
|
444
|
+
materials this means ``stainless.copy().s304`` raises.
|
|
445
|
+
- ``_key`` cleared so it can't be confused with the registry entry
|
|
446
|
+
|
|
447
|
+
For just changing appearance, prefer
|
|
448
|
+
:meth:`with_vis` + :meth:`Vis.override` — single-shot and clearer.
|
|
449
|
+
"""
|
|
450
|
+
from copy import deepcopy
|
|
451
|
+
|
|
452
|
+
new = type(self)(name=self.name)
|
|
453
|
+
# Identity / hierarchy metadata
|
|
454
|
+
new.formula = self.formula
|
|
455
|
+
new.composition = deepcopy(self.composition)
|
|
456
|
+
new.color = self.color
|
|
457
|
+
new.grade = self.grade
|
|
458
|
+
new.temper = self.temper
|
|
459
|
+
new.treatment = self.treatment
|
|
460
|
+
new.vendor = self.vendor
|
|
461
|
+
# Property-group kwarg dicts — preserve so re-init is consistent
|
|
462
|
+
new.mechanical = deepcopy(self.mechanical)
|
|
463
|
+
new.thermal = deepcopy(self.thermal)
|
|
464
|
+
new.electrical = deepcopy(self.electrical)
|
|
465
|
+
new.optical = deepcopy(self.optical)
|
|
466
|
+
new.manufacturing = deepcopy(self.manufacturing)
|
|
467
|
+
new.compliance = deepcopy(self.compliance)
|
|
468
|
+
new.sourcing = deepcopy(self.sourcing)
|
|
469
|
+
# Independent properties + vis. ``deepcopy`` bypasses
|
|
470
|
+
# ``Vis.__post_init__`` (it goes through ``__reduce_ex__``), so
|
|
471
|
+
# the texture cache must be zeroed explicitly. Otherwise the copy
|
|
472
|
+
# carries duplicated texture bytes (waste, not a leak — the deep
|
|
473
|
+
# copy is independent of the parent — but the docstring promises
|
|
474
|
+
# a clean cache so we deliver it).
|
|
475
|
+
new.properties = deepcopy(self.properties)
|
|
476
|
+
if self._vis is not None:
|
|
477
|
+
new_vis = deepcopy(self._vis)
|
|
478
|
+
# ``deepcopy`` bypasses ``Vis.__post_init__`` (it goes through
|
|
479
|
+
# ``__reduce_ex__``), so the texture cache must be zeroed
|
|
480
|
+
# explicitly. ``object.__setattr__`` skips the identity-
|
|
481
|
+
# invalidation hook in ``Vis.__setattr__`` (which is fine here
|
|
482
|
+
# — we're not changing identity, just resetting cache).
|
|
483
|
+
object.__setattr__(new_vis, "_textures", {})
|
|
484
|
+
object.__setattr__(new_vis, "_fetched", False)
|
|
485
|
+
new._vis = new_vis
|
|
486
|
+
else:
|
|
487
|
+
new._vis = None
|
|
488
|
+
# Registry decoupling
|
|
489
|
+
new.parent = self.parent # stays registry-aware for fallback chain
|
|
490
|
+
new._children = {}
|
|
491
|
+
new._key = None
|
|
492
|
+
return new
|
|
493
|
+
|
|
494
|
+
def with_vis(self, vis: "Vis") -> "Material": # noqa: F821 — forward ref
|
|
495
|
+
"""Return a detached copy with the given Vis attached.
|
|
496
|
+
|
|
497
|
+
The canonical safe path for "I want this material with a tweaked
|
|
498
|
+
appearance" — pairs cleanly with :meth:`Vis.override`::
|
|
499
|
+
|
|
500
|
+
steel = pymat["Stainless Steel 304"]
|
|
501
|
+
shiny = steel.with_vis(steel.vis.override(roughness=0.05, finish="polished"))
|
|
502
|
+
pymat.vis.to_threejs(shiny)
|
|
503
|
+
# steel is the registry singleton; shiny is independent.
|
|
504
|
+
|
|
505
|
+
Closes the parent-singleton hazard: ``m.vis = ...`` directly on
|
|
506
|
+
a registry instance still mutates the shared object. ``with_vis``
|
|
507
|
+
returns a fresh Material whose ``vis`` slot is the new value,
|
|
508
|
+
leaving the registry untouched.
|
|
509
|
+
|
|
510
|
+
The supplied ``vis`` is deep-copied so passing in a registry
|
|
511
|
+
instance's own ``vis`` (e.g. ``m.with_vis(m.vis)``) does not
|
|
512
|
+
re-alias the singleton. Always returns an independent Vis.
|
|
513
|
+
"""
|
|
514
|
+
from copy import deepcopy
|
|
515
|
+
|
|
516
|
+
from pymat.vis._model import Vis
|
|
517
|
+
|
|
518
|
+
if not isinstance(vis, Vis):
|
|
519
|
+
raise TypeError(f"with_vis expects a Vis instance, got {type(vis).__name__}")
|
|
520
|
+
new = self.copy()
|
|
521
|
+
# Deep-copy the supplied vis so callers passing the registry's
|
|
522
|
+
# own ``m.vis`` don't re-alias the singleton. ``override`` already
|
|
523
|
+
# returns an independent Vis, but we cannot rely on the caller
|
|
524
|
+
# having gone through it.
|
|
525
|
+
new._vis = deepcopy(vis)
|
|
526
|
+
return new
|
|
527
|
+
|
|
413
528
|
# =========================================================================
|
|
414
529
|
# Information & Inspection
|
|
415
530
|
# =========================================================================
|
|
@@ -98,7 +98,6 @@ class Vis:
|
|
|
98
98
|
steel.vis.material_id # "Metal012"
|
|
99
99
|
steel.vis.tier # "1k"
|
|
100
100
|
steel.vis.finishes # {"brushed": {"source": ..., "id": ...}, ...}
|
|
101
|
-
steel.vis.finish = "polished" # switch appearance
|
|
102
101
|
|
|
103
102
|
Material-keyed delegates (ADR-0002)::
|
|
104
103
|
|
|
@@ -112,6 +111,21 @@ class Vis:
|
|
|
112
111
|
import pymat
|
|
113
112
|
d = pymat.vis.to_threejs(steel) # MeshPhysicalMaterial init dict
|
|
114
113
|
|
|
114
|
+
**Don't mutate the Vis on a registry Material.** Materials reached
|
|
115
|
+
via ``pymat["..."]`` or category imports (``from pymat import
|
|
116
|
+
stainless``) are *shared instances* — the same object every caller
|
|
117
|
+
in the process sees. Writing ``steel.vis.finish = "polished"`` on
|
|
118
|
+
one of those leaks into every other consumer.
|
|
119
|
+
|
|
120
|
+
The safe path: derive an independent copy via :meth:`override`,
|
|
121
|
+
attach it to a fresh Material via :meth:`Material.with_vis`::
|
|
122
|
+
|
|
123
|
+
polished_vis = steel.vis.override(finish="polished", roughness=0.05)
|
|
124
|
+
steel_polished = steel.with_vis(polished_vis)
|
|
125
|
+
|
|
126
|
+
Materials *you* construct directly (``Material(name="custom",
|
|
127
|
+
vis={...})``) are not shared and can be mutated freely.
|
|
128
|
+
|
|
115
129
|
Assigning ``source``, ``material_id``, or ``tier`` invalidates the
|
|
116
130
|
lazy texture cache automatically — the next ``.textures`` access
|
|
117
131
|
will re-fetch for the new identity.
|
|
@@ -328,17 +342,24 @@ class Vis:
|
|
|
328
342
|
polished = steel.vis.override(roughness=0.3, finish="polished")
|
|
329
343
|
# steel.vis is unchanged; polished is independent.
|
|
330
344
|
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
345
|
+
Note: ``override`` is the runtime mirror of TOML grade override
|
|
346
|
+
(children inherit parent properties unless overridden). It is
|
|
347
|
+
NOT a flat field substitution like ``dataclasses.replace`` —
|
|
348
|
+
the ``finishes`` map is deep-copied (including caller-supplied
|
|
349
|
+
deltas), ``finish=`` is a special-cased lookup that flips
|
|
350
|
+
identity via the finishes map, and identity changes invalidate
|
|
351
|
+
the texture cache atomically.
|
|
335
352
|
|
|
336
353
|
Identity deltas (``source`` / ``material_id`` / ``tier``) route
|
|
337
|
-
through :meth:`set_identity` for atomic invalidation.
|
|
338
|
-
|
|
339
|
-
``
|
|
340
|
-
|
|
341
|
-
|
|
354
|
+
through :meth:`set_identity` for atomic invalidation. The
|
|
355
|
+
``_finish`` label is cleared only when ``source`` or
|
|
356
|
+
``material_id`` change without an explicit ``finish=`` —
|
|
357
|
+
finishes pin (source, material_id), not tier, so a tier-only
|
|
358
|
+
change preserves the finish label.
|
|
359
|
+
|
|
360
|
+
``finish=`` runs LAST against the new (deep-copied) finishes
|
|
361
|
+
map, so ``override(finishes={...}, finish="x")`` resolves
|
|
362
|
+
``x`` in the replaced map.
|
|
342
363
|
|
|
343
364
|
Unknown kwargs raise ``TypeError`` — catches typos like
|
|
344
365
|
``roughnes=0.5`` that ``dataclasses.replace`` would accept
|
|
@@ -366,21 +387,28 @@ class Vis:
|
|
|
366
387
|
finish_delta = deltas.pop("finish", None)
|
|
367
388
|
|
|
368
389
|
# Identity-pair updates: route through set_identity for atomic
|
|
369
|
-
# cache invalidation. Compute "
|
|
370
|
-
#
|
|
371
|
-
#
|
|
372
|
-
|
|
373
|
-
|
|
390
|
+
# cache invalidation. Compute "finish-invalidating change" from
|
|
391
|
+
# ``source``/``material_id`` only — tier doesn't pin which
|
|
392
|
+
# finish entry is selected (#103). Computed before set_identity
|
|
393
|
+
# writes so a no-op (same value) doesn't trigger the clear.
|
|
394
|
+
identity_keys = _IDENTITY_FIELDS & deltas.keys()
|
|
395
|
+
finish_invalidating_keys = identity_keys & {"source", "material_id"}
|
|
396
|
+
finish_invalidating = any(deltas[k] != getattr(new, k) for k in finish_invalidating_keys)
|
|
374
397
|
if identity_keys:
|
|
375
398
|
new.set_identity(**{k: deltas.pop(k) for k in list(identity_keys)})
|
|
376
399
|
|
|
377
400
|
for k, v in deltas.items():
|
|
401
|
+
# Caller-supplied ``finishes=`` must be deep-copied — storing
|
|
402
|
+
# by reference breaks the docstring promise and diverges from
|
|
403
|
+
# ``merge_from_toml`` semantics (#104).
|
|
404
|
+
if k == "finishes":
|
|
405
|
+
v = deepcopy(v)
|
|
378
406
|
setattr(new, k, v)
|
|
379
407
|
|
|
380
|
-
# Identity moved without an explicit
|
|
381
|
-
# _finish label is now stale.
|
|
382
|
-
if
|
|
383
|
-
|
|
408
|
+
# Identity moved (source/material_id) without an explicit
|
|
409
|
+
# finish= → the inherited _finish label is now stale.
|
|
410
|
+
if finish_invalidating and finish_delta is None:
|
|
411
|
+
object.__setattr__(new, "_finish", None)
|
|
384
412
|
|
|
385
413
|
if finish_delta is not None:
|
|
386
414
|
new.finish = finish_delta
|
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
"""Tests for ``Material.copy()`` and ``Material.with_vis(...)`` (#109).
|
|
2
|
+
|
|
3
|
+
The motivating bug: ``pymat["..."]`` returns the registry singleton.
|
|
4
|
+
Even after `Vis.override` insulated the visual side, users still had
|
|
5
|
+
to do ``m.vis = m.vis.override(...)`` — mutating the *Material*
|
|
6
|
+
singleton (the new pitfall one level up). ``copy`` and ``with_vis``
|
|
7
|
+
close that hazard:
|
|
8
|
+
|
|
9
|
+
shiny = m.with_vis(m.vis.override(roughness=0.05, finish="polished"))
|
|
10
|
+
|
|
11
|
+
Pinned semantics:
|
|
12
|
+
|
|
13
|
+
- ``copy()`` returns a Material whose vis / properties / property-group
|
|
14
|
+
dicts are deep-copied; identity metadata (name, grade, etc.) preserved.
|
|
15
|
+
- The copy is registry-detached: ``_key`` is None, ``_children`` is {},
|
|
16
|
+
``parent`` retained for inheritance chain only.
|
|
17
|
+
- ``with_vis(vis)`` returns ``copy()`` with the new ``_vis`` attached.
|
|
18
|
+
- Adapters operating on the copy reflect the new state, not the registry.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
from __future__ import annotations
|
|
22
|
+
|
|
23
|
+
import pytest
|
|
24
|
+
|
|
25
|
+
from pymat import Material
|
|
26
|
+
from pymat.vis._model import Vis
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class TestCopyReturnsIndependentInstance:
|
|
30
|
+
def test_returns_new_instance(self):
|
|
31
|
+
m = Material(name="probe", density=7.8)
|
|
32
|
+
c = m.copy()
|
|
33
|
+
assert c is not m
|
|
34
|
+
|
|
35
|
+
def test_identity_metadata_preserved(self):
|
|
36
|
+
m = Material(name="Stainless 304", grade="304", formula="FeCr18Ni8")
|
|
37
|
+
c = m.copy()
|
|
38
|
+
assert c.name == "Stainless 304"
|
|
39
|
+
assert c.grade == "304"
|
|
40
|
+
assert c.formula == "FeCr18Ni8"
|
|
41
|
+
|
|
42
|
+
def test_properties_independent(self):
|
|
43
|
+
m = Material(name="probe", density=7.8)
|
|
44
|
+
c = m.copy()
|
|
45
|
+
c.properties.mechanical.density = 12.0
|
|
46
|
+
assert m.properties.mechanical.density == 7.8
|
|
47
|
+
assert c.properties.mechanical.density == 12.0
|
|
48
|
+
|
|
49
|
+
def test_vis_independent(self):
|
|
50
|
+
m = Material(name="probe")
|
|
51
|
+
m.vis.roughness = 0.3
|
|
52
|
+
m.vis.metallic = 1.0
|
|
53
|
+
c = m.copy()
|
|
54
|
+
c.vis.roughness = 0.9
|
|
55
|
+
assert m.vis.roughness == 0.3
|
|
56
|
+
assert c.vis.roughness == 0.9
|
|
57
|
+
|
|
58
|
+
def test_vis_finishes_independent(self):
|
|
59
|
+
"""Deep-copy reaches into nested vis state."""
|
|
60
|
+
m = Material(name="probe")
|
|
61
|
+
m.vis.finishes = {"a": {"source": "x", "id": "y"}}
|
|
62
|
+
c = m.copy()
|
|
63
|
+
c.vis.finishes["b"] = {"source": "z", "id": "w"}
|
|
64
|
+
assert "b" not in m.vis.finishes
|
|
65
|
+
assert "b" in c.vis.finishes
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class TestCopyRegistryDetachment:
|
|
69
|
+
def test_key_cleared_on_copy(self):
|
|
70
|
+
import pymat
|
|
71
|
+
|
|
72
|
+
steel = pymat["Stainless Steel 304"]
|
|
73
|
+
c = steel.copy()
|
|
74
|
+
assert c._key is None
|
|
75
|
+
# Original still has its registry key
|
|
76
|
+
assert steel._key is not None
|
|
77
|
+
|
|
78
|
+
def test_children_emptied_on_copy(self):
|
|
79
|
+
import pymat
|
|
80
|
+
|
|
81
|
+
# stainless has child grades (s304, s316L, ...)
|
|
82
|
+
stainless = pymat.stainless
|
|
83
|
+
c = stainless.copy()
|
|
84
|
+
assert c._children == {}
|
|
85
|
+
# Original retains its children
|
|
86
|
+
assert len(stainless._children) > 0
|
|
87
|
+
|
|
88
|
+
def test_parent_preserved_for_inheritance_chain(self):
|
|
89
|
+
"""Copy stays parent-aware so property fallbacks still work."""
|
|
90
|
+
import pymat
|
|
91
|
+
|
|
92
|
+
s304 = pymat.stainless.s304
|
|
93
|
+
c = s304.copy()
|
|
94
|
+
assert c.parent is s304.parent # same parent reference
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
class TestRegistryHazardClosed:
|
|
98
|
+
def test_copy_isolates_vis_mutations_from_registry(self):
|
|
99
|
+
import pymat
|
|
100
|
+
|
|
101
|
+
steel = pymat["Stainless Steel 304"]
|
|
102
|
+
original_roughness = steel.vis.roughness
|
|
103
|
+
|
|
104
|
+
c = steel.copy()
|
|
105
|
+
c.vis = c.vis.override(roughness=0.99)
|
|
106
|
+
|
|
107
|
+
# Registry singleton untouched
|
|
108
|
+
assert steel.vis.roughness == original_roughness
|
|
109
|
+
|
|
110
|
+
def test_with_vis_isolates_vis_swap_from_registry(self):
|
|
111
|
+
import pymat
|
|
112
|
+
|
|
113
|
+
steel = pymat["Stainless Steel 304"]
|
|
114
|
+
original_vis = steel.vis # capture identity reference
|
|
115
|
+
|
|
116
|
+
new_vis = steel.vis.override(roughness=0.05, finish="polished")
|
|
117
|
+
shiny = steel.with_vis(new_vis)
|
|
118
|
+
|
|
119
|
+
assert shiny is not steel
|
|
120
|
+
# ``with_vis`` deep-copies the supplied vis (defends against
|
|
121
|
+
# callers passing the registry's own .vis back in). Equality,
|
|
122
|
+
# not identity, is the contract.
|
|
123
|
+
assert shiny.vis == new_vis
|
|
124
|
+
assert shiny.vis is not new_vis
|
|
125
|
+
assert steel.vis is original_vis # registry untouched
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
class TestWithVis:
|
|
129
|
+
def test_returns_new_material(self):
|
|
130
|
+
m = Material(name="probe")
|
|
131
|
+
c = m.with_vis(Vis(source="ambientcg", material_id="Metal012"))
|
|
132
|
+
assert c is not m
|
|
133
|
+
|
|
134
|
+
def test_attaches_provided_vis(self):
|
|
135
|
+
m = Material(name="probe")
|
|
136
|
+
new_vis = Vis(source="ambientcg", material_id="Metal032", tier="2k")
|
|
137
|
+
c = m.with_vis(new_vis)
|
|
138
|
+
# Deep-copied — equal but not the same instance (defends against
|
|
139
|
+
# the registry-singleton aliasing case).
|
|
140
|
+
assert c.vis == new_vis
|
|
141
|
+
assert c.vis is not new_vis
|
|
142
|
+
assert c.vis.source == "ambientcg"
|
|
143
|
+
assert c.vis.material_id == "Metal032"
|
|
144
|
+
|
|
145
|
+
def test_original_vis_unchanged(self):
|
|
146
|
+
m = Material(name="probe")
|
|
147
|
+
m.vis.source = "ambientcg"
|
|
148
|
+
m.vis.material_id = "Metal012"
|
|
149
|
+
c = m.with_vis(Vis(source="polyhaven", material_id="metal_01"))
|
|
150
|
+
assert m.vis.source == "ambientcg"
|
|
151
|
+
assert c.vis.source == "polyhaven"
|
|
152
|
+
|
|
153
|
+
def test_rejects_non_vis(self):
|
|
154
|
+
m = Material(name="probe")
|
|
155
|
+
with pytest.raises(TypeError, match="Vis instance"):
|
|
156
|
+
m.with_vis({"source": "ambientcg", "material_id": "Metal012"}) # dict, not Vis
|
|
157
|
+
|
|
158
|
+
def test_preserves_other_material_state(self):
|
|
159
|
+
m = Material(name="probe", density=7.8, grade="304")
|
|
160
|
+
c = m.with_vis(Vis(source="ambientcg", material_id="Metal012"))
|
|
161
|
+
assert c.name == "probe"
|
|
162
|
+
assert c.grade == "304"
|
|
163
|
+
assert c.properties.mechanical.density == 7.8
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
class TestAdapterRoundTripViaWithVis:
|
|
167
|
+
"""The headline user story: derive a tweaked variant, hand to an
|
|
168
|
+
adapter, get the new values out."""
|
|
169
|
+
|
|
170
|
+
def test_to_threejs_reflects_new_vis(self):
|
|
171
|
+
from pymat.vis.adapters import to_threejs
|
|
172
|
+
|
|
173
|
+
m = Material(name="probe")
|
|
174
|
+
m.vis.roughness = 0.3
|
|
175
|
+
m.vis.metallic = 1.0
|
|
176
|
+
|
|
177
|
+
new_vis = m.vis.override(roughness=0.7, metallic=0.0)
|
|
178
|
+
c = m.with_vis(new_vis)
|
|
179
|
+
|
|
180
|
+
d = to_threejs(c)
|
|
181
|
+
assert d["roughness"] == 0.7
|
|
182
|
+
assert d["metalness"] == 0.0
|
|
183
|
+
|
|
184
|
+
def test_original_material_adapter_unchanged(self):
|
|
185
|
+
from pymat.vis.adapters import to_threejs
|
|
186
|
+
|
|
187
|
+
m = Material(name="probe")
|
|
188
|
+
m.vis.roughness = 0.3
|
|
189
|
+
m.vis.metallic = 1.0
|
|
190
|
+
|
|
191
|
+
c = m.with_vis(m.vis.override(roughness=0.7, metallic=0.0))
|
|
192
|
+
_ = to_threejs(c) # noqa: F841 — exercising the adapter
|
|
193
|
+
|
|
194
|
+
d_orig = to_threejs(m)
|
|
195
|
+
assert d_orig["roughness"] == 0.3
|
|
196
|
+
assert d_orig["metalness"] == 1.0
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
class TestRegistryParentChainStillResolves:
|
|
200
|
+
"""Detached copies keep their parent so property inheritance
|
|
201
|
+
fallbacks still work — important for CAD users who derive a
|
|
202
|
+
grade-level variant but expect ``c.density`` to still resolve."""
|
|
203
|
+
|
|
204
|
+
def test_density_via_parent_chain(self):
|
|
205
|
+
import pymat
|
|
206
|
+
|
|
207
|
+
s304 = pymat.stainless.s304
|
|
208
|
+
c = s304.copy()
|
|
209
|
+
# The grade may not own density itself; parent chain provides it
|
|
210
|
+
assert c.properties.mechanical.density == s304.properties.mechanical.density
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
class TestVisCacheZeroedOnCopy:
|
|
214
|
+
"""``copy()`` deep-copies ``_vis``. ``deepcopy`` does NOT call
|
|
215
|
+
``Vis.__post_init__`` (it goes through ``__reduce_ex__``), so the
|
|
216
|
+
texture cache + ``_fetched`` flag would carry over without an
|
|
217
|
+
explicit zeroing — wasteful and contradicts the docstring."""
|
|
218
|
+
|
|
219
|
+
def test_copy_zeroes_texture_cache(self):
|
|
220
|
+
m = Material(name="cached probe")
|
|
221
|
+
m.vis.source = "ambientcg"
|
|
222
|
+
m.vis.material_id = "Metal012"
|
|
223
|
+
m.vis._textures = {"color": b"\x89PNG_fake_bytes"}
|
|
224
|
+
m.vis._fetched = True
|
|
225
|
+
|
|
226
|
+
c = m.copy()
|
|
227
|
+
assert c.vis._textures == {}
|
|
228
|
+
assert c.vis._fetched is False
|
|
229
|
+
# Original cache untouched
|
|
230
|
+
assert m.vis._textures == {"color": b"\x89PNG_fake_bytes"}
|
|
231
|
+
assert m.vis._fetched is True
|
|
232
|
+
|
|
233
|
+
def test_with_vis_target_starts_fresh(self):
|
|
234
|
+
"""``with_vis`` builds on top of ``copy``, so the texture cache
|
|
235
|
+
on the resulting Material's vis is whatever the supplied vis
|
|
236
|
+
has — but the copy's pre-existing cache is wiped, so there's
|
|
237
|
+
no leftover bytes from the parent."""
|
|
238
|
+
m = Material(name="probe")
|
|
239
|
+
m.vis.source = "ambientcg"
|
|
240
|
+
m.vis.material_id = "Metal012"
|
|
241
|
+
m.vis._textures = {"color": b"\x89PNG_old"}
|
|
242
|
+
m.vis._fetched = True
|
|
243
|
+
|
|
244
|
+
new_vis = m.vis.override(material_id="Metal032")
|
|
245
|
+
c = m.with_vis(new_vis)
|
|
246
|
+
# New vis starts unfetched (override invalidated identity)
|
|
247
|
+
assert c.vis._textures == {}
|
|
248
|
+
assert c.vis._fetched is False
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
class TestWithVisDeepCopiesSuppliedVis:
|
|
252
|
+
"""Bug B2: ``with_vis(vis)`` must deep-copy ``vis`` so callers
|
|
253
|
+
passing the registry's own ``m.vis`` don't get a Material whose
|
|
254
|
+
``_vis`` is re-aliased to the singleton's instance."""
|
|
255
|
+
|
|
256
|
+
def test_supplied_vis_is_deep_copied(self):
|
|
257
|
+
import pymat
|
|
258
|
+
|
|
259
|
+
steel = pymat["Stainless Steel 304"]
|
|
260
|
+
# Caller passes the registry's own vis directly
|
|
261
|
+
c = steel.with_vis(steel.vis)
|
|
262
|
+
assert c.vis is not steel.vis # independent instance
|
|
263
|
+
# Mutating the copy must not leak to the registry
|
|
264
|
+
c.vis.roughness = 0.99
|
|
265
|
+
assert steel.vis.roughness != 0.99
|
|
266
|
+
|
|
267
|
+
def test_supplied_vis_finishes_dict_independent(self):
|
|
268
|
+
m = Material(name="probe")
|
|
269
|
+
m.vis.source = "ambientcg"
|
|
270
|
+
m.vis.material_id = "Metal012"
|
|
271
|
+
m.vis.finishes = {"a": {"source": "ambientcg", "id": "Metal012"}}
|
|
272
|
+
|
|
273
|
+
c = m.with_vis(m.vis)
|
|
274
|
+
c.vis.finishes["b"] = {"source": "x", "id": "y"}
|
|
275
|
+
assert "b" not in m.vis.finishes
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
class TestParentMutationWarning:
|
|
279
|
+
"""``copy()`` preserves ``parent`` by reference for the inheritance
|
|
280
|
+
fallback chain. This means mutating ``c.parent.<anything>`` still
|
|
281
|
+
corrupts the registry — documented as a footgun. Pin the behavior
|
|
282
|
+
so a future "deep-copy parent too" change can't slip through
|
|
283
|
+
silently."""
|
|
284
|
+
|
|
285
|
+
def test_parent_is_same_object_after_copy(self):
|
|
286
|
+
import pymat
|
|
287
|
+
|
|
288
|
+
s304 = pymat.stainless.s304
|
|
289
|
+
c = s304.copy()
|
|
290
|
+
# By design — the copy joins the existing inheritance chain
|
|
291
|
+
assert c.parent is s304.parent
|
|
292
|
+
assert c.parent is pymat.stainless
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
class TestCopyOnNonRegistryMaterial:
|
|
296
|
+
"""``copy()`` of a user-constructed Material is also valid; nothing
|
|
297
|
+
in the implementation requires the source be a registry entry."""
|
|
298
|
+
|
|
299
|
+
def test_custom_material_copy(self):
|
|
300
|
+
m = Material(name="custom probe", density=4.2)
|
|
301
|
+
m.vis.roughness = 0.5
|
|
302
|
+
c = m.copy()
|
|
303
|
+
assert c is not m
|
|
304
|
+
assert c.name == "custom probe"
|
|
305
|
+
assert c.parent is None # custom Material has no parent
|
|
306
|
+
assert c._key is None
|
|
307
|
+
# Independent state
|
|
308
|
+
c.properties.mechanical.density = 9.9
|
|
309
|
+
c.vis.roughness = 0.1
|
|
310
|
+
assert m.properties.mechanical.density == 4.2
|
|
311
|
+
assert m.vis.roughness == 0.5
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
class TestCopyOnParentLevelEmptiesChildren:
|
|
315
|
+
"""``stainless.copy()`` empties ``_children`` — documented behavior.
|
|
316
|
+
A user reaching for ``stainless.copy().s304`` should get a clean
|
|
317
|
+
failure, not a stale registry reference."""
|
|
318
|
+
|
|
319
|
+
def test_parent_copy_has_no_children(self):
|
|
320
|
+
import pymat
|
|
321
|
+
|
|
322
|
+
c = pymat.stainless.copy()
|
|
323
|
+
assert c._children == {}
|
|
324
|
+
with pytest.raises(AttributeError):
|
|
325
|
+
_ = c.s304
|
|
@@ -196,6 +196,48 @@ class TestHierarchicalMaterials:
|
|
|
196
196
|
assert "LYSO" in lyso_crystal.name
|
|
197
197
|
|
|
198
198
|
|
|
199
|
+
class TestCustomizingAppearance:
|
|
200
|
+
"""Examples for the safe pattern of customizing appearance without
|
|
201
|
+
corrupting the registry singleton."""
|
|
202
|
+
|
|
203
|
+
def test_safe_appearance_customization(self):
|
|
204
|
+
"""
|
|
205
|
+
## Customizing Appearance Safely
|
|
206
|
+
|
|
207
|
+
Materials reached via `pymat["..."]` or category imports
|
|
208
|
+
(`from pymat import stainless`) are *shared instances* — the
|
|
209
|
+
same object every caller in the process sees. Mutating
|
|
210
|
+
`m.vis.roughness = 0.6` or flipping `m.vis.finish` on those
|
|
211
|
+
leaks into every other consumer.
|
|
212
|
+
|
|
213
|
+
The safe pattern uses two methods that pair cleanly:
|
|
214
|
+
"""
|
|
215
|
+
import pymat
|
|
216
|
+
from pymat.vis import to_threejs
|
|
217
|
+
|
|
218
|
+
steel = pymat["Stainless Steel 304"] # registry singleton — DON'T mutate
|
|
219
|
+
|
|
220
|
+
# Derive an independent variant (1) and attach it to a fresh Material (2)
|
|
221
|
+
polished_vis = steel.vis.override(roughness=0.05, finish="polished")
|
|
222
|
+
shiny = steel.with_vis(polished_vis)
|
|
223
|
+
|
|
224
|
+
three = to_threejs(shiny) # uses the polished values
|
|
225
|
+
assert three["roughness"] == 0.05
|
|
226
|
+
assert steel.vis.material_id == "Metal012" # registry untouched
|
|
227
|
+
|
|
228
|
+
# ``Vis.override(**deltas) -> Vis`` → deep-copied Vis with deltas
|
|
229
|
+
# (finishes deep-copied; identity changes invalidate the texture
|
|
230
|
+
# cache atomically; unknown kwargs raise TypeError).
|
|
231
|
+
# ``Material.with_vis(vis) -> Material`` → registry-detached Material
|
|
232
|
+
# with the supplied Vis attached. Equivalent to ``Material.copy()``
|
|
233
|
+
# followed by slotting the new vis in.
|
|
234
|
+
# ``Material.copy() -> Material`` → generic detach if you need to
|
|
235
|
+
# tweak more than just the Vis.
|
|
236
|
+
|
|
237
|
+
# Materials *you* construct directly (Material(name="custom",
|
|
238
|
+
# vis={...})) are not shared and can be mutated freely.
|
|
239
|
+
|
|
240
|
+
|
|
199
241
|
class TestOpticalVsVisualization:
|
|
200
242
|
"""Examples showing separation of optical properties and visualization."""
|
|
201
243
|
|
|
@@ -259,3 +259,208 @@ class TestRegistryMutationHazardFixed:
|
|
|
259
259
|
assert "custom_b" in b.finishes and "custom_b" not in a.finishes
|
|
260
260
|
assert "custom_a" not in steel.vis.finishes
|
|
261
261
|
assert "custom_b" not in steel.vis.finishes
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
# ── Bugs caught by independent review of 3.6.0 ─────────────────
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
class TestTierOnlyChangePreservesFinish:
|
|
268
|
+
"""Issue #103. ``tier`` is an identity field but doesn't affect
|
|
269
|
+
which finish-map entry is selected — clearing ``_finish`` on a
|
|
270
|
+
tier-only change is over-eager. The stale-finish guard should
|
|
271
|
+
only fire when ``source`` or ``material_id`` actually changes.
|
|
272
|
+
"""
|
|
273
|
+
|
|
274
|
+
def test_tier_change_keeps_finish_label(self):
|
|
275
|
+
v = _base()
|
|
276
|
+
v.finish = "polished"
|
|
277
|
+
v2 = v.override(tier="2k")
|
|
278
|
+
assert v2._finish == "polished", (
|
|
279
|
+
"tier change must not clear _finish — finishes pin (source, material_id), not tier"
|
|
280
|
+
)
|
|
281
|
+
# Same (source, material_id) as the polished entry → finish remains valid
|
|
282
|
+
assert v2.source == "ambientcg"
|
|
283
|
+
assert v2.material_id == "Metal049A"
|
|
284
|
+
assert v2.tier == "2k"
|
|
285
|
+
|
|
286
|
+
def test_tier_change_still_invalidates_cache(self):
|
|
287
|
+
"""Tier change must clear the texture cache (different bytes
|
|
288
|
+
for 1k vs 2k) but leave the finish *label* alone."""
|
|
289
|
+
v = _base()
|
|
290
|
+
v.finish = "polished"
|
|
291
|
+
v._textures = {"color": b"\x89PNG_1k_bytes"}
|
|
292
|
+
v._fetched = True
|
|
293
|
+
v2 = v.override(tier="2k")
|
|
294
|
+
assert v2._textures == {}
|
|
295
|
+
assert v2._fetched is False
|
|
296
|
+
assert v2._finish == "polished"
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
class TestCallerSuppliedFinishesDeepCopied:
|
|
300
|
+
"""Issue #104. Caller-supplied ``finishes=`` dict must be
|
|
301
|
+
deep-copied; storing by reference breaks the docstring promise
|
|
302
|
+
and diverges from ``merge_from_toml``.
|
|
303
|
+
"""
|
|
304
|
+
|
|
305
|
+
def test_caller_dict_mutation_does_not_leak(self):
|
|
306
|
+
v = _base()
|
|
307
|
+
caller_dict = {"matte": {"source": "x", "id": "y"}}
|
|
308
|
+
v2 = v.override(finishes=caller_dict)
|
|
309
|
+
# Caller mutates their own dict after the call
|
|
310
|
+
caller_dict["matte"]["id"] = "TAMPERED"
|
|
311
|
+
caller_dict["another"] = {"source": "z", "id": "w"}
|
|
312
|
+
# Override result must be insulated
|
|
313
|
+
assert v2.finishes["matte"]["id"] == "y"
|
|
314
|
+
assert "another" not in v2.finishes
|
|
315
|
+
|
|
316
|
+
def test_caller_dict_top_level_isolated(self):
|
|
317
|
+
v = _base()
|
|
318
|
+
caller_dict = {"matte": {"source": "x", "id": "y"}}
|
|
319
|
+
v2 = v.override(finishes=caller_dict)
|
|
320
|
+
assert v2.finishes is not caller_dict
|
|
321
|
+
|
|
322
|
+
def test_finish_resolves_against_new_map(self):
|
|
323
|
+
"""``finish=`` runs after ``finishes=`` is applied, so the
|
|
324
|
+
finish setter looks up in the *new* (deep-copied) map."""
|
|
325
|
+
v = _base()
|
|
326
|
+
new_map = {"matte": {"source": "polyhaven", "id": "metal_matte"}}
|
|
327
|
+
v2 = v.override(finishes=new_map, finish="matte")
|
|
328
|
+
assert v2._finish == "matte"
|
|
329
|
+
assert v2.source == "polyhaven"
|
|
330
|
+
assert v2.material_id == "metal_matte"
|
|
331
|
+
|
|
332
|
+
def test_finish_against_replaced_map_unknown_raises(self):
|
|
333
|
+
"""If the new finishes map doesn't contain the requested
|
|
334
|
+
finish, the finish setter raises (not the inherited map)."""
|
|
335
|
+
v = _base()
|
|
336
|
+
new_map = {"matte": {"source": "x", "id": "y"}}
|
|
337
|
+
# 'polished' was in the original map but not in new_map
|
|
338
|
+
with pytest.raises(ValueError, match="Unknown finish 'polished'"):
|
|
339
|
+
v.override(finishes=new_map, finish="polished")
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
# ── Coverage gaps from review (#105) ──────────────────────────
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
class TestAdapterRoundTrip:
|
|
346
|
+
"""Override must flow into the cross-tool adapters. Otherwise
|
|
347
|
+
a downstream consumer's tweaked variant is silently ignored."""
|
|
348
|
+
|
|
349
|
+
def test_to_threejs_picks_up_overridden_roughness(self):
|
|
350
|
+
from pymat import Material
|
|
351
|
+
from pymat.vis.adapters import to_threejs
|
|
352
|
+
|
|
353
|
+
m = Material(name="adapter probe")
|
|
354
|
+
m.vis.roughness = 0.3
|
|
355
|
+
m.vis.metallic = 1.0
|
|
356
|
+
m.vis.base_color = (0.5, 0.5, 0.5, 1.0)
|
|
357
|
+
|
|
358
|
+
# Mutate only the variant via override
|
|
359
|
+
m2 = Material(name="adapter probe variant")
|
|
360
|
+
new_vis = m.vis.override(roughness=0.7, metallic=0.0)
|
|
361
|
+
m2._vis = new_vis # bypass property to keep test focused on adapter
|
|
362
|
+
|
|
363
|
+
d = to_threejs(m2)
|
|
364
|
+
assert d["roughness"] == 0.7
|
|
365
|
+
assert d["metalness"] == 0.0
|
|
366
|
+
# Original Material's adapter output unchanged
|
|
367
|
+
d_orig = to_threejs(m)
|
|
368
|
+
assert d_orig["roughness"] == 0.3
|
|
369
|
+
assert d_orig["metalness"] == 1.0
|
|
370
|
+
|
|
371
|
+
def test_to_gltf_picks_up_overridden_color(self):
|
|
372
|
+
from pymat import Material
|
|
373
|
+
from pymat.vis.adapters import to_gltf
|
|
374
|
+
|
|
375
|
+
m = Material(name="gltf probe")
|
|
376
|
+
m.vis.base_color = (1.0, 0.0, 0.0, 1.0)
|
|
377
|
+
|
|
378
|
+
v2 = m.vis.override(base_color=(0.0, 1.0, 0.0, 1.0))
|
|
379
|
+
m2 = Material(name="gltf probe variant")
|
|
380
|
+
m2._vis = v2
|
|
381
|
+
|
|
382
|
+
d = to_gltf(m2)
|
|
383
|
+
pbr = d.get("pbrMetallicRoughness", {})
|
|
384
|
+
assert pbr.get("baseColorFactor") == [0.0, 1.0, 0.0, 1.0]
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
class TestScalarReset:
|
|
388
|
+
"""Setting a scalar back to ``None`` via override is allowed —
|
|
389
|
+
consumer's way to undo an inherited value."""
|
|
390
|
+
|
|
391
|
+
def test_roughness_reset_to_none(self):
|
|
392
|
+
v = _base()
|
|
393
|
+
assert v.roughness == 0.3
|
|
394
|
+
v2 = v.override(roughness=None)
|
|
395
|
+
assert v2.roughness is None
|
|
396
|
+
# Other scalars untouched
|
|
397
|
+
assert v2.metallic == 1.0
|
|
398
|
+
|
|
399
|
+
def test_base_color_reset_to_none(self):
|
|
400
|
+
v = _base()
|
|
401
|
+
assert v.base_color is not None
|
|
402
|
+
v2 = v.override(base_color=None)
|
|
403
|
+
assert v2.base_color is None
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
class TestErrorMessageQuality:
|
|
407
|
+
"""Tighter assertions than the original 3.6.0 tests.
|
|
408
|
+
|
|
409
|
+
Loose original: ``test_typo_raises_typeerror`` only matched the
|
|
410
|
+
typo substring; didn't pin the fix-it hint. These pin both.
|
|
411
|
+
"""
|
|
412
|
+
|
|
413
|
+
def test_typo_message_includes_typo_and_valid_keys(self):
|
|
414
|
+
v = _base()
|
|
415
|
+
with pytest.raises(TypeError) as exc:
|
|
416
|
+
v.override(roughnes=0.5)
|
|
417
|
+
msg = str(exc.value)
|
|
418
|
+
assert "roughnes" in msg
|
|
419
|
+
assert "Valid keys" in msg
|
|
420
|
+
assert "roughness" in msg # the correct name appears in the hint
|
|
421
|
+
|
|
422
|
+
def test_internal_field_message_names_field(self):
|
|
423
|
+
v = _base()
|
|
424
|
+
with pytest.raises(TypeError, match="_textures"):
|
|
425
|
+
v.override(_textures={})
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
class TestRegistryDataCoupling:
|
|
429
|
+
"""Replace the ``pymat["Stainless Steel 304"]``-coupled tests
|
|
430
|
+
above with fixture-built Vises so the contract doesn't depend on
|
|
431
|
+
TOML data shape staying constant. The original tests still run
|
|
432
|
+
and still pass; these are an additional safety net."""
|
|
433
|
+
|
|
434
|
+
def test_finish_change_independent_of_registry(self):
|
|
435
|
+
v = _base() # local fixture with brushed/polished
|
|
436
|
+
v.finish = "brushed"
|
|
437
|
+
v2 = v.override(finish="polished")
|
|
438
|
+
assert v.material_id == "Metal012" # original untouched
|
|
439
|
+
assert v2.material_id == "Metal049A"
|
|
440
|
+
|
|
441
|
+
def test_scalar_override_independent_of_registry(self):
|
|
442
|
+
v = _base()
|
|
443
|
+
v2 = v.override(roughness=0.99)
|
|
444
|
+
assert v.roughness == 0.3 # local fixture's value
|
|
445
|
+
assert v2.roughness == 0.99
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
class TestIdempotence:
|
|
449
|
+
"""``v.override()`` chained twice produces an equivalent result —
|
|
450
|
+
confirms no hidden state accumulates."""
|
|
451
|
+
|
|
452
|
+
def test_double_no_op_override_equals_single(self):
|
|
453
|
+
v = _base()
|
|
454
|
+
once = v.override()
|
|
455
|
+
twice = once.override()
|
|
456
|
+
assert once == twice
|
|
457
|
+
assert once is not twice # but distinct objects
|
|
458
|
+
|
|
459
|
+
def test_double_scalar_override_overlay(self):
|
|
460
|
+
v = _base()
|
|
461
|
+
a = v.override(roughness=0.5)
|
|
462
|
+
b = a.override(roughness=0.9)
|
|
463
|
+
assert b.roughness == 0.9
|
|
464
|
+
# Original and intermediate untouched
|
|
465
|
+
assert v.roughness == 0.3
|
|
466
|
+
assert a.roughness == 0.5
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|