py-materials 3.2.0__tar.gz → 3.2.1__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.2.0 → py_materials-3.2.1}/PKG-INFO +1 -1
- {py_materials-3.2.0 → py_materials-3.2.1}/pyproject.toml +1 -1
- {py_materials-3.2.0 → py_materials-3.2.1}/src/pymat/__init__.py +1 -1
- {py_materials-3.2.0 → py_materials-3.2.1}/src/pymat/vis/_model.py +38 -0
- py_materials-3.2.1/src/pymat/vis/adapters.py +123 -0
- py_materials-3.2.1/tests/test_adapters.py +318 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/tests/test_e2e_vis.py +33 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/tests/test_integration.py +102 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/tests/test_vis.py +5 -2
- py_materials-3.2.0/src/pymat/vis/adapters.py +0 -91
- py_materials-3.2.0/tests/test_adapters.py +0 -159
- {py_materials-3.2.0 → py_materials-3.2.1}/.gitignore +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/LICENSE +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/README.md +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/docs/catalog/README.md +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/docs/catalog/ceramics/README.md +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/docs/catalog/electronics/README.md +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/docs/catalog/gases/README.md +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/docs/catalog/liquids/README.md +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/docs/catalog/metals/README.md +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/docs/catalog/plastics/README.md +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/docs/catalog/scintillators/README.md +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/docs/decisions/README.md +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/mat-rs/data/README.md +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/src/pymat/core.py +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/src/pymat/data/ceramics.toml +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/src/pymat/data/electronics.toml +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/src/pymat/data/gases.toml +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/src/pymat/data/liquids.toml +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/src/pymat/data/metals.toml +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/src/pymat/data/plastics.toml +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/src/pymat/data/scintillators.toml +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/src/pymat/elements.py +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/src/pymat/enrichers.py +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/src/pymat/factories.py +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/src/pymat/loader.py +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/src/pymat/properties.py +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/src/pymat/registry.py +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/src/pymat/units.py +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/src/pymat/vis/__init__.py +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/tests/__init__.py +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/tests/_visual_compare.py +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/tests/baselines/README.md +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/tests/conftest.py +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/tests/material_preview.html +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/tests/test_apply_to.py +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/tests/test_catalog.py +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/tests/test_core.py +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/tests/test_enrichers.py +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/tests/test_loader.py +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/tests/test_properties.py +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/tests/test_readme_examples.py +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/tests/test_toml_integrity.py +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/tests/test_uncertainties.py +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/tests/test_visual_compare.py +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/tests/test_visual_regression.py +0 -0
- {py_materials-3.2.0 → py_materials-3.2.1}/tests/visual_render.html +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: py-materials
|
|
3
|
-
Version: 3.2.
|
|
3
|
+
Version: 3.2.1
|
|
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
|
|
@@ -406,6 +406,44 @@ class Vis:
|
|
|
406
406
|
tex = self.textures.get(channel)
|
|
407
407
|
return ResolvedChannel(texture=tex, scalar=scalar)
|
|
408
408
|
|
|
409
|
+
# ── Adapter sugar (delegate to module-level, method discoverability) ─
|
|
410
|
+
|
|
411
|
+
def to_threejs(self) -> dict[str, Any]:
|
|
412
|
+
"""Shorthand for ``pymat.vis.to_threejs(material)`` — method form.
|
|
413
|
+
|
|
414
|
+
Same output as the module-level adapter. Use this when you have
|
|
415
|
+
a ``Vis`` in hand (``material.vis.to_threejs()``); use the
|
|
416
|
+
module-level form (``pymat.vis.to_threejs(material)``) in code
|
|
417
|
+
that's explicitly function-composition oriented.
|
|
418
|
+
"""
|
|
419
|
+
from pymat.vis.adapters import to_threejs
|
|
420
|
+
|
|
421
|
+
return to_threejs(self)
|
|
422
|
+
|
|
423
|
+
def to_gltf(self, *, name: str | None = None) -> dict[str, Any]:
|
|
424
|
+
"""Shorthand for ``pymat.vis.to_gltf(material)`` — method form.
|
|
425
|
+
|
|
426
|
+
The glTF material node's ``name`` field is populated from
|
|
427
|
+
``name=`` if given, else left empty (the method has no
|
|
428
|
+
visibility into the owning Material's name; pass it through
|
|
429
|
+
explicitly when calling on a standalone Vis). The module-level
|
|
430
|
+
``pymat.vis.to_gltf(material)`` reads ``material.name``
|
|
431
|
+
automatically.
|
|
432
|
+
"""
|
|
433
|
+
from pymat.vis.adapters import to_gltf
|
|
434
|
+
|
|
435
|
+
return to_gltf(self, name=name)
|
|
436
|
+
|
|
437
|
+
def export_mtlx(self, output_dir: str | Path, *, name: str | None = None) -> Path:
|
|
438
|
+
"""Shorthand for ``pymat.vis.export_mtlx(material, out)``.
|
|
439
|
+
|
|
440
|
+
``name=`` sets the MTLX filename stem. Omitted on a standalone
|
|
441
|
+
Vis → defaults to ``"material"``.
|
|
442
|
+
"""
|
|
443
|
+
from pymat.vis.adapters import export_mtlx
|
|
444
|
+
|
|
445
|
+
return export_mtlx(self, Path(output_dir), name=name)
|
|
446
|
+
|
|
409
447
|
# ── Discovery (py-mat's tag-aware layer over client.search) ─
|
|
410
448
|
|
|
411
449
|
def discover(
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Output adapters — thin wrappers that map Material to mat-vis's
|
|
3
|
+
generic adapter functions.
|
|
4
|
+
|
|
5
|
+
The actual format logic (Three.js field names, glTF schema,
|
|
6
|
+
MaterialX XML) lives in mat_vis_client.adapters (installed from
|
|
7
|
+
mat-vis-client package). These wrappers extract scalars + textures
|
|
8
|
+
from a ``Material`` (or a standalone ``Vis``) and pass them through.
|
|
9
|
+
|
|
10
|
+
from pymat.vis.adapters import to_threejs, to_gltf, export_mtlx
|
|
11
|
+
result = to_threejs(material) # Material form
|
|
12
|
+
result = to_threejs(material.vis) # Vis form — same output
|
|
13
|
+
|
|
14
|
+
The polymorphism lets ``Vis.to_gltf()`` / ``Vis.to_threejs()`` method
|
|
15
|
+
sugar delegate here without a back-reference from ``Vis`` to its
|
|
16
|
+
owning ``Material``.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
from pathlib import Path
|
|
22
|
+
from typing import TYPE_CHECKING, Any, Union
|
|
23
|
+
|
|
24
|
+
from mat_vis_client.adapters import export_mtlx as _export_mtlx
|
|
25
|
+
from mat_vis_client.adapters import to_gltf as _to_gltf
|
|
26
|
+
from mat_vis_client.adapters import to_threejs as _to_threejs
|
|
27
|
+
|
|
28
|
+
if TYPE_CHECKING:
|
|
29
|
+
from pymat.core import _MaterialInternal as Material
|
|
30
|
+
from pymat.vis._model import Vis
|
|
31
|
+
|
|
32
|
+
MaterialOrVis = Union[Material, Vis]
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _rgba_to_hex(rgba: list[float] | tuple[float, ...] | None) -> str | None:
|
|
36
|
+
"""Convert [r, g, b, a?] in 0-1 range to '#RRGGBB'. Alpha dropped."""
|
|
37
|
+
if rgba is None:
|
|
38
|
+
return None
|
|
39
|
+
r, g, b = (int(round(max(0.0, min(1.0, c)) * 255)) for c in rgba[:3])
|
|
40
|
+
return f"#{r:02x}{g:02x}{b:02x}"
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _resolve_vis_and_name(obj: MaterialOrVis) -> tuple[Vis, str]:
|
|
44
|
+
"""Unwrap a Material (``→ .vis, .name``) or a standalone Vis
|
|
45
|
+
(``→ self, ""``). Duck-typed via the ``.vis`` attribute: anything
|
|
46
|
+
that exposes ``.vis`` is treated as the owning Material."""
|
|
47
|
+
if hasattr(obj, "vis"):
|
|
48
|
+
return obj.vis, getattr(obj, "name", "") or ""
|
|
49
|
+
return obj, "" # assume it's a Vis
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _extract_scalars(obj: MaterialOrVis) -> dict[str, Any]:
|
|
53
|
+
"""Extract PBR scalars from material.vis (or a plain Vis).
|
|
54
|
+
|
|
55
|
+
Maps py-mat "metallic" → mat-vis "metalness" and our RGBA
|
|
56
|
+
base_color list → mat-vis's color_hex string (its adapters
|
|
57
|
+
only know how to emit color from the hex form).
|
|
58
|
+
"""
|
|
59
|
+
vis, _ = _resolve_vis_and_name(obj)
|
|
60
|
+
return {
|
|
61
|
+
"metalness": vis.get("metallic"),
|
|
62
|
+
"roughness": vis.get("roughness"),
|
|
63
|
+
"color_hex": _rgba_to_hex(vis.get("base_color")),
|
|
64
|
+
"ior": vis.get("ior"),
|
|
65
|
+
"transmission": vis.get("transmission"),
|
|
66
|
+
"clearcoat": vis.get("clearcoat"),
|
|
67
|
+
"emissive": vis.get("emissive"),
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _extract_textures(obj: MaterialOrVis) -> dict[str, bytes]:
|
|
72
|
+
"""Extract texture bytes from a Material's Vis (or a plain Vis)."""
|
|
73
|
+
vis, _ = _resolve_vis_and_name(obj)
|
|
74
|
+
if not vis.has_mapping:
|
|
75
|
+
return {}
|
|
76
|
+
return vis.textures
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def to_threejs(obj: MaterialOrVis) -> dict[str, Any]:
|
|
80
|
+
"""Format as a Three.js MeshPhysicalMaterial-compatible dict.
|
|
81
|
+
|
|
82
|
+
Accepts either a ``Material`` or a standalone ``Vis``. Reads PBR
|
|
83
|
+
scalars and texture maps from ``obj.vis`` (or from ``obj`` itself
|
|
84
|
+
if it's a Vis). Delegates to mat-vis's generic adapter.
|
|
85
|
+
"""
|
|
86
|
+
return _to_threejs(_extract_scalars(obj), _extract_textures(obj))
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def to_gltf(obj: MaterialOrVis, *, name: str | None = None) -> dict[str, Any]:
|
|
90
|
+
"""Format as a glTF pbrMetallicRoughness material dict.
|
|
91
|
+
|
|
92
|
+
Accepts either a ``Material`` (its ``.name`` is used as the glTF
|
|
93
|
+
material ``name`` field) or a standalone ``Vis`` (pass ``name=``
|
|
94
|
+
explicitly to populate the field; empty string otherwise).
|
|
95
|
+
Delegates to mat-vis's generic adapter.
|
|
96
|
+
"""
|
|
97
|
+
_, resolved_name = _resolve_vis_and_name(obj)
|
|
98
|
+
result = _to_gltf(_extract_scalars(obj), _extract_textures(obj))
|
|
99
|
+
result["name"] = name if name is not None else resolved_name
|
|
100
|
+
return result
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def export_mtlx(
|
|
104
|
+
obj: MaterialOrVis,
|
|
105
|
+
output_dir: Path,
|
|
106
|
+
*,
|
|
107
|
+
name: str | None = None,
|
|
108
|
+
) -> Path:
|
|
109
|
+
"""Export as a MaterialX .mtlx file + PNG textures on disk.
|
|
110
|
+
|
|
111
|
+
Accepts either a ``Material`` (its ``.name`` becomes the filename
|
|
112
|
+
stem) or a standalone ``Vis`` (pass ``name=`` explicitly to name
|
|
113
|
+
the output). Delegates to mat-vis's generic adapter.
|
|
114
|
+
"""
|
|
115
|
+
_, resolved_name = _resolve_vis_and_name(obj)
|
|
116
|
+
mat_name = name if name is not None else resolved_name
|
|
117
|
+
safe_name = mat_name.replace(" ", "_").replace("/", "_") or "material"
|
|
118
|
+
return _export_mtlx(
|
|
119
|
+
_extract_scalars(obj),
|
|
120
|
+
_extract_textures(obj),
|
|
121
|
+
output_dir,
|
|
122
|
+
material_name=safe_name,
|
|
123
|
+
)
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
"""Tests for pymat.vis.adapters — Material → format wrappers."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import tempfile
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from pymat import Material
|
|
9
|
+
from pymat.vis.adapters import (
|
|
10
|
+
_extract_scalars,
|
|
11
|
+
_extract_textures,
|
|
12
|
+
_rgba_to_hex,
|
|
13
|
+
export_mtlx,
|
|
14
|
+
to_gltf,
|
|
15
|
+
to_threejs,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _make_material(with_vis=False):
|
|
20
|
+
"""Create a test material with vis scalars."""
|
|
21
|
+
m = Material(name="Test Steel")
|
|
22
|
+
m.vis.metallic = 1.0
|
|
23
|
+
m.vis.roughness = 0.3
|
|
24
|
+
m.vis.base_color = (0.8, 0.8, 0.8, 1.0)
|
|
25
|
+
m.vis.ior = 2.5
|
|
26
|
+
m.vis.transmission = 0.0
|
|
27
|
+
m.vis.clearcoat = 0.0
|
|
28
|
+
m.vis.emissive = (0, 0, 0)
|
|
29
|
+
if with_vis:
|
|
30
|
+
# Identity must be set BEFORE the cache — assigning source /
|
|
31
|
+
# material_id invalidates _textures + _fetched via Vis.__setattr__
|
|
32
|
+
# (3.1.1 cache-invalidation behavior).
|
|
33
|
+
m.vis.source = "ambientcg"
|
|
34
|
+
m.vis.material_id = "Metal032"
|
|
35
|
+
m.vis._textures = {
|
|
36
|
+
"color": b"\x89PNG_color",
|
|
37
|
+
"normal": b"\x89PNG_normal",
|
|
38
|
+
"roughness": b"\x89PNG_roughness",
|
|
39
|
+
}
|
|
40
|
+
m.vis._fetched = True
|
|
41
|
+
return m
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class TestExtractScalars:
|
|
45
|
+
def test_from_pbr(self):
|
|
46
|
+
m = _make_material()
|
|
47
|
+
s = _extract_scalars(m)
|
|
48
|
+
assert s["metalness"] == 1.0
|
|
49
|
+
assert s["roughness"] == 0.3
|
|
50
|
+
assert s["ior"] == 2.5
|
|
51
|
+
|
|
52
|
+
def test_vis_wins_over_pbr(self):
|
|
53
|
+
m = _make_material()
|
|
54
|
+
m.vis.roughness = 0.5 # vis override
|
|
55
|
+
s = _extract_scalars(m)
|
|
56
|
+
assert s["roughness"] == 0.5 # vis wins
|
|
57
|
+
assert s["metalness"] == 1.0 # falls back to pbr
|
|
58
|
+
|
|
59
|
+
def test_metallic_mapped_to_metalness(self):
|
|
60
|
+
m = _make_material()
|
|
61
|
+
s = _extract_scalars(m)
|
|
62
|
+
assert "metalness" in s
|
|
63
|
+
assert "metallic" not in s
|
|
64
|
+
|
|
65
|
+
def test_base_color_emitted_as_color_hex(self):
|
|
66
|
+
"""mat-vis-client only formats color from color_hex — the extractor
|
|
67
|
+
must convert our RGBA list to '#RRGGBB' or the final Three.js dict
|
|
68
|
+
has no color at all (every material renders as default grey)."""
|
|
69
|
+
m = _make_material()
|
|
70
|
+
m.vis.base_color = (1.0, 0.0, 0.0, 1.0)
|
|
71
|
+
s = _extract_scalars(m)
|
|
72
|
+
assert s["color_hex"] == "#ff0000"
|
|
73
|
+
assert "base_color" not in s # RGBA form must not leak through
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class TestRgbaToHex:
|
|
77
|
+
def test_pure_red(self):
|
|
78
|
+
assert _rgba_to_hex([1.0, 0.0, 0.0, 1.0]) == "#ff0000"
|
|
79
|
+
|
|
80
|
+
def test_pure_green(self):
|
|
81
|
+
assert _rgba_to_hex([0.0, 1.0, 0.0]) == "#00ff00" # alpha optional
|
|
82
|
+
|
|
83
|
+
def test_mid_grey(self):
|
|
84
|
+
assert _rgba_to_hex([0.5, 0.5, 0.5]) == "#808080"
|
|
85
|
+
|
|
86
|
+
def test_none_passthrough(self):
|
|
87
|
+
assert _rgba_to_hex(None) is None
|
|
88
|
+
|
|
89
|
+
def test_clamps_out_of_range(self):
|
|
90
|
+
# PBR values can over/undershoot; don't raise, just clamp
|
|
91
|
+
assert _rgba_to_hex([-0.1, 1.5, 0.5]) == "#00ff80"
|
|
92
|
+
|
|
93
|
+
def test_alpha_ignored(self):
|
|
94
|
+
assert _rgba_to_hex([1.0, 1.0, 1.0, 0.3]) == "#ffffff" # alpha stripped
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
class TestExtractTextures:
|
|
98
|
+
def test_no_vis_returns_empty(self):
|
|
99
|
+
m = _make_material(with_vis=False)
|
|
100
|
+
assert _extract_textures(m) == {}
|
|
101
|
+
|
|
102
|
+
def test_with_vis_returns_textures(self):
|
|
103
|
+
m = _make_material(with_vis=True)
|
|
104
|
+
tex = _extract_textures(m)
|
|
105
|
+
assert "color" in tex
|
|
106
|
+
assert tex["color"] == b"\x89PNG_color"
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
class TestToThreejs:
|
|
110
|
+
def test_scalar_only(self):
|
|
111
|
+
m = _make_material(with_vis=False)
|
|
112
|
+
d = to_threejs(m)
|
|
113
|
+
assert d["metalness"] == 1.0
|
|
114
|
+
assert d["roughness"] == 0.3
|
|
115
|
+
assert "map" not in d # no textures
|
|
116
|
+
|
|
117
|
+
def test_with_textures(self):
|
|
118
|
+
m = _make_material(with_vis=True)
|
|
119
|
+
d = to_threejs(m)
|
|
120
|
+
assert "map" in d # base64 data URI
|
|
121
|
+
assert d["map"].startswith("data:image/png;base64,")
|
|
122
|
+
assert "normalMap" in d
|
|
123
|
+
assert "roughnessMap" in d
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
class TestToGltf:
|
|
127
|
+
def test_scalar_only(self):
|
|
128
|
+
m = _make_material(with_vis=False)
|
|
129
|
+
d = to_gltf(m)
|
|
130
|
+
assert d["name"] == "Test Steel"
|
|
131
|
+
assert "pbrMetallicRoughness" in d
|
|
132
|
+
pbr = d["pbrMetallicRoughness"]
|
|
133
|
+
assert pbr["metallicFactor"] == 1.0
|
|
134
|
+
assert pbr["roughnessFactor"] == 0.3
|
|
135
|
+
|
|
136
|
+
def test_with_textures(self):
|
|
137
|
+
m = _make_material(with_vis=True)
|
|
138
|
+
d = to_gltf(m)
|
|
139
|
+
assert d["name"] == "Test Steel"
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
class TestExportMtlx:
|
|
143
|
+
def test_export_creates_files(self):
|
|
144
|
+
m = _make_material(with_vis=True)
|
|
145
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
146
|
+
mtlx_path = export_mtlx(m, Path(tmp))
|
|
147
|
+
assert mtlx_path.exists()
|
|
148
|
+
assert mtlx_path.suffix == ".mtlx"
|
|
149
|
+
# Check textures were written as PNG files
|
|
150
|
+
pngs = list(Path(tmp).glob("*.png"))
|
|
151
|
+
assert len(pngs) == 3 # color, normal, roughness
|
|
152
|
+
|
|
153
|
+
def test_export_no_textures(self):
|
|
154
|
+
m = _make_material(with_vis=False)
|
|
155
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
156
|
+
mtlx_path = export_mtlx(m, Path(tmp))
|
|
157
|
+
assert mtlx_path.exists()
|
|
158
|
+
pngs = list(Path(tmp).glob("*.png"))
|
|
159
|
+
assert len(pngs) == 0 # no textures
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
class TestAdapterPolymorphism:
|
|
163
|
+
"""Adapters accept either a Material or a Vis — same output.
|
|
164
|
+
|
|
165
|
+
This is the property that lets ``Vis.to_gltf()`` method sugar
|
|
166
|
+
delegate to the module-level adapter without a back-reference
|
|
167
|
+
from Vis to its owning Material.
|
|
168
|
+
"""
|
|
169
|
+
|
|
170
|
+
def test_to_threejs_material_and_vis_agree(self):
|
|
171
|
+
m = _make_material()
|
|
172
|
+
assert to_threejs(m) == to_threejs(m.vis)
|
|
173
|
+
|
|
174
|
+
def test_to_gltf_material_and_vis_agree_on_pbr(self):
|
|
175
|
+
m = _make_material()
|
|
176
|
+
from_material = to_gltf(m)
|
|
177
|
+
from_vis = to_gltf(m.vis, name=m.name)
|
|
178
|
+
assert from_material == from_vis
|
|
179
|
+
|
|
180
|
+
def test_to_gltf_vis_without_name_leaves_empty_string(self):
|
|
181
|
+
m = _make_material()
|
|
182
|
+
d = to_gltf(m.vis) # no name= kwarg
|
|
183
|
+
assert d["name"] == ""
|
|
184
|
+
|
|
185
|
+
def test_export_mtlx_vis_accepts_name_kwarg(self):
|
|
186
|
+
m = _make_material(with_vis=True)
|
|
187
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
188
|
+
# Via Vis + explicit name — filename stem must match.
|
|
189
|
+
mtlx_path = export_mtlx(m.vis, Path(tmp), name="Stem")
|
|
190
|
+
assert mtlx_path.exists()
|
|
191
|
+
assert mtlx_path.stem == "Stem", f"expected stem 'Stem', got {mtlx_path.stem!r}"
|
|
192
|
+
|
|
193
|
+
def test_export_mtlx_vis_without_name_falls_back(self):
|
|
194
|
+
"""A standalone Vis with no name= kwarg must not crash; the
|
|
195
|
+
default stem is whatever the adapter picks (currently 'material')."""
|
|
196
|
+
from pymat.vis._model import Vis
|
|
197
|
+
|
|
198
|
+
# Construct a Vis fully detached from any Material — this is
|
|
199
|
+
# the path a downstream like mat-vis-client might hit if they
|
|
200
|
+
# adopted the same sugar independently.
|
|
201
|
+
vis = Vis(source=None, material_id=None) # no mapping → no textures
|
|
202
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
203
|
+
mtlx_path = export_mtlx(vis, Path(tmp))
|
|
204
|
+
assert mtlx_path.exists()
|
|
205
|
+
assert mtlx_path.stem != "", "empty stem would break filesystem writes"
|
|
206
|
+
|
|
207
|
+
def test_resolve_vis_duck_typing_invariant(self):
|
|
208
|
+
"""``_resolve_vis_and_name`` distinguishes Material-vs-Vis via
|
|
209
|
+
``hasattr(obj, "vis")``. If Vis ever grows a ``.vis`` attribute
|
|
210
|
+
(self-reference, alias, anything), the helper would recurse and
|
|
211
|
+
misclassify. Pin that invariant here."""
|
|
212
|
+
from pymat.vis._model import Vis
|
|
213
|
+
|
|
214
|
+
vis = Vis(source="ambientcg", material_id="Metal032")
|
|
215
|
+
assert not hasattr(vis, "vis"), (
|
|
216
|
+
"Vis must not have a .vis attribute — would break the "
|
|
217
|
+
"Material-vs-Vis duck-typing in adapter._resolve_vis_and_name. "
|
|
218
|
+
"If this test starts failing, the helper needs an "
|
|
219
|
+
"isinstance check instead of hasattr."
|
|
220
|
+
)
|
|
221
|
+
|
|
222
|
+
def test_adapter_name_is_keyword_only(self):
|
|
223
|
+
"""``to_gltf(obj, name=...)`` must be keyword-only. Positional
|
|
224
|
+
call should be a TypeError — prevents a downstream relying on
|
|
225
|
+
positional ordering that could silently shift on a future
|
|
226
|
+
signature change."""
|
|
227
|
+
import pytest
|
|
228
|
+
|
|
229
|
+
from pymat.vis._model import Vis
|
|
230
|
+
|
|
231
|
+
vis = Vis()
|
|
232
|
+
with pytest.raises(TypeError):
|
|
233
|
+
to_gltf(vis, "positional_name") # type: ignore[misc]
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
class TestAdapterOnDetachedVis:
|
|
237
|
+
"""Full-path tests that construct a Vis without any Material,
|
|
238
|
+
verifying every adapter still works on the raw payload object.
|
|
239
|
+
This is the contract downstreams (including a hypothetical
|
|
240
|
+
mat-vis-client VisAsset) can rely on."""
|
|
241
|
+
|
|
242
|
+
def _detached_vis_with_scalars(self):
|
|
243
|
+
from pymat.vis._model import Vis
|
|
244
|
+
|
|
245
|
+
v = Vis()
|
|
246
|
+
v.metallic = 1.0
|
|
247
|
+
v.roughness = 0.25
|
|
248
|
+
v.base_color = (0.9, 0.9, 0.9, 1.0)
|
|
249
|
+
v.ior = 1.5
|
|
250
|
+
v.transmission = 0.0
|
|
251
|
+
return v
|
|
252
|
+
|
|
253
|
+
def test_to_threejs_on_detached_vis(self):
|
|
254
|
+
v = self._detached_vis_with_scalars()
|
|
255
|
+
d = to_threejs(v)
|
|
256
|
+
assert d["metalness"] == 1.0
|
|
257
|
+
assert d["roughness"] == 0.25
|
|
258
|
+
assert "ior" in d # ior scalar routed through
|
|
259
|
+
|
|
260
|
+
def test_to_gltf_on_detached_vis_empty_name(self):
|
|
261
|
+
v = self._detached_vis_with_scalars()
|
|
262
|
+
d = to_gltf(v)
|
|
263
|
+
assert d["pbrMetallicRoughness"]["metallicFactor"] == 1.0
|
|
264
|
+
assert d["name"] == "" # no owning Material, no name kwarg
|
|
265
|
+
|
|
266
|
+
def test_to_gltf_on_detached_vis_with_name(self):
|
|
267
|
+
v = self._detached_vis_with_scalars()
|
|
268
|
+
d = to_gltf(v, name="Hand-rolled")
|
|
269
|
+
assert d["name"] == "Hand-rolled"
|
|
270
|
+
|
|
271
|
+
def test_vis_method_on_detached_vis(self):
|
|
272
|
+
"""The method form must also work on a detached Vis — covers
|
|
273
|
+
the common case of ``Vis().to_gltf(name='...')`` in downstream
|
|
274
|
+
code that constructs Vis without a py-mat Material."""
|
|
275
|
+
v = self._detached_vis_with_scalars()
|
|
276
|
+
assert v.to_gltf(name="X") == to_gltf(v, name="X")
|
|
277
|
+
assert v.to_threejs() == to_threejs(v)
|
|
278
|
+
|
|
279
|
+
def test_method_call_surface_present(self):
|
|
280
|
+
"""``m.vis.<TAB>`` must surface the three adapter methods —
|
|
281
|
+
that's the whole point of this sugar."""
|
|
282
|
+
v = self._detached_vis_with_scalars()
|
|
283
|
+
for name in ("to_threejs", "to_gltf", "export_mtlx"):
|
|
284
|
+
attr = getattr(v, name, None)
|
|
285
|
+
assert attr is not None, f"Vis.{name} missing"
|
|
286
|
+
assert callable(attr), f"Vis.{name} not callable"
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
class TestVisAdapterMethods:
|
|
290
|
+
"""Method-form sugar on Vis — discoverability via tab completion.
|
|
291
|
+
|
|
292
|
+
Must be a drop-in shorthand: ``m.vis.to_gltf()`` and
|
|
293
|
+
``pymat.vis.to_gltf(m)`` must produce the same dict. Same for
|
|
294
|
+
``to_threejs`` and ``export_mtlx``.
|
|
295
|
+
"""
|
|
296
|
+
|
|
297
|
+
def test_vis_to_threejs_matches_module_level(self):
|
|
298
|
+
m = _make_material()
|
|
299
|
+
assert m.vis.to_threejs() == to_threejs(m)
|
|
300
|
+
|
|
301
|
+
def test_vis_to_gltf_matches_module_level_with_name(self):
|
|
302
|
+
m = _make_material()
|
|
303
|
+
assert m.vis.to_gltf(name=m.name) == to_gltf(m)
|
|
304
|
+
|
|
305
|
+
def test_vis_to_gltf_no_name_leaves_empty(self):
|
|
306
|
+
"""Vis.to_gltf() without name= produces an empty name field —
|
|
307
|
+
the method can't reach the owning Material without a back-ref,
|
|
308
|
+
so users on a standalone Vis path opt in by passing name="X"."""
|
|
309
|
+
m = _make_material()
|
|
310
|
+
d = m.vis.to_gltf()
|
|
311
|
+
assert d["name"] == ""
|
|
312
|
+
|
|
313
|
+
def test_vis_export_mtlx_delegates(self):
|
|
314
|
+
m = _make_material(with_vis=True)
|
|
315
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
316
|
+
mtlx_path = m.vis.export_mtlx(Path(tmp), name="ViaMethod")
|
|
317
|
+
assert mtlx_path.exists()
|
|
318
|
+
assert mtlx_path.suffix == ".mtlx"
|
|
@@ -220,6 +220,39 @@ class TestEndToEnd:
|
|
|
220
220
|
textures = vis.fetch(r["source"], r["id"], tier="128")
|
|
221
221
|
assert len(textures) >= 0 # may be 0 if 128 tier not available
|
|
222
222
|
|
|
223
|
+
def test_mtlx_xml_returns_materialx_string(self):
|
|
224
|
+
"""Material.vis.mtlx.xml() returns a MaterialX XML string.
|
|
225
|
+
|
|
226
|
+
Pins the 0.5 method-form (``.xml()`` not property ``.xml``) and
|
|
227
|
+
asserts the content is MaterialX — not an error page or empty
|
|
228
|
+
string. The full ``.export(path)`` package path is covered by
|
|
229
|
+
the adapter test; this test locks the direct ``.xml()`` accessor
|
|
230
|
+
the README + build123d integration docs call out.
|
|
231
|
+
"""
|
|
232
|
+
from pymat import Material, vis
|
|
233
|
+
|
|
234
|
+
results = vis.search(category="metal", limit=1)
|
|
235
|
+
if not results:
|
|
236
|
+
pytest.skip("No metal materials in mat-vis index")
|
|
237
|
+
|
|
238
|
+
source = results[0].get("source", "ambientcg")
|
|
239
|
+
mat_id = results[0]["id"]
|
|
240
|
+
|
|
241
|
+
m = Material(name="mtlx xml probe")
|
|
242
|
+
m.vis.source = source
|
|
243
|
+
m.vis.material_id = mat_id
|
|
244
|
+
m.vis.tier = results[0].get("default_tier") or "1k"
|
|
245
|
+
|
|
246
|
+
with _skip_on_upstream_outage():
|
|
247
|
+
xml = m.vis.mtlx.xml()
|
|
248
|
+
|
|
249
|
+
assert isinstance(xml, str), f"expected str, got {type(xml).__name__}"
|
|
250
|
+
lowered = xml.lower()
|
|
251
|
+
assert "<materialx" in lowered or xml.startswith("<?xml"), (
|
|
252
|
+
f"not a MaterialX document; first 200 chars: {xml[:200]!r}"
|
|
253
|
+
)
|
|
254
|
+
assert len(xml) > 200, f"suspiciously short MaterialX doc ({len(xml)} chars)"
|
|
255
|
+
|
|
223
256
|
def test_resolve_channel(self):
|
|
224
257
|
"""Vis.resolve() returns texture or scalar fallback."""
|
|
225
258
|
from pymat import Material, vis
|
|
@@ -69,6 +69,108 @@ class TestApplyToShape:
|
|
|
69
69
|
assert material.properties.optical.light_yield == 34000
|
|
70
70
|
|
|
71
71
|
|
|
72
|
+
class TestGltfMaterialPassthrough:
|
|
73
|
+
"""Pins what build123d's ``export_gltf`` *actually* preserves from
|
|
74
|
+
``shape.material``. The picture is narrower than the README first
|
|
75
|
+
suggested:
|
|
76
|
+
|
|
77
|
+
- ``apply_to()`` sets ``shape.color`` (derived from the material's
|
|
78
|
+
hex), which build123d serializes as ``baseColorFactor``. **Only
|
|
79
|
+
base color survives.**
|
|
80
|
+
- Direct ``shape.material = m`` (no ``apply_to``) writes **no
|
|
81
|
+
material at all** — build123d's exporter reads ``shape.color``,
|
|
82
|
+
not ``shape.material``.
|
|
83
|
+
- ``metallic``, ``roughness``, textures, ``ior``, ``transmission``:
|
|
84
|
+
all dropped by build123d 0.10's glTF exporter regardless of path.
|
|
85
|
+
|
|
86
|
+
Closing that gap is the job of build123d#1270 (Materials class).
|
|
87
|
+
Until it lands, downstream consumers who want the full PBR surface
|
|
88
|
+
should reach for ``pymat.vis.to_threejs(m)`` / ``to_gltf(m)`` / the
|
|
89
|
+
mtlx exporter instead of relying on ``export_gltf``.
|
|
90
|
+
|
|
91
|
+
Format note: build123d 0.10 writes JSON (not GLB binary) even when
|
|
92
|
+
the extension is ``.glb``; we parse the JSON directly.
|
|
93
|
+
"""
|
|
94
|
+
|
|
95
|
+
def test_apply_to_preserves_base_color_only(self, tmp_path):
|
|
96
|
+
pytest.importorskip("build123d")
|
|
97
|
+
|
|
98
|
+
import json
|
|
99
|
+
|
|
100
|
+
from build123d import Box, export_gltf
|
|
101
|
+
|
|
102
|
+
from pymat import stainless
|
|
103
|
+
|
|
104
|
+
shape = Box(10, 10, 10)
|
|
105
|
+
stainless.s304.apply_to(shape)
|
|
106
|
+
assert shape.color is not None, "apply_to should set shape.color"
|
|
107
|
+
|
|
108
|
+
out = tmp_path / "apply_to.glb"
|
|
109
|
+
export_gltf(shape, str(out))
|
|
110
|
+
doc = json.loads(out.read_text())
|
|
111
|
+
|
|
112
|
+
materials = doc.get("materials") or []
|
|
113
|
+
assert materials, (
|
|
114
|
+
"apply_to() should produce a shape.color that build123d "
|
|
115
|
+
"serializes as a glTF material — got none"
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
pbr = materials[0].get("pbrMetallicRoughness") or {}
|
|
119
|
+
base_color = pbr.get("baseColorFactor")
|
|
120
|
+
assert base_color and len(base_color) >= 3, f"baseColorFactor missing: {base_color!r}"
|
|
121
|
+
|
|
122
|
+
# Document the gap: build123d's exporter reads shape.color only,
|
|
123
|
+
# so metallic/roughness don't come through even though the
|
|
124
|
+
# Material has them.
|
|
125
|
+
assert "metallicFactor" not in pbr, (
|
|
126
|
+
"Unexpectedly found metallicFactor — build123d glTF exporter "
|
|
127
|
+
"now serializes more than shape.color? Update this test and "
|
|
128
|
+
"the claim 8 wording for build123d#1270."
|
|
129
|
+
)
|
|
130
|
+
assert "roughnessFactor" not in pbr, (
|
|
131
|
+
"Unexpectedly found roughnessFactor — see comment above."
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
# Mesh primitive must actually reference the material for it to
|
|
135
|
+
# render. This one *does* — glTF validator would fail otherwise.
|
|
136
|
+
primitives = (doc.get("meshes") or [{}])[0].get("primitives") or []
|
|
137
|
+
mat_indices = [p.get("material") for p in primitives if "material" in p]
|
|
138
|
+
assert mat_indices, "glTF has a material but no primitive references it"
|
|
139
|
+
|
|
140
|
+
def test_direct_material_assignment_does_not_reach_gltf(self, tmp_path):
|
|
141
|
+
"""Counter-case: setting ``shape.material`` directly (no
|
|
142
|
+
``apply_to``) produces no material in the glTF — build123d's
|
|
143
|
+
exporter doesn't read ``shape.material``."""
|
|
144
|
+
pytest.importorskip("build123d")
|
|
145
|
+
|
|
146
|
+
import json
|
|
147
|
+
|
|
148
|
+
from build123d import Box, export_gltf
|
|
149
|
+
|
|
150
|
+
from pymat import Material
|
|
151
|
+
|
|
152
|
+
shape = Box(10, 10, 10)
|
|
153
|
+
m = Material(name="Direct")
|
|
154
|
+
m.vis.metallic = 1.0
|
|
155
|
+
m.vis.roughness = 0.3
|
|
156
|
+
m.vis.base_color = (0.8, 0.1, 0.1, 1.0)
|
|
157
|
+
shape.material = m
|
|
158
|
+
# Intentionally NOT calling apply_to — no shape.color set.
|
|
159
|
+
|
|
160
|
+
out = tmp_path / "direct.glb"
|
|
161
|
+
export_gltf(shape, str(out))
|
|
162
|
+
doc = json.loads(out.read_text())
|
|
163
|
+
|
|
164
|
+
# Note: if build123d#1270 lands and starts reading
|
|
165
|
+
# shape.material, this assertion will flip — that's the signal
|
|
166
|
+
# to update this test + the claim wording.
|
|
167
|
+
assert not doc.get("materials"), (
|
|
168
|
+
"Unexpectedly found materials[] from direct shape.material = m "
|
|
169
|
+
"(no apply_to). build123d#1270 may have landed — update the "
|
|
170
|
+
"build123d integration story and this test."
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
|
|
72
174
|
class TestPropertyAccess:
|
|
73
175
|
"""Test accessing various properties through loaded materials."""
|
|
74
176
|
|
|
@@ -450,9 +450,12 @@ class TestModuleShape:
|
|
|
450
450
|
# And the Material-accepting signature must hold
|
|
451
451
|
import inspect
|
|
452
452
|
|
|
453
|
+
# First positional parameter must be the Material/Vis object.
|
|
454
|
+
# 3.2+ renamed the param to ``obj`` for the polymorphic
|
|
455
|
+
# Material|Vis signature; historically it was ``material``.
|
|
453
456
|
params = list(inspect.signature(adapters.to_threejs).parameters)
|
|
454
|
-
assert params
|
|
455
|
-
f"local to_threejs must accept a Material, got params {params}"
|
|
457
|
+
assert params and params[0] in {"material", "obj"}, (
|
|
458
|
+
f"local to_threejs must accept a Material/Vis as first param, got params {params}"
|
|
456
459
|
)
|
|
457
460
|
|
|
458
461
|
def test_top_level_adapter_reexports(self):
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
Output adapters — thin wrappers that map Material to mat-vis's
|
|
3
|
-
generic adapter functions.
|
|
4
|
-
|
|
5
|
-
The actual format logic (Three.js field names, glTF schema,
|
|
6
|
-
MaterialX XML) lives in mat_vis_client.adapters (installed from
|
|
7
|
-
mat-vis-client package). These wrappers extract scalars + textures
|
|
8
|
-
from a Material and pass them through.
|
|
9
|
-
|
|
10
|
-
from pymat.vis.adapters import to_threejs, to_gltf, export_mtlx
|
|
11
|
-
result = to_threejs(material)
|
|
12
|
-
"""
|
|
13
|
-
|
|
14
|
-
from __future__ import annotations
|
|
15
|
-
|
|
16
|
-
from pathlib import Path
|
|
17
|
-
from typing import TYPE_CHECKING, Any
|
|
18
|
-
|
|
19
|
-
from mat_vis_client.adapters import export_mtlx as _export_mtlx
|
|
20
|
-
from mat_vis_client.adapters import to_gltf as _to_gltf
|
|
21
|
-
from mat_vis_client.adapters import to_threejs as _to_threejs
|
|
22
|
-
|
|
23
|
-
if TYPE_CHECKING:
|
|
24
|
-
from pymat.core import _MaterialInternal as Material
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
def _rgba_to_hex(rgba: list[float] | tuple[float, ...] | None) -> str | None:
|
|
28
|
-
"""Convert [r, g, b, a?] in 0-1 range to '#RRGGBB'. Alpha dropped."""
|
|
29
|
-
if rgba is None:
|
|
30
|
-
return None
|
|
31
|
-
r, g, b = (int(round(max(0.0, min(1.0, c)) * 255)) for c in rgba[:3])
|
|
32
|
-
return f"#{r:02x}{g:02x}{b:02x}"
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
def _extract_scalars(material: Material) -> dict[str, Any]:
|
|
36
|
-
"""Extract PBR scalars from material.vis with defaults.
|
|
37
|
-
|
|
38
|
-
Maps py-mat "metallic" → mat-vis "metalness" and our RGBA
|
|
39
|
-
base_color list → mat-vis's color_hex string (its adapters
|
|
40
|
-
only know how to emit color from the hex form).
|
|
41
|
-
"""
|
|
42
|
-
vis = material.vis
|
|
43
|
-
return {
|
|
44
|
-
"metalness": vis.get("metallic"),
|
|
45
|
-
"roughness": vis.get("roughness"),
|
|
46
|
-
"color_hex": _rgba_to_hex(vis.get("base_color")),
|
|
47
|
-
"ior": vis.get("ior"),
|
|
48
|
-
"transmission": vis.get("transmission"),
|
|
49
|
-
"clearcoat": vis.get("clearcoat"),
|
|
50
|
-
"emissive": vis.get("emissive"),
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
def _extract_textures(material: Material) -> dict[str, bytes]:
|
|
55
|
-
"""Extract texture bytes from Material.vis."""
|
|
56
|
-
if not material.vis.has_mapping:
|
|
57
|
-
return {}
|
|
58
|
-
return material.vis.textures
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
def to_threejs(material: Material) -> dict[str, Any]:
|
|
62
|
-
"""Format as a Three.js MeshPhysicalMaterial-compatible dict.
|
|
63
|
-
|
|
64
|
-
Reads PBR scalars and texture maps from material.vis.
|
|
65
|
-
Delegates to mat-vis's generic adapter.
|
|
66
|
-
"""
|
|
67
|
-
return _to_threejs(_extract_scalars(material), _extract_textures(material))
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
def to_gltf(material: Material) -> dict[str, Any]:
|
|
71
|
-
"""Format as a glTF pbrMetallicRoughness material dict.
|
|
72
|
-
|
|
73
|
-
Delegates to mat-vis's generic adapter.
|
|
74
|
-
"""
|
|
75
|
-
result = _to_gltf(_extract_scalars(material), _extract_textures(material))
|
|
76
|
-
result["name"] = material.name
|
|
77
|
-
return result
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
def export_mtlx(material: Material, output_dir: Path) -> Path:
|
|
81
|
-
"""Export as a MaterialX .mtlx file + PNG textures on disk.
|
|
82
|
-
|
|
83
|
-
Delegates to mat-vis's generic adapter.
|
|
84
|
-
"""
|
|
85
|
-
safe_name = material.name.replace(" ", "_").replace("/", "_")
|
|
86
|
-
return _export_mtlx(
|
|
87
|
-
_extract_scalars(material),
|
|
88
|
-
_extract_textures(material),
|
|
89
|
-
output_dir,
|
|
90
|
-
material_name=safe_name,
|
|
91
|
-
)
|
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
"""Tests for pymat.vis.adapters — Material → format wrappers."""
|
|
2
|
-
|
|
3
|
-
from __future__ import annotations
|
|
4
|
-
|
|
5
|
-
import tempfile
|
|
6
|
-
from pathlib import Path
|
|
7
|
-
|
|
8
|
-
from pymat import Material
|
|
9
|
-
from pymat.vis.adapters import (
|
|
10
|
-
_extract_scalars,
|
|
11
|
-
_extract_textures,
|
|
12
|
-
_rgba_to_hex,
|
|
13
|
-
export_mtlx,
|
|
14
|
-
to_gltf,
|
|
15
|
-
to_threejs,
|
|
16
|
-
)
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
def _make_material(with_vis=False):
|
|
20
|
-
"""Create a test material with vis scalars."""
|
|
21
|
-
m = Material(name="Test Steel")
|
|
22
|
-
m.vis.metallic = 1.0
|
|
23
|
-
m.vis.roughness = 0.3
|
|
24
|
-
m.vis.base_color = (0.8, 0.8, 0.8, 1.0)
|
|
25
|
-
m.vis.ior = 2.5
|
|
26
|
-
m.vis.transmission = 0.0
|
|
27
|
-
m.vis.clearcoat = 0.0
|
|
28
|
-
m.vis.emissive = (0, 0, 0)
|
|
29
|
-
if with_vis:
|
|
30
|
-
# Identity must be set BEFORE the cache — assigning source /
|
|
31
|
-
# material_id invalidates _textures + _fetched via Vis.__setattr__
|
|
32
|
-
# (3.1.1 cache-invalidation behavior).
|
|
33
|
-
m.vis.source = "ambientcg"
|
|
34
|
-
m.vis.material_id = "Metal032"
|
|
35
|
-
m.vis._textures = {
|
|
36
|
-
"color": b"\x89PNG_color",
|
|
37
|
-
"normal": b"\x89PNG_normal",
|
|
38
|
-
"roughness": b"\x89PNG_roughness",
|
|
39
|
-
}
|
|
40
|
-
m.vis._fetched = True
|
|
41
|
-
return m
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
class TestExtractScalars:
|
|
45
|
-
def test_from_pbr(self):
|
|
46
|
-
m = _make_material()
|
|
47
|
-
s = _extract_scalars(m)
|
|
48
|
-
assert s["metalness"] == 1.0
|
|
49
|
-
assert s["roughness"] == 0.3
|
|
50
|
-
assert s["ior"] == 2.5
|
|
51
|
-
|
|
52
|
-
def test_vis_wins_over_pbr(self):
|
|
53
|
-
m = _make_material()
|
|
54
|
-
m.vis.roughness = 0.5 # vis override
|
|
55
|
-
s = _extract_scalars(m)
|
|
56
|
-
assert s["roughness"] == 0.5 # vis wins
|
|
57
|
-
assert s["metalness"] == 1.0 # falls back to pbr
|
|
58
|
-
|
|
59
|
-
def test_metallic_mapped_to_metalness(self):
|
|
60
|
-
m = _make_material()
|
|
61
|
-
s = _extract_scalars(m)
|
|
62
|
-
assert "metalness" in s
|
|
63
|
-
assert "metallic" not in s
|
|
64
|
-
|
|
65
|
-
def test_base_color_emitted_as_color_hex(self):
|
|
66
|
-
"""mat-vis-client only formats color from color_hex — the extractor
|
|
67
|
-
must convert our RGBA list to '#RRGGBB' or the final Three.js dict
|
|
68
|
-
has no color at all (every material renders as default grey)."""
|
|
69
|
-
m = _make_material()
|
|
70
|
-
m.vis.base_color = (1.0, 0.0, 0.0, 1.0)
|
|
71
|
-
s = _extract_scalars(m)
|
|
72
|
-
assert s["color_hex"] == "#ff0000"
|
|
73
|
-
assert "base_color" not in s # RGBA form must not leak through
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
class TestRgbaToHex:
|
|
77
|
-
def test_pure_red(self):
|
|
78
|
-
assert _rgba_to_hex([1.0, 0.0, 0.0, 1.0]) == "#ff0000"
|
|
79
|
-
|
|
80
|
-
def test_pure_green(self):
|
|
81
|
-
assert _rgba_to_hex([0.0, 1.0, 0.0]) == "#00ff00" # alpha optional
|
|
82
|
-
|
|
83
|
-
def test_mid_grey(self):
|
|
84
|
-
assert _rgba_to_hex([0.5, 0.5, 0.5]) == "#808080"
|
|
85
|
-
|
|
86
|
-
def test_none_passthrough(self):
|
|
87
|
-
assert _rgba_to_hex(None) is None
|
|
88
|
-
|
|
89
|
-
def test_clamps_out_of_range(self):
|
|
90
|
-
# PBR values can over/undershoot; don't raise, just clamp
|
|
91
|
-
assert _rgba_to_hex([-0.1, 1.5, 0.5]) == "#00ff80"
|
|
92
|
-
|
|
93
|
-
def test_alpha_ignored(self):
|
|
94
|
-
assert _rgba_to_hex([1.0, 1.0, 1.0, 0.3]) == "#ffffff" # alpha stripped
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
class TestExtractTextures:
|
|
98
|
-
def test_no_vis_returns_empty(self):
|
|
99
|
-
m = _make_material(with_vis=False)
|
|
100
|
-
assert _extract_textures(m) == {}
|
|
101
|
-
|
|
102
|
-
def test_with_vis_returns_textures(self):
|
|
103
|
-
m = _make_material(with_vis=True)
|
|
104
|
-
tex = _extract_textures(m)
|
|
105
|
-
assert "color" in tex
|
|
106
|
-
assert tex["color"] == b"\x89PNG_color"
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
class TestToThreejs:
|
|
110
|
-
def test_scalar_only(self):
|
|
111
|
-
m = _make_material(with_vis=False)
|
|
112
|
-
d = to_threejs(m)
|
|
113
|
-
assert d["metalness"] == 1.0
|
|
114
|
-
assert d["roughness"] == 0.3
|
|
115
|
-
assert "map" not in d # no textures
|
|
116
|
-
|
|
117
|
-
def test_with_textures(self):
|
|
118
|
-
m = _make_material(with_vis=True)
|
|
119
|
-
d = to_threejs(m)
|
|
120
|
-
assert "map" in d # base64 data URI
|
|
121
|
-
assert d["map"].startswith("data:image/png;base64,")
|
|
122
|
-
assert "normalMap" in d
|
|
123
|
-
assert "roughnessMap" in d
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
class TestToGltf:
|
|
127
|
-
def test_scalar_only(self):
|
|
128
|
-
m = _make_material(with_vis=False)
|
|
129
|
-
d = to_gltf(m)
|
|
130
|
-
assert d["name"] == "Test Steel"
|
|
131
|
-
assert "pbrMetallicRoughness" in d
|
|
132
|
-
pbr = d["pbrMetallicRoughness"]
|
|
133
|
-
assert pbr["metallicFactor"] == 1.0
|
|
134
|
-
assert pbr["roughnessFactor"] == 0.3
|
|
135
|
-
|
|
136
|
-
def test_with_textures(self):
|
|
137
|
-
m = _make_material(with_vis=True)
|
|
138
|
-
d = to_gltf(m)
|
|
139
|
-
assert d["name"] == "Test Steel"
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
class TestExportMtlx:
|
|
143
|
-
def test_export_creates_files(self):
|
|
144
|
-
m = _make_material(with_vis=True)
|
|
145
|
-
with tempfile.TemporaryDirectory() as tmp:
|
|
146
|
-
mtlx_path = export_mtlx(m, Path(tmp))
|
|
147
|
-
assert mtlx_path.exists()
|
|
148
|
-
assert mtlx_path.suffix == ".mtlx"
|
|
149
|
-
# Check textures were written as PNG files
|
|
150
|
-
pngs = list(Path(tmp).glob("*.png"))
|
|
151
|
-
assert len(pngs) == 3 # color, normal, roughness
|
|
152
|
-
|
|
153
|
-
def test_export_no_textures(self):
|
|
154
|
-
m = _make_material(with_vis=False)
|
|
155
|
-
with tempfile.TemporaryDirectory() as tmp:
|
|
156
|
-
mtlx_path = export_mtlx(m, Path(tmp))
|
|
157
|
-
assert mtlx_path.exists()
|
|
158
|
-
pngs = list(Path(tmp).glob("*.png"))
|
|
159
|
-
assert len(pngs) == 0 # no textures
|
|
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
|