Skip to content

Commit b40e681

Browse files
committed
Moves pyosg-polyhaven.py to an importable pyosg_polyhaven.py
1 parent 7ae5d4d commit b40e681

2 files changed

Lines changed: 43 additions & 9 deletions

File tree

‎examples/manifest.cmake‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ set(PYOSG_OFFICIAL_EXAMPLES
6262
"pyosg-noise.py=noise.py"
6363
"pyosg-picking.py=picking.py"
6464
"pyosg-points.py=points.py"
65-
"pyosg-polyhaven.py=polyhaven.py"
65+
"pyosg_polyhaven.py=polyhaven.py"
6666
"pyosg-rtt.py=rtt.py"
6767
"pyosg-taa.py=taa.py"
6868
"pyosg-voronoi-reveal.py=voronoi_reveal.py"
Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
#!/usr/bin/env python3
22
#
3-
# pyosg-polyhaven.py worn_brick_wall --texture
4-
# pyosg-polyhaven.py https://polyhaven.com/a/worn_brick_wall --texture
5-
# pyosg-polyhaven.py /path/to/local.gltf --texture
6-
# pyosg-polyhaven.py worn_brick_wall --texture --res 4k
3+
# pyosg_polyhaven.py worn_brick_wall --texture
4+
# pyosg_polyhaven.py https://polyhaven.com/a/worn_brick_wall --texture
5+
# pyosg_polyhaven.py /path/to/local.gltf --texture
6+
# pyosg_polyhaven.py worn_brick_wall --texture --res 4k
77
#
8-
# pyosg-polyhaven.py qwantani_dusk_2 --hdr
9-
# pyosg-polyhaven.py https://polyhaven.com/a/qwantani_dusk_2 --hdr
10-
# pyosg-polyhaven.py /path/to/local.hdr --hdr
11-
# pyosg-polyhaven.py qwantani_dusk_2 --hdr --res 4k
8+
# pyosg_polyhaven.py qwantani_dusk_2 --hdr
9+
# pyosg_polyhaven.py https://polyhaven.com/a/qwantani_dusk_2 --hdr
10+
# pyosg_polyhaven.py /path/to/local.hdr --hdr
11+
# pyosg_polyhaven.py qwantani_dusk_2 --hdr --res 4k
12+
#
13+
# Installs as OpenSceneGraph/examples/polyhaven.py (manifest.cmake strips the pyosg_ prefix on
14+
# install, same as every other runnable demo here -- only the pure-library helpers like
15+
# pyosg_example.py keep it) -- import its download/cache functions via
16+
# `from OpenSceneGraph.examples import polyhaven`.
1217

1318
import sys
1419
import os
@@ -307,6 +312,35 @@ def download_polyhaven_texture(slug, res="2k"):
307312

308313
return gltf_path
309314

315+
# Pure JSON/path resolution, no OSG dependency - a caller building its own material (not just
316+
# reusing build_texture_root()'s sphere-preview scene below) still needs to know which downloaded
317+
# file is which. Resolved via the glTF's own material -> texture -> image indirection rather than
318+
# assumed by filename convention, since Polyhaven's own naming isn't perfectly consistent across
319+
# every asset. `orm` is Polyhaven's packed AO(R)/Roughness(G)/Metallic(B) texture, read via
320+
# metallicRoughnessTexture and falling back to occlusionTexture for assets that only declare that
321+
# slot. Raises KeyError if the glTF has no materials or is missing an expected texture.
322+
def resolve_pbr_textures(gltf_path):
323+
with open(gltf_path) as f:
324+
doc = json.load(f)
325+
326+
base_dir = os.path.dirname(gltf_path)
327+
material = doc["materials"][0]
328+
pbr = material["pbrMetallicRoughness"]
329+
330+
def image_path(texture_ref):
331+
texture = doc["textures"][texture_ref["index"]]
332+
image = doc["images"][texture["source"]]
333+
334+
return os.path.join(base_dir, image["uri"])
335+
336+
orm_ref = pbr.get("metallicRoughnessTexture") or material["occlusionTexture"]
337+
338+
return {
339+
"baseColor": image_path(pbr["baseColorTexture"]),
340+
"normal": image_path(material["normalTexture"]),
341+
"orm": image_path(orm_ref),
342+
}
343+
310344
def download_polyhaven_hdr(slug, res="2k"):
311345
dest_dir = os.path.join(CACHE_DIR, slug, res)
312346
hdr_path = os.path.join(dest_dir, f"{slug}_{res}.hdr")

0 commit comments

Comments
 (0)