hstream-3d 0.1.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.
@@ -0,0 +1,172 @@
1
+ # files sometimes used for local testing
2
+ /Dockerfile
3
+ /example.py
4
+ test-env/
5
+
6
+ # Auto-generated version file
7
+
8
+ # Byte-compiled / optimized / DLL files
9
+ __pycache__/
10
+ *.py[cod]
11
+ *$py.class
12
+
13
+ # C extensions
14
+ *.so
15
+
16
+ # Distribution / packaging
17
+ .Python
18
+ build/
19
+ develop-eggs/
20
+ dist/
21
+ downloads/
22
+ eggs/
23
+ .eggs/
24
+ lib/
25
+ lib64/
26
+ parts/
27
+ sdist/
28
+ var/
29
+ wheels/
30
+ share/python-wheels/
31
+ *.egg-info/
32
+ .installed.cfg
33
+ *.egg
34
+ MANIFEST
35
+
36
+ # PyInstaller
37
+ # Usually these files are written by a python script from a template
38
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
39
+ *.manifest
40
+ *.spec
41
+
42
+ # Installer logs
43
+ pip-log.txt
44
+ pip-delete-this-directory.txt
45
+
46
+ # Unit test / coverage reports
47
+ htmlcov/
48
+ .tox/
49
+ .nox/
50
+ .coverage
51
+ .coverage.*
52
+ .cache
53
+ nosetests.xml
54
+ coverage.xml
55
+ *.cover
56
+ *.py,cover
57
+ .hypothesis/
58
+ .pytest_cache/
59
+ cover/
60
+
61
+ # Translations
62
+ *.mo
63
+ *.pot
64
+
65
+ # Django stuff:
66
+ *.log
67
+ local_settings.py
68
+ db.sqlite3
69
+ db.sqlite3-journal
70
+
71
+ # Flask stuff:
72
+ instance/
73
+ .webassets-cache
74
+
75
+ # Scrapy stuff:
76
+ .scrapy
77
+
78
+ # Sphinx documentation
79
+ docs/_build/
80
+
81
+ # PyBuilder
82
+ .pybuilder/
83
+ target/
84
+
85
+ # Jupyter Notebook
86
+ .ipynb_checkpoints
87
+
88
+ # IPython
89
+ profile_default/
90
+ ipython_config.py
91
+
92
+ # pyenv
93
+ # For a library or package, you might want to ignore these files since the code is
94
+ # intended to run in multiple environments; otherwise, check them in:
95
+ # .python-version
96
+
97
+ # pipenv
98
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
99
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
100
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
101
+ # install all needed dependencies.
102
+ #Pipfile.lock
103
+
104
+ # poetry
105
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
106
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
107
+ # commonly ignored for libraries.
108
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
109
+ #poetry.lock
110
+
111
+ # pdm
112
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
113
+ #pdm.lock
114
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
115
+ # in version control.
116
+ # https://pdm.fming.dev/#use-with-ide
117
+ .pdm.toml
118
+
119
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
120
+ __pypackages__/
121
+
122
+ # Celery stuff
123
+ celerybeat-schedule
124
+ celerybeat.pid
125
+
126
+ # SageMath parsed files
127
+ *.sage.py
128
+
129
+ # Environments
130
+ .env
131
+ .venv
132
+ env/
133
+ venv/
134
+ ENV/
135
+ env.bak/
136
+ venv.bak/
137
+
138
+ # Spyder project settings
139
+ .spyderproject
140
+ .spyproject
141
+
142
+ # Rope project settings
143
+ .ropeproject
144
+
145
+ # mkdocs documentation
146
+ /site
147
+
148
+ # mypy
149
+ .mypy_cache/
150
+ .dmypy.json
151
+ dmypy.json
152
+
153
+ # Pyre type checker
154
+ .pyre/
155
+
156
+ # pytype static type analyzer
157
+ .pytype/
158
+
159
+ # Cython debug symbols
160
+ cython_debug/
161
+
162
+ # PyCharm
163
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
164
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
165
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
166
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
167
+ #.idea/
168
+ .aider*
169
+ test_strategy_logs.txt
170
+
171
+ # hs.file_upload saves uploads here
172
+ tmp/
@@ -0,0 +1,90 @@
1
+ Metadata-Version: 2.5
2
+ Name: hstream-3d
3
+ Version: 0.1.0
4
+ Summary: 3D model viewers for HStream apps (Google <model-viewer> and Babylon.js Viewer v2), with click picking and annotations.
5
+ Project-URL: Repository, https://github.com/conradbez/hstream
6
+ Requires-Python: >=3.10
7
+ Requires-Dist: hstream>=0.9.0
8
+ Description-Content-Type: text/markdown
9
+
10
+ # hstream-3d
11
+
12
+ 3D model viewers for [HStream](../README.md) apps, as `viewer3d.*`:
13
+ Google's [`<model-viewer>`](https://modelviewer.dev) and
14
+ [Babylon.js Viewer v2](https://doc.babylonjs.com/toolsAndResources/viewerV2).
15
+ Click the model and your script gets the 3D point that was hit.
16
+
17
+ ```bash
18
+ pip install "hstream[3d]"
19
+ ```
20
+
21
+ ```python
22
+ from hstream import hs, viewer3d
23
+
24
+ pick = viewer3d.model_viewer(
25
+ "https://modelviewer.dev/shared-assets/models/Astronaut.glb",
26
+ show_pick="You clicked here",
27
+ )
28
+ if pick:
29
+ hs.markdown(f"point {pick.point}, normal {pick.normal}")
30
+ ```
31
+
32
+ `viewer3d.babylon(source, ...)` works the same way. Each viewer's script
33
+ loads from its CDN, only on pages that use it.
34
+
35
+ ## Picking
36
+
37
+ A click (not an orbit drag) posts the hit; the viewer returns a `Pick`, or
38
+ `None` before the first click:
39
+
40
+ | Field | `model_viewer` | `babylon` |
41
+ | :--- | :--- | :--- |
42
+ | `point`, `normal` | model space | world space |
43
+ | `screen` | pixels from the viewer's top-left | same |
44
+ | `material` | material name | material name |
45
+ | `mesh`, `face` | not reported | mesh name, triangle index |
46
+ | `uv` | texture coordinates | texture coordinates |
47
+ | `surface` | `data-surface` id | surface hotspot (animated meshes only) |
48
+
49
+ Coordinates are glTF units (meters). A CAD model's "faces" are not glTF
50
+ triangles: `face` is the triangle, so map back to CAD faces yourself (for
51
+ example by material or mesh name).
52
+
53
+ ## Annotations
54
+
55
+ ```python
56
+ from hstream_3d import Annotation
57
+
58
+ viewer3d.babylon(
59
+ url,
60
+ annotations=[
61
+ ((0, 1.2, 0), "Head"), # (point, label)
62
+ Annotation(pick, "Picked"), # from a click
63
+ ],
64
+ )
65
+ ```
66
+
67
+ `show_pick=True` (or a label) pins the latest click. Pins are the viewers'
68
+ own hotspots, so they stay on the model as it rotates.
69
+
70
+ ## Local files
71
+
72
+ `src`/`source` can be a local `.glb` (a `Path`, or a path string that
73
+ exists). HStream serves no files, so it is sent inline as a `data:` URL:
74
+ fine up to a few MB. Use self-contained `.glb`, not `.gltf` with separate
75
+ buffers. A changed file is picked up on the next rerun.
76
+
77
+ [`examples/build123d_part.py`](examples/build123d_part.py) builds a
78
+ parametric part with [build123d](https://github.com/gumyr/build123d) on every
79
+ input change, saves each build to `examples/models/`, and falls back to the
80
+ last saved model when build123d isn't installed or a build fails.
81
+
82
+ ## Over plain HTTP (agents, curl)
83
+
84
+ `POST /set_component_value?component_id=<key>` with `new_value` set to
85
+ JSON: `{"point": [x, y, z], "normal": [x, y, z]}` (other fields optional).
86
+
87
+ ## Examples
88
+
89
+ - `hstream run examples/pick_demo.py`: both viewers, click to pin a label.
90
+ - `hstream run examples/build123d_part.py`: parametric CAD part.
@@ -0,0 +1,81 @@
1
+ # hstream-3d
2
+
3
+ 3D model viewers for [HStream](../README.md) apps, as `viewer3d.*`:
4
+ Google's [`<model-viewer>`](https://modelviewer.dev) and
5
+ [Babylon.js Viewer v2](https://doc.babylonjs.com/toolsAndResources/viewerV2).
6
+ Click the model and your script gets the 3D point that was hit.
7
+
8
+ ```bash
9
+ pip install "hstream[3d]"
10
+ ```
11
+
12
+ ```python
13
+ from hstream import hs, viewer3d
14
+
15
+ pick = viewer3d.model_viewer(
16
+ "https://modelviewer.dev/shared-assets/models/Astronaut.glb",
17
+ show_pick="You clicked here",
18
+ )
19
+ if pick:
20
+ hs.markdown(f"point {pick.point}, normal {pick.normal}")
21
+ ```
22
+
23
+ `viewer3d.babylon(source, ...)` works the same way. Each viewer's script
24
+ loads from its CDN, only on pages that use it.
25
+
26
+ ## Picking
27
+
28
+ A click (not an orbit drag) posts the hit; the viewer returns a `Pick`, or
29
+ `None` before the first click:
30
+
31
+ | Field | `model_viewer` | `babylon` |
32
+ | :--- | :--- | :--- |
33
+ | `point`, `normal` | model space | world space |
34
+ | `screen` | pixels from the viewer's top-left | same |
35
+ | `material` | material name | material name |
36
+ | `mesh`, `face` | not reported | mesh name, triangle index |
37
+ | `uv` | texture coordinates | texture coordinates |
38
+ | `surface` | `data-surface` id | surface hotspot (animated meshes only) |
39
+
40
+ Coordinates are glTF units (meters). A CAD model's "faces" are not glTF
41
+ triangles: `face` is the triangle, so map back to CAD faces yourself (for
42
+ example by material or mesh name).
43
+
44
+ ## Annotations
45
+
46
+ ```python
47
+ from hstream_3d import Annotation
48
+
49
+ viewer3d.babylon(
50
+ url,
51
+ annotations=[
52
+ ((0, 1.2, 0), "Head"), # (point, label)
53
+ Annotation(pick, "Picked"), # from a click
54
+ ],
55
+ )
56
+ ```
57
+
58
+ `show_pick=True` (or a label) pins the latest click. Pins are the viewers'
59
+ own hotspots, so they stay on the model as it rotates.
60
+
61
+ ## Local files
62
+
63
+ `src`/`source` can be a local `.glb` (a `Path`, or a path string that
64
+ exists). HStream serves no files, so it is sent inline as a `data:` URL:
65
+ fine up to a few MB. Use self-contained `.glb`, not `.gltf` with separate
66
+ buffers. A changed file is picked up on the next rerun.
67
+
68
+ [`examples/build123d_part.py`](examples/build123d_part.py) builds a
69
+ parametric part with [build123d](https://github.com/gumyr/build123d) on every
70
+ input change, saves each build to `examples/models/`, and falls back to the
71
+ last saved model when build123d isn't installed or a build fails.
72
+
73
+ ## Over plain HTTP (agents, curl)
74
+
75
+ `POST /set_component_value?component_id=<key>` with `new_value` set to
76
+ JSON: `{"point": [x, y, z], "normal": [x, y, z]}` (other fields optional).
77
+
78
+ ## Examples
79
+
80
+ - `hstream run examples/pick_demo.py`: both viewers, click to pin a label.
81
+ - `hstream run examples/build123d_part.py`: parametric CAD part.
@@ -0,0 +1,101 @@
1
+ """A parametric part built with build123d and shown in 3D.
2
+
3
+ hstream run build123d_part.py
4
+
5
+ build123d is optional (`pip install build123d`). Each parameter change tries
6
+ to import it and build the part. Every build is saved to ./models/ under a
7
+ name made from its parameters, so:
8
+
9
+ - a combination built before loads from disk without rebuilding,
10
+ - without build123d, or when a build fails, the last good model still shows,
11
+ - the default part ships with the example, so it shows on a fresh checkout.
12
+ """
13
+
14
+ import hashlib
15
+ import json
16
+ import os
17
+ from pathlib import Path
18
+
19
+ from hstream import hs, viewer3d
20
+
21
+ MODELS = Path(__file__).parent / "models"
22
+
23
+
24
+ def model_path(params: dict) -> Path:
25
+ digest = hashlib.sha1(json.dumps(params, sort_keys=True).encode()).hexdigest()
26
+ return MODELS / f"plate-{digest[:12]}.glb"
27
+
28
+
29
+ def build(params: dict, path: Path) -> None:
30
+ """Build the plate and save it as a GLB. Raises ImportError without build123d."""
31
+ from build123d import Axis, Box, Color, Cylinder, Pos, export_gltf, fillet
32
+
33
+ length, width, thickness = params["length"], params["width"], params["thickness"]
34
+ part = Box(length, width, thickness)
35
+ if params["fillet"] > 0:
36
+ part = fillet(part.edges().filter_by(Axis.Z), radius=params["fillet"])
37
+ holes = params["holes"]
38
+ for i in range(holes):
39
+ x = -length / 2 + length * (i + 1) / (holes + 1)
40
+ part -= Pos(x, 0, 0) * Cylinder(params["hole_diameter"] / 2, thickness)
41
+ part.color = Color(0.36, 0.52, 0.68) # exported as the glTF material colour
42
+ MODELS.mkdir(exist_ok=True)
43
+ tmp = path.with_suffix(".tmp")
44
+ export_gltf(part, str(tmp), binary=True)
45
+ tmp.replace(path) # never leave a half-written model behind
46
+
47
+
48
+ def last_good() -> Path | None:
49
+ """The most recently built or shown model (newest file in ./models)."""
50
+ return max(MODELS.glob("*.glb"), key=lambda p: p.stat().st_mtime, default=None)
51
+
52
+
53
+ hs.markdown("# Parametric plate (build123d)")
54
+ with hs.grid():
55
+ length = hs.number_input("Length (mm)", 80, key="length")
56
+ width = hs.number_input("Width (mm)", 40, key="width")
57
+ thickness = hs.number_input("Thickness (mm)", 6, key="thickness")
58
+ with hs.grid():
59
+ holes = hs.slider("Holes", 0, 6, 3, key="holes")
60
+ hole_diameter = hs.number_input("Hole diameter (mm)", 8, key="hole_diameter")
61
+ fillet_radius = hs.number_input("Corner fillet (mm)", 6, key="fillet")
62
+ viewer = hs.select_box(["model-viewer", "babylon"], "model-viewer", key="viewer")
63
+
64
+ params = {
65
+ "length": length,
66
+ "width": width,
67
+ "thickness": thickness,
68
+ "holes": holes,
69
+ "hole_diameter": hole_diameter,
70
+ "fillet": fillet_radius,
71
+ }
72
+ path = model_path(params)
73
+ status = hs.placeholder()
74
+
75
+ if path.is_file():
76
+ os.utime(path) # now the latest good model
77
+ else:
78
+ try:
79
+ build(params, path)
80
+ except ImportError:
81
+ status.markdown(
82
+ "build123d isn't installed (`pip install build123d`), so these "
83
+ "parameters can't be built. Showing the last saved model."
84
+ )
85
+ except Exception as error: # a bad combination, e.g. a fillet too large
86
+ status.markdown(
87
+ f"Couldn't build this part: `{error}`. Showing the last good one."
88
+ )
89
+
90
+ shown = path if path.is_file() else last_good()
91
+ if shown is None:
92
+ hs.markdown("No saved model yet: install build123d to build one.")
93
+ else:
94
+ show = viewer3d.model_viewer if viewer == "model-viewer" else viewer3d.babylon
95
+ pick = show(shown, key=f"view_{viewer}", height="420px", show_pick=True)
96
+ if shown != path:
97
+ hs.markdown(f"Showing `{shown.name}`.")
98
+ if pick:
99
+ hs.markdown(
100
+ f"Clicked at `{pick.point}` (glTF units: meters), normal `{pick.normal}`."
101
+ )
@@ -0,0 +1,4 @@
1
+ # Builds made by build123d_part.py; only the default part is kept in git.
2
+ *
3
+ !.gitignore
4
+ !plate-a95450e534c5.glb
@@ -0,0 +1,33 @@
1
+ """Click a 3D model to pin a label there: `hstream run pick_demo.py`."""
2
+
3
+ from hstream import hs, viewer3d
4
+
5
+ ASTRONAUT = "https://modelviewer.dev/shared-assets/models/Astronaut.glb"
6
+ BOOMBOX = "https://assets.babylonjs.com/meshes/boombox.glb"
7
+
8
+ hs.markdown("# Click the model to drop a pin")
9
+ label = hs.text_input("Pin label", "Here", key="label")
10
+
11
+ hs.markdown("## Google `<model-viewer>`")
12
+ pick = viewer3d.model_viewer(ASTRONAUT, key="mv", height="360px", show_pick=label)
13
+ if pick:
14
+ hs.markdown(
15
+ f"point `{pick.point}` · normal `{pick.normal}` · "
16
+ f"material `{pick.material}` · screen `{pick.screen}`"
17
+ )
18
+
19
+ # Photograph the viewer as it looks now (camera angle, pin and all); the
20
+ # photo shows under the button.
21
+ snapshot = hs.screenshot(
22
+ "Take a photo of the model", target="#container_for_mv", key="photo"
23
+ )
24
+ if snapshot:
25
+ hs.markdown(f"Saved to `{snapshot}`")
26
+
27
+ hs.markdown("## Babylon.js Viewer v2")
28
+ pick = viewer3d.babylon(BOOMBOX, key="babylon", height="360px", show_pick=label)
29
+ if pick:
30
+ hs.markdown(
31
+ f"point `{pick.point}` · normal `{pick.normal}` · mesh `{pick.mesh}` · "
32
+ f"face `{pick.face}` · screen `{pick.screen}`"
33
+ )
@@ -0,0 +1,31 @@
1
+ [project]
2
+ name = "hstream-3d"
3
+ version = "0.1.0"
4
+ description = "3D model viewers for HStream apps (Google <model-viewer> and Babylon.js Viewer v2), with click picking and annotations."
5
+ readme = "README.md"
6
+ requires-python = ">=3.10"
7
+ dependencies = ["hstream>=0.9.0"]
8
+
9
+ [project.urls]
10
+ Repository = "https://github.com/conradbez/hstream"
11
+
12
+ # HStream imports this the first time a script uses viewer3d.
13
+ [project.entry-points."hstream.components"]
14
+ viewer3d = "hstream_3d"
15
+
16
+ [dependency-groups]
17
+ dev = ["pytest>=8"]
18
+
19
+ [tool.pytest.ini_options]
20
+ testpaths = ["tests"]
21
+ markers = ["e2e: real browser against the viewers' CDNs"]
22
+
23
+ [tool.hatch.build.targets.wheel]
24
+ packages = ["src/hstream_3d"]
25
+
26
+ [build-system]
27
+ requires = ["hatchling"]
28
+ build-backend = "hatchling.build"
29
+
30
+ [tool.uv.sources]
31
+ hstream = { path = "..", editable = true }