threejs-materials 0.2.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.
- threejs_materials-0.2.0/LICENSE +190 -0
- threejs_materials-0.2.0/PKG-INFO +16 -0
- threejs_materials-0.2.0/README.md +385 -0
- threejs_materials-0.2.0/pyproject.toml +45 -0
- threejs_materials-0.2.0/setup.cfg +4 -0
- threejs_materials-0.2.0/src/threejs_materials/__init__.py +4 -0
- threejs_materials-0.2.0/src/threejs_materials/convert.py +774 -0
- threejs_materials-0.2.0/src/threejs_materials/library.py +396 -0
- threejs_materials-0.2.0/src/threejs_materials/sources/__init__.py +16 -0
- threejs_materials-0.2.0/src/threejs_materials/sources/ambientcg.py +63 -0
- threejs_materials-0.2.0/src/threejs_materials/sources/gpuopen.py +123 -0
- threejs_materials-0.2.0/src/threejs_materials/sources/physicallybased.py +175 -0
- threejs_materials-0.2.0/src/threejs_materials/sources/polyhaven.py +86 -0
- threejs_materials-0.2.0/src/threejs_materials/usd_reader.py +319 -0
- threejs_materials-0.2.0/src/threejs_materials.egg-info/PKG-INFO +16 -0
- threejs_materials-0.2.0/src/threejs_materials.egg-info/SOURCES.txt +20 -0
- threejs_materials-0.2.0/src/threejs_materials.egg-info/dependency_links.txt +1 -0
- threejs_materials-0.2.0/src/threejs_materials.egg-info/requires.txt +10 -0
- threejs_materials-0.2.0/src/threejs_materials.egg-info/top_level.txt +1 -0
- threejs_materials-0.2.0/tests/test_convert.py +979 -0
- threejs_materials-0.2.0/tests/test_library.py +264 -0
- threejs_materials-0.2.0/tests/test_usd.py +465 -0
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright 2025 materialx-lib contributors
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: threejs-materials
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Download PBR materials on demand and convert to Three.js MeshPhysicalMaterial JSON
|
|
5
|
+
License-Expression: Apache-2.0
|
|
6
|
+
Requires-Python: >=3.11
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Dist: materialx>=1.39.4
|
|
9
|
+
Requires-Dist: requests>=2.31.0
|
|
10
|
+
Requires-Dist: openexr>=3.3
|
|
11
|
+
Requires-Dist: pillow>=10.0
|
|
12
|
+
Provides-Extra: usd
|
|
13
|
+
Requires-Dist: usd-core>=24.11; extra == "usd"
|
|
14
|
+
Provides-Extra: test
|
|
15
|
+
Requires-Dist: pytest; extra == "test"
|
|
16
|
+
Dynamic: license-file
|
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
# threejs-materials
|
|
2
|
+
|
|
3
|
+
A Python library that converts PBR materials into [Three.js `MeshPhysicalMaterial`](https://threejs.org/docs/#api/en/materials/MeshPhysicalMaterial)-compatible JSON with base64-encoded textures.
|
|
4
|
+
|
|
5
|
+
The primary input format is [MaterialX](https://materialx.org/) — the library can download materials on demand from four open sources and bake procedural graphs into flat textures. For Blender / USD users, `Material.from_usd()` reads `UsdPreviewSurface` materials directly without baking.
|
|
6
|
+
|
|
7
|
+
<table>
|
|
8
|
+
<tr>
|
|
9
|
+
<td align="center"><strong>Studio mode</strong> (full PBR)</td>
|
|
10
|
+
<td align="center"><strong>CAD mode</strong> (<code>interpolate_color()</code>)</td>
|
|
11
|
+
</tr>
|
|
12
|
+
<tr>
|
|
13
|
+
<td><img src="screenshots/Studio mode.png" width="400"></td>
|
|
14
|
+
<td><img src="screenshots/CAD mode.png" width="400"></td>
|
|
15
|
+
</tr>
|
|
16
|
+
</table>
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
cd threejs-materials
|
|
22
|
+
uv sync # or: pip install -e .
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### Dependencies
|
|
26
|
+
|
|
27
|
+
- `materialx >= 1.39.4` — MaterialX SDK with TextureBaker
|
|
28
|
+
- `requests >= 2.31.0` — HTTP downloads
|
|
29
|
+
- `openexr >= 3.3` — EXR to PNG conversion
|
|
30
|
+
|
|
31
|
+
### Optional: USD support
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pip install threejs-materials[usd]
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
This installs `usd-core` for `Material.from_usd()`. Note that `usd-core` may not support the latest Python version.
|
|
38
|
+
|
|
39
|
+
## Output format
|
|
40
|
+
|
|
41
|
+
Both MaterialX and USD pipelines produce the same output format. Each property carries a `value`, a base64-encoded `texture`, or both:
|
|
42
|
+
|
|
43
|
+
```json
|
|
44
|
+
{
|
|
45
|
+
"id": "Car Paint",
|
|
46
|
+
"name": "Car Paint",
|
|
47
|
+
"source": "gpuopen",
|
|
48
|
+
"properties": {
|
|
49
|
+
"color": {
|
|
50
|
+
"value": [0.944, 0.776, 0.373],
|
|
51
|
+
"texture": "data:image/png;base64,..."
|
|
52
|
+
},
|
|
53
|
+
"metalness": { "value": 1.0 },
|
|
54
|
+
"roughness": { "value": 0.5, "texture": "data:image/png;base64,..." },
|
|
55
|
+
"normal": { "texture": "data:image/png;base64,..." },
|
|
56
|
+
"ior": { "value": 1.5 }
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Parametric materials (PhysicallyBased) have values only:
|
|
62
|
+
|
|
63
|
+
```json
|
|
64
|
+
{
|
|
65
|
+
"id": "Gold",
|
|
66
|
+
"name": "Gold",
|
|
67
|
+
"source": "physicallybased",
|
|
68
|
+
"properties": {
|
|
69
|
+
"color": { "value": [1.059, 0.773, 0.307] },
|
|
70
|
+
"metalness": { "value": 1.0 },
|
|
71
|
+
"roughness": { "value": 0.0 },
|
|
72
|
+
"ior": { "value": 1.5 }
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Property name mapping
|
|
78
|
+
|
|
79
|
+
Each output property maps to Three.js `MeshPhysicalMaterial` fields:
|
|
80
|
+
|
|
81
|
+
| Output property | `value` → | `texture` → |
|
|
82
|
+
| --------------------------- | --------------------------- | ---------------------------------------------------------- |
|
|
83
|
+
| `color` | `color` | `map` |
|
|
84
|
+
| `metalness` | `metalness` | `metalnessMap` |
|
|
85
|
+
| `roughness` | `roughness` | `roughnessMap` |
|
|
86
|
+
| `normal` | — | `normalMap` |
|
|
87
|
+
| `normalScale` | `normalScale` | — |
|
|
88
|
+
| `ao` | — | `aoMap` |
|
|
89
|
+
| `emissive` | `emissive` | `emissiveMap` |
|
|
90
|
+
| `emissiveIntensity` | `emissiveIntensity` | — |
|
|
91
|
+
| `ior` | `ior` | — |
|
|
92
|
+
| `transmission` | `transmission` | `transmissionMap` |
|
|
93
|
+
| `thickness` | `thickness` | `thicknessMap` |
|
|
94
|
+
| `attenuationColor` | `attenuationColor` | — |
|
|
95
|
+
| `attenuationDistance` | `attenuationDistance` | — |
|
|
96
|
+
| `clearcoat` | `clearcoat` | `clearcoatMap` |
|
|
97
|
+
| `clearcoatRoughness` | `clearcoatRoughness` | — |
|
|
98
|
+
| `clearcoatNormal` | — | `clearcoatNormalMap` |
|
|
99
|
+
| `sheen` | `sheen` | — |
|
|
100
|
+
| `sheenColor` | `sheenColor` | `sheenColorMap` |
|
|
101
|
+
| `sheenRoughness` | `sheenRoughness` | — |
|
|
102
|
+
| `iridescence` | `iridescence` | `iridescenceMap` |
|
|
103
|
+
| `iridescenceIOR` | `iridescenceIOR` | — |
|
|
104
|
+
| `iridescenceThicknessRange` | `iridescenceThicknessRange` | — |
|
|
105
|
+
| `anisotropy` | `anisotropy` | — |
|
|
106
|
+
| `anisotropyRotation` | `anisotropyRotation` | — |
|
|
107
|
+
| `specularIntensity` | `specularIntensity` | `specularIntensityMap` |
|
|
108
|
+
| `specularColor` | `specularColor` | `specularColorMap` |
|
|
109
|
+
| `opacity` | `opacity` | `alphaMap` |
|
|
110
|
+
| `transparent` | `transparent` | — |
|
|
111
|
+
| `alphaTest` | `alphaTest` | — |
|
|
112
|
+
| `dispersion` | `dispersion` | — |
|
|
113
|
+
| `displacement` | — | `displacementMap` |
|
|
114
|
+
| `displacementScale` | `displacementScale` | — |
|
|
115
|
+
| `side` | `side` | — |
|
|
116
|
+
| `metallicRoughness` | — | `metalnessMap` + `roughnessMap` (G=roughness, B=metalness) |
|
|
117
|
+
|
|
118
|
+
## Common API
|
|
119
|
+
|
|
120
|
+
### `material.override(**props, repeat=None) -> Material`
|
|
121
|
+
|
|
122
|
+
Return a new `Material` with property and/or texture repeat overrides. The original material is not modified.
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
mat = Material.gpuopen.load("Car Paint")
|
|
126
|
+
|
|
127
|
+
red_paint = mat.override(color=(0.8, 0.1, 0.1))
|
|
128
|
+
rough_red = mat.override(color=(0.8, 0.1, 0.1), roughness=0.9)
|
|
129
|
+
tiled = mat.override(repeat=(3, 3))
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Overrides set the `value` of the named property, creating it if absent. Existing textures are preserved. Calls can be chained: `mat.override(color=(1,0,0)).override(repeat=(2,2))`.
|
|
133
|
+
|
|
134
|
+
### `encode_texture_base64(file_path) -> str`
|
|
135
|
+
|
|
136
|
+
Encode an image file as a base64 data URI. Automatically converts EXR to PNG.
|
|
137
|
+
|
|
138
|
+
```python
|
|
139
|
+
from threejs_materials import encode_texture_base64
|
|
140
|
+
|
|
141
|
+
data_uri = encode_texture_base64("textures/normal.png")
|
|
142
|
+
# -> 'data:image/png;base64,iVBORw0KGgo...'
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### `material.interpolate_color() -> (r, g, b, a)`
|
|
146
|
+
|
|
147
|
+
Estimate a single representative sRGB color from a material — useful for CAD viewers that need a flat color per object while keeping a material dictionary for full PBR rendering.
|
|
148
|
+
|
|
149
|
+
```python
|
|
150
|
+
from threejs_materials import Material
|
|
151
|
+
|
|
152
|
+
wood = Material.gpuopen.load("Ivory Walnut Solid Wood")
|
|
153
|
+
|
|
154
|
+
materials = {"wood": wood} # keep for full PBR rendering
|
|
155
|
+
object.material = "wood"
|
|
156
|
+
object.color = wood.interpolate_color() # (0.53, 0.31, 0.18, 1.0)
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
When the material has a color texture, the texture is decoded and averaged (requires `Pillow`). Scalar colors (linear RGB) are converted to sRGB. Transmission and opacity are mapped to the alpha channel so glass-like materials appear semi-transparent.
|
|
160
|
+
|
|
161
|
+
### Three.js usage
|
|
162
|
+
|
|
163
|
+
```javascript
|
|
164
|
+
const data = JSON.parse(jsonStr);
|
|
165
|
+
const material = new THREE.MeshPhysicalMaterial();
|
|
166
|
+
|
|
167
|
+
for (const [key, prop] of Object.entries(data.properties)) {
|
|
168
|
+
if (prop.texture) {
|
|
169
|
+
material[key] = new THREE.TextureLoader().load(prop.texture);
|
|
170
|
+
}
|
|
171
|
+
if (prop.value !== undefined) {
|
|
172
|
+
if (Array.isArray(prop.value) && prop.value.length === 3) {
|
|
173
|
+
material[key] = new THREE.Color(...prop.value);
|
|
174
|
+
} else {
|
|
175
|
+
material[key] = prop.value;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### Consumer notes
|
|
182
|
+
|
|
183
|
+
- **Color space**: Textures include a `colorSpace` field when available. Three.js expects color textures (baseColor, emissive, sheenColor, specularColor) in **sRGB** and data textures (roughness, metalness, normal, AO, displacement) in **linear**. Set `texture.colorSpace` accordingly.
|
|
184
|
+
- **Normal maps**: Baked using the OpenGL convention (Y-up), matching Three.js and glTF expectations.
|
|
185
|
+
- **Scalar x texture**: When both `value` and `texture` are present, Three.js multiplies them. The library sets scalars to `1.0` (neutral) when a texture is present so the texture controls fully.
|
|
186
|
+
- **glTF packed metallicRoughness**: When present, the `metallicRoughness` property carries a single packed texture (G=roughness, B=metalness). The consumer must assign it to both `metalnessMap` and `roughnessMap`.
|
|
187
|
+
|
|
188
|
+
---
|
|
189
|
+
|
|
190
|
+
## MaterialX
|
|
191
|
+
|
|
192
|
+
### Sources
|
|
193
|
+
|
|
194
|
+
| Source | Type | Shader model |
|
|
195
|
+
| -------------------------------------------------------- | ------------------------ | ------------------ |
|
|
196
|
+
| [ambientCG](https://ambientcg.com/) | Texture-based | `open_pbr_surface` |
|
|
197
|
+
| [GPUOpen MaterialX Library](https://matlib.gpuopen.com/) | Procedural (baked) | `standard_surface` |
|
|
198
|
+
| [PolyHaven](https://polyhaven.com/) | Texture-based | `standard_surface` |
|
|
199
|
+
| [PhysicallyBased](https://physicallybased.info/) | Parametric (no textures) | `open_pbr_surface` |
|
|
200
|
+
|
|
201
|
+
Browse materials on the source websites, then load them by name.
|
|
202
|
+
|
|
203
|
+
### `Material.{source}.load(name, resolution="1K") -> Material`
|
|
204
|
+
|
|
205
|
+
Download, convert, and cache a material.
|
|
206
|
+
|
|
207
|
+
```python
|
|
208
|
+
from threejs_materials import Material
|
|
209
|
+
|
|
210
|
+
mat = Material.gpuopen.load("Car Paint", resolution="1K")
|
|
211
|
+
mat = Material.ambientcg.load("Onyx015", resolution="1K")
|
|
212
|
+
mat = Material.polyhaven.load("plank_flooring_04", resolution="1K")
|
|
213
|
+
mat = Material.physicallybased.load("Titanium")
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
The first call downloads and converts the material (takes a few seconds). Subsequent calls return the cached JSON instantly from `~/.materialx-cache/`.
|
|
217
|
+
|
|
218
|
+
#### Resolution
|
|
219
|
+
|
|
220
|
+
Pass a normalized resolution (`1K`, `2K`, `4K`, `8K` — case-insensitive). Each source maps it to its native format:
|
|
221
|
+
|
|
222
|
+
| Input | GPUOpen | ambientCG | PolyHaven | PhysicallyBased |
|
|
223
|
+
| ----- | ------- | --------- | --------- | --------------- |
|
|
224
|
+
| 1K | 1k 8b | 1K-PNG | 1k | n/a |
|
|
225
|
+
| 2K | 2k 8b | 2K-PNG | 2k | n/a |
|
|
226
|
+
| 4K | 4k 8b | 4K-PNG | 4k | n/a |
|
|
227
|
+
| 8K | — | 8K-PNG | 8k | n/a |
|
|
228
|
+
|
|
229
|
+
PhysicallyBased materials are parametric — no resolution needed (and not accepted).
|
|
230
|
+
|
|
231
|
+
### `Material.list_sources()`
|
|
232
|
+
|
|
233
|
+
Print available sources with clickable URLs.
|
|
234
|
+
|
|
235
|
+
```python
|
|
236
|
+
from threejs_materials import Material
|
|
237
|
+
|
|
238
|
+
Material.list_sources()
|
|
239
|
+
# Material sources:
|
|
240
|
+
# Material.ambientCG https://ambientcg.com/list?type=material
|
|
241
|
+
# Material.GPUOpen https://matlib.gpuopen.com/main/materials/all
|
|
242
|
+
# Material.PolyHaven https://polyhaven.com/textures
|
|
243
|
+
# Material.PhysicallyBased https://physicallybased.info/
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
### `Material.from_mtlx(mtlx_file) -> Material`
|
|
247
|
+
|
|
248
|
+
Convert a local `.mtlx` file without downloading anything.
|
|
249
|
+
|
|
250
|
+
```python
|
|
251
|
+
from threejs_materials import Material
|
|
252
|
+
|
|
253
|
+
mat = Material.from_mtlx("examples/gpuo-car-paint.mtlx")
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
Texture paths in the `.mtlx` are resolved relative to the file's location.
|
|
257
|
+
|
|
258
|
+
### Conversion pipeline
|
|
259
|
+
|
|
260
|
+
When a material is loaded:
|
|
261
|
+
|
|
262
|
+
1. **Download** — source-specific: fetch ZIP (ambientCG, GPUOpen), individual files (PolyHaven), or generate from parameters (PhysicallyBased)
|
|
263
|
+
2. **Bake** — run MaterialX `TextureBaker` (GLSL preferred, MSL fallback on macOS) to flatten procedural graphs into texture images
|
|
264
|
+
3. **Fallback merge** — if the baker can't handle certain textures, merge from the original document
|
|
265
|
+
4. **EXR to PNG** — convert any EXR textures to 8-bit PNG
|
|
266
|
+
5. **Extract** — map shader inputs to `MeshPhysicalMaterial` properties with base64-encoded textures
|
|
267
|
+
6. **Cache** — write JSON to `~/.materialx-cache/`
|
|
268
|
+
|
|
269
|
+
### Cache
|
|
270
|
+
|
|
271
|
+
Converted materials are cached as flat JSON files in `~/.materialx-cache/`:
|
|
272
|
+
|
|
273
|
+
```
|
|
274
|
+
~/.materialx-cache/
|
|
275
|
+
gpuopen_car_paint_1k_8b.json
|
|
276
|
+
ambientcg_onyx015_1k-png.json
|
|
277
|
+
polyhaven_plank_flooring_04_1k.json
|
|
278
|
+
physicallybased_titanium.json
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
To force re-conversion, delete the cached file and call `.load()` again.
|
|
282
|
+
|
|
283
|
+
### Shader model coverage
|
|
284
|
+
|
|
285
|
+
Supported models: `standard_surface`, `gltf_pbr`, `open_pbr_surface`. Other models produce empty output with a logged warning.
|
|
286
|
+
|
|
287
|
+
| Feature | standard_surface | gltf_pbr | open_pbr_surface |
|
|
288
|
+
| ---------------- | --------------------------------------- | ------------------------ | ------------------------ |
|
|
289
|
+
| Base color | Yes | Yes | Yes |
|
|
290
|
+
| Metalness | Yes | Yes | Yes |
|
|
291
|
+
| Roughness | Yes | Yes | Yes |
|
|
292
|
+
| Normal map | Yes | Yes | Yes |
|
|
293
|
+
| Specular | Yes (weight, color, IOR) | Yes (weight, color, IOR) | Yes (weight, color, IOR) |
|
|
294
|
+
| Transmission | Yes | Yes (+ attenuation) | Yes (+ attenuation) |
|
|
295
|
+
| Emission | Yes | Yes | Yes |
|
|
296
|
+
| Clearcoat | Yes | Yes | Yes |
|
|
297
|
+
| Clearcoat normal | Yes | Yes | Yes |
|
|
298
|
+
| Sheen | Yes | Yes | Yes (fuzz) |
|
|
299
|
+
| Iridescence | Yes | Yes | Yes |
|
|
300
|
+
| Anisotropy | Yes (scalar — no Three.js strength map) | Yes | Yes |
|
|
301
|
+
| Opacity | Yes | Yes (alpha/alpha_mode) | Yes (geometry_opacity) |
|
|
302
|
+
| Displacement | Yes (model-independent) | Yes | Yes |
|
|
303
|
+
| Dispersion | No | Yes | Yes |
|
|
304
|
+
| Normal scale | No (baked into texture) | Yes | No (baked into texture) |
|
|
305
|
+
| Thin-walled | No | No | Yes (→ DoubleSide) |
|
|
306
|
+
| Subsurface | No | No | No |
|
|
307
|
+
|
|
308
|
+
Subsurface scattering is not mapped — Three.js `MeshPhysicalMaterial` has no SSS support.
|
|
309
|
+
|
|
310
|
+
### MaterialX limitations
|
|
311
|
+
|
|
312
|
+
#### Materials
|
|
313
|
+
|
|
314
|
+
- **Single material per document** — only the first material is used when a `.mtlx` file contains multiple materials. A warning is logged.
|
|
315
|
+
- **First shader node** — if a material has multiple shader nodes (e.g. surface + volume), only the first surface shader is extracted.
|
|
316
|
+
|
|
317
|
+
#### Baking
|
|
318
|
+
|
|
319
|
+
- **8-bit textures** — the TextureBaker uses `UINT8` output. HDR information (emissive, HDR environment lighting baked into textures) is clamped to [0,1]. This is acceptable for web preview but lossy for physically accurate emissive maps.
|
|
320
|
+
- **Global bake lock** — baking operations are serialized via a `threading.Lock` because the MaterialX baker requires `os.chdir`. This is thread-safe but becomes a bottleneck under concurrent load. The lock is per-process only (`threading.Lock`, not `multiprocessing.Lock`).
|
|
321
|
+
- **Geometry-dependent nodes** — procedurals driven by `<position>`, `<normal>`, or `<tangent>` cannot be baked (the baker renders on a flat UV quad with no 3D geometry).
|
|
322
|
+
|
|
323
|
+
#### Image tracing
|
|
324
|
+
|
|
325
|
+
- **Single upstream image** — `find_upstream_image` returns the first image node found when walking upstream. Complex graphs with multiple images (layered blends, channel packing before baking) will only capture one image. After baking, this is fine since the baker flattens everything to single `<image>` nodes.
|
|
326
|
+
- **No channel extraction tracking** — when an image passes through `extract` or `swizzle` nodes, the specific channel being used is not recorded. The consumer must know glTF metallicRoughness packing conventions (G=roughness, B=metalness).
|
|
327
|
+
|
|
328
|
+
#### EXR conversion
|
|
329
|
+
|
|
330
|
+
- **LDR clamp** — EXR textures are clamped to [0,1] and converted to 8-bit PNG. Dynamic range beyond 1.0 is lost.
|
|
331
|
+
- **Channel naming** — EXR files with non-standard channel names (not R/G/B/A) fall back to source-order channel selection, which may produce incorrect color mappings for unusual EXR layouts.
|
|
332
|
+
|
|
333
|
+
#### Network
|
|
334
|
+
|
|
335
|
+
- **No retry logic** — a single network failure raises an exception. The caller is responsible for retries.
|
|
336
|
+
- **GPUOpen pagination** — the material search assumes all results fit in one API page. Materials not in the first page may not be found.
|
|
337
|
+
- **GPUOpen sequential package lookup** — each package UUID is queried individually; materials with many packages may be slow to resolve.
|
|
338
|
+
|
|
339
|
+
#### Caching
|
|
340
|
+
|
|
341
|
+
- **No cache invalidation** — cached materials are never automatically refreshed. Delete the cache file manually to force re-conversion.
|
|
342
|
+
|
|
343
|
+
---
|
|
344
|
+
|
|
345
|
+
## USD
|
|
346
|
+
|
|
347
|
+
For Blender users and other USD workflows: `Material.from_usd()` reads `UsdPreviewSurface` materials directly from USD files. No MaterialX baking is needed since UsdPreviewSurface is already a flat PBR shader.
|
|
348
|
+
|
|
349
|
+
### `Material.from_usd(usd_file) -> Material`
|
|
350
|
+
|
|
351
|
+
Load a USD file (`.usda`, `.usdc`, `.usdz`) with `UsdPreviewSurface` materials.
|
|
352
|
+
|
|
353
|
+
```python
|
|
354
|
+
from threejs_materials import Material
|
|
355
|
+
|
|
356
|
+
mat = Material.from_usd("model.usda")
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
Textures are resolved relative to the file location. USDZ archives with embedded textures are supported. Both metallic and specular workflows are handled (`useSpecularWorkflow` input).
|
|
360
|
+
|
|
361
|
+
### UsdPreviewSurface input mapping
|
|
362
|
+
|
|
363
|
+
| UsdPreviewSurface input | Output property | Notes |
|
|
364
|
+
| ----------------------- | -------------------- | ---------------------------------- |
|
|
365
|
+
| `diffuseColor` | `color` | value + texture |
|
|
366
|
+
| `metallic` | `metalness` | value + texture |
|
|
367
|
+
| `roughness` | `roughness` | value + texture |
|
|
368
|
+
| `normal` | `normal` | texture only |
|
|
369
|
+
| `emissiveColor` | `emissive` | value + texture |
|
|
370
|
+
| `clearcoat` | `clearcoat` | value only |
|
|
371
|
+
| `clearcoatRoughness` | `clearcoatRoughness` | value only |
|
|
372
|
+
| `ior` | `ior` | value only |
|
|
373
|
+
| `occlusion` | `ao` | texture only |
|
|
374
|
+
| `displacement` | `displacement` | texture only |
|
|
375
|
+
| `opacity` | `opacity` | + `transparent: true` if < 1 |
|
|
376
|
+
| `opacityThreshold` | `alphaTest` | mask/cutout mode |
|
|
377
|
+
| `specularColor` | `specularColor` | only if `useSpecularWorkflow == 1` |
|
|
378
|
+
|
|
379
|
+
Inputs at their UsdPreviewSurface default value are omitted from the output.
|
|
380
|
+
|
|
381
|
+
### USD limitations
|
|
382
|
+
|
|
383
|
+
- **Single material per file** — only the first `UsdPreviewSurface` material is used when a file contains multiple materials. A warning is logged.
|
|
384
|
+
- **No UV transform support** — `UsdTransform2d` nodes are not read; texture coordinates are assumed to be used as-is.
|
|
385
|
+
- **Emission intensity** — UsdPreviewSurface has no emission intensity input; `emissiveColor` maps directly to `emissive`.
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "threejs-materials"
|
|
3
|
+
version = "0.2.0"
|
|
4
|
+
# version also managed by [tool.bumpversion] below
|
|
5
|
+
description = "Download PBR materials on demand and convert to Three.js MeshPhysicalMaterial JSON"
|
|
6
|
+
requires-python = ">=3.11"
|
|
7
|
+
license = "Apache-2.0"
|
|
8
|
+
dependencies = [
|
|
9
|
+
"materialx>=1.39.4",
|
|
10
|
+
"requests>=2.31.0",
|
|
11
|
+
"openexr>=3.3",
|
|
12
|
+
"pillow>=10.0",
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
[project.optional-dependencies]
|
|
16
|
+
usd = ["usd-core>=24.11"]
|
|
17
|
+
test = ["pytest"]
|
|
18
|
+
|
|
19
|
+
[build-system]
|
|
20
|
+
requires = ["setuptools"]
|
|
21
|
+
build-backend = "setuptools.build_meta"
|
|
22
|
+
|
|
23
|
+
[tool.setuptools.packages.find]
|
|
24
|
+
where = ["src"]
|
|
25
|
+
|
|
26
|
+
# bump-my-version settings
|
|
27
|
+
|
|
28
|
+
[tool.bumpversion]
|
|
29
|
+
current_version = "0.2.0"
|
|
30
|
+
commit = false
|
|
31
|
+
tag = false
|
|
32
|
+
parse = "(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)"
|
|
33
|
+
serialize = ["{major}.{minor}.{patch}"]
|
|
34
|
+
allow_dirty = true
|
|
35
|
+
message = "Bump version: {current_version} → {new_version}"
|
|
36
|
+
search = "{current_version}"
|
|
37
|
+
replace = "{new_version}"
|
|
38
|
+
|
|
39
|
+
[[tool.bumpversion.files]]
|
|
40
|
+
filename = "pyproject.toml"
|
|
41
|
+
search = 'version = "{current_version}"'
|
|
42
|
+
replace = 'version = "{new_version}"'
|
|
43
|
+
|
|
44
|
+
[tool.pytest.ini_options]
|
|
45
|
+
testpaths = ["tests"]
|