viser2blender 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.
- viser2blender-0.2.0/LICENSE +176 -0
- viser2blender-0.2.0/PKG-INFO +31 -0
- viser2blender-0.2.0/README.md +17 -0
- viser2blender-0.2.0/pyproject.toml +35 -0
- viser2blender-0.2.0/setup.cfg +4 -0
- viser2blender-0.2.0/src/viser2blender/__init__.py +4 -0
- viser2blender-0.2.0/src/viser2blender/__main__.py +5 -0
- viser2blender-0.2.0/src/viser2blender/_bpy_backend.py +347 -0
- viser2blender-0.2.0/src/viser2blender/_cli.py +58 -0
- viser2blender-0.2.0/src/viser2blender/_normalize.py +1030 -0
- viser2blender-0.2.0/src/viser2blender/_recording.py +49 -0
- viser2blender-0.2.0/src/viser2blender/_types.py +134 -0
- viser2blender-0.2.0/src/viser2blender.egg-info/PKG-INFO +31 -0
- viser2blender-0.2.0/src/viser2blender.egg-info/SOURCES.txt +17 -0
- viser2blender-0.2.0/src/viser2blender.egg-info/dependency_links.txt +1 -0
- viser2blender-0.2.0/src/viser2blender.egg-info/entry_points.txt +2 -0
- viser2blender-0.2.0/src/viser2blender.egg-info/requires.txt +4 -0
- viser2blender-0.2.0/src/viser2blender.egg-info/top_level.txt +1 -0
- viser2blender-0.2.0/tests/test_blender.py +213 -0
|
@@ -0,0 +1,176 @@
|
|
|
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
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: viser2blender
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Convert strict .viser recordings into .blend files.
|
|
5
|
+
License-Expression: Apache-2.0
|
|
6
|
+
Requires-Python: <3.14,>=3.13
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: bpy>=5.1.0
|
|
10
|
+
Requires-Dist: msgspec<1.0.0,>=0.18.6
|
|
11
|
+
Requires-Dist: numpy<2,>=1
|
|
12
|
+
Requires-Dist: zstandard<1.0.0,>=0.20.0
|
|
13
|
+
Dynamic: license-file
|
|
14
|
+
|
|
15
|
+
# viser2blender
|
|
16
|
+
|
|
17
|
+
`viser2blender` converts a strict supported subset of `.viser` recordings into
|
|
18
|
+
`.blend` files through `bpy`.
|
|
19
|
+
|
|
20
|
+
It is packaged separately from `viser4d` because it requires `numpy<2` and
|
|
21
|
+
`bpy>=5.1.0`.
|
|
22
|
+
|
|
23
|
+
Quick manual check:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
cd ../viser2blender
|
|
27
|
+
uv run --python 3.13 viser2blender \
|
|
28
|
+
tests/assets/blender_showcase.viser \
|
|
29
|
+
/tmp/blender-showcase.blend \
|
|
30
|
+
--overwrite
|
|
31
|
+
```
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# viser2blender
|
|
2
|
+
|
|
3
|
+
`viser2blender` converts a strict supported subset of `.viser` recordings into
|
|
4
|
+
`.blend` files through `bpy`.
|
|
5
|
+
|
|
6
|
+
It is packaged separately from `viser4d` because it requires `numpy<2` and
|
|
7
|
+
`bpy>=5.1.0`.
|
|
8
|
+
|
|
9
|
+
Quick manual check:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
cd ../viser2blender
|
|
13
|
+
uv run --python 3.13 viser2blender \
|
|
14
|
+
tests/assets/blender_showcase.viser \
|
|
15
|
+
/tmp/blender-showcase.blend \
|
|
16
|
+
--overwrite
|
|
17
|
+
```
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "viser2blender"
|
|
3
|
+
version = "0.2.0"
|
|
4
|
+
description = "Convert strict .viser recordings into .blend files."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "Apache-2.0"
|
|
7
|
+
requires-python = ">=3.13,<3.14"
|
|
8
|
+
dependencies = [
|
|
9
|
+
"bpy>=5.1.0",
|
|
10
|
+
"msgspec>=0.18.6,<1.0.0",
|
|
11
|
+
"numpy>=1,<2",
|
|
12
|
+
"zstandard>=0.20.0,<1.0.0",
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
[project.scripts]
|
|
16
|
+
viser2blender = "viser2blender._cli:main"
|
|
17
|
+
|
|
18
|
+
[dependency-groups]
|
|
19
|
+
dev = [
|
|
20
|
+
"pytest>=8.3.4",
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
[build-system]
|
|
24
|
+
requires = ["setuptools>=68", "wheel"]
|
|
25
|
+
build-backend = "setuptools.build_meta"
|
|
26
|
+
|
|
27
|
+
[tool.setuptools]
|
|
28
|
+
package-dir = {"" = "src"}
|
|
29
|
+
include-package-data = false
|
|
30
|
+
|
|
31
|
+
[tool.setuptools.packages.find]
|
|
32
|
+
where = ["src"]
|
|
33
|
+
|
|
34
|
+
[tool.pytest.ini_options]
|
|
35
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import importlib
|
|
4
|
+
import importlib.util
|
|
5
|
+
from fractions import Fraction
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Any, Callable, cast
|
|
8
|
+
|
|
9
|
+
from ._types import (
|
|
10
|
+
ColorValue,
|
|
11
|
+
CylinderGeometry,
|
|
12
|
+
FrameGeometry,
|
|
13
|
+
GeometryData,
|
|
14
|
+
IcosphereGeometry,
|
|
15
|
+
LineSegmentsGeometry,
|
|
16
|
+
MaterialStyle,
|
|
17
|
+
MeshGeometry,
|
|
18
|
+
NodeManifest,
|
|
19
|
+
PointCloudGeometry,
|
|
20
|
+
RecordingManifest,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
MIN_BPY_VERSION = (5, 1, 0)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def convert_manifest(scene_manifest: RecordingManifest, output_path: Path) -> None:
|
|
28
|
+
bpy = _import_bpy()
|
|
29
|
+
|
|
30
|
+
bpy.ops.wm.read_factory_settings(use_empty=True)
|
|
31
|
+
scene = bpy.context.scene
|
|
32
|
+
fps_numerator, fps_denominator = _fps_fraction(scene_manifest.fps)
|
|
33
|
+
scene.render.fps = fps_numerator
|
|
34
|
+
scene.render.fps_base = float(fps_denominator)
|
|
35
|
+
scene.frame_start = 1
|
|
36
|
+
scene.frame_end = int(scene_manifest.frame_count)
|
|
37
|
+
|
|
38
|
+
objects = {
|
|
39
|
+
node.node_id: _create_node_object(bpy, node) for node in scene_manifest.nodes
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
for node in scene_manifest.nodes:
|
|
43
|
+
if node.parent_id is not None:
|
|
44
|
+
objects[node.node_id].parent = objects[node.parent_id]
|
|
45
|
+
|
|
46
|
+
for node in scene_manifest.nodes:
|
|
47
|
+
obj = objects[node.node_id]
|
|
48
|
+
_apply_transform_keyframes(obj, node)
|
|
49
|
+
_apply_visibility_keyframes(obj, node)
|
|
50
|
+
_apply_geometry_keyframes(obj, node)
|
|
51
|
+
|
|
52
|
+
_set_constant_interpolation(bpy)
|
|
53
|
+
bpy.ops.wm.save_as_mainfile(filepath=str(output_path), compress=False)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _import_bpy() -> Any:
|
|
57
|
+
if importlib.util.find_spec("bpy") is None:
|
|
58
|
+
raise RuntimeError("viser2blender requires Blender bpy>=5.1.0.")
|
|
59
|
+
bpy = importlib.import_module("bpy")
|
|
60
|
+
version = tuple(cast(tuple[int, int, int], bpy.app.version))
|
|
61
|
+
if version < MIN_BPY_VERSION:
|
|
62
|
+
found = ".".join(str(part) for part in version)
|
|
63
|
+
raise RuntimeError(
|
|
64
|
+
f"viser2blender requires Blender bpy>=5.1.0. Found bpy {found}."
|
|
65
|
+
)
|
|
66
|
+
return bpy
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _fps_fraction(fps: float) -> tuple[int, int]:
|
|
70
|
+
ratio = Fraction(fps).limit_denominator(1001)
|
|
71
|
+
return max(ratio.numerator, 1), max(ratio.denominator, 1)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _safe_name(name: str) -> str:
|
|
75
|
+
stripped = name.strip("/")
|
|
76
|
+
return stripped.replace("/", "__") or "root"
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _blender_name(node: NodeManifest) -> str:
|
|
80
|
+
base = _safe_name(node.name)
|
|
81
|
+
epoch = node.node_id.rsplit("#", 1)[1]
|
|
82
|
+
return base if epoch == "0" else f"{base}__epoch{epoch}"
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _first_color_leaf(color: ColorValue) -> list[int]:
|
|
86
|
+
sample: Any = color
|
|
87
|
+
while sample and isinstance(sample[0], list):
|
|
88
|
+
sample = sample[0]
|
|
89
|
+
return cast(list[int], sample)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _ensure_material(bpy: Any, name: str, style: MaterialStyle) -> Any | None:
|
|
93
|
+
if style.color is None:
|
|
94
|
+
return None
|
|
95
|
+
rgba = _normalized_rgba(_first_color_leaf(style.color), style.opacity)
|
|
96
|
+
material = bpy.data.materials.new(name=name)
|
|
97
|
+
material.use_nodes = True
|
|
98
|
+
|
|
99
|
+
principled = material.node_tree.nodes["Principled BSDF"]
|
|
100
|
+
principled.inputs["Base Color"].default_value = rgba
|
|
101
|
+
principled.inputs["Alpha"].default_value = rgba[3]
|
|
102
|
+
if rgba[3] < 1.0:
|
|
103
|
+
material.blend_method = "HASHED"
|
|
104
|
+
material.use_backface_culling = style.side == "front"
|
|
105
|
+
return material
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _create_group_object(bpy: Any, _node: NodeManifest) -> Any:
|
|
109
|
+
return _add_empty_object(bpy, "PLAIN_AXES")
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def _create_frame_object(bpy: Any, node: NodeManifest) -> Any:
|
|
113
|
+
geometry = cast(FrameGeometry, node.geometry)
|
|
114
|
+
obj = _add_empty_object(bpy, "ARROWS" if geometry.show_axes else "PLAIN_AXES")
|
|
115
|
+
obj.empty_display_size = float(geometry.axes_length)
|
|
116
|
+
obj["viser_axes_radius"] = float(geometry.axes_radius)
|
|
117
|
+
obj["viser_origin_radius"] = float(geometry.origin_radius)
|
|
118
|
+
obj["viser_show_axes"] = bool(geometry.show_axes)
|
|
119
|
+
return obj
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _create_mesh_object(
|
|
123
|
+
bpy: Any,
|
|
124
|
+
name: str,
|
|
125
|
+
vertices: list[list[float]],
|
|
126
|
+
faces: list[list[int]],
|
|
127
|
+
edges: list[tuple[int, int]] | None = None,
|
|
128
|
+
) -> Any:
|
|
129
|
+
mesh = bpy.data.meshes.new(name)
|
|
130
|
+
mesh.from_pydata(vertices, edges or [], faces)
|
|
131
|
+
mesh.update()
|
|
132
|
+
obj = bpy.data.objects.new(name, mesh)
|
|
133
|
+
bpy.context.scene.collection.objects.link(obj)
|
|
134
|
+
return obj
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _create_mesh_node(bpy: Any, node: NodeManifest) -> Any:
|
|
138
|
+
geometry = cast(MeshGeometry, node.geometry)
|
|
139
|
+
return _create_mesh_object(
|
|
140
|
+
bpy,
|
|
141
|
+
_blender_name(node),
|
|
142
|
+
geometry.vertices,
|
|
143
|
+
geometry.faces,
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def _create_point_cloud_node(bpy: Any, node: NodeManifest) -> Any:
|
|
148
|
+
geometry = cast(PointCloudGeometry, node.geometry)
|
|
149
|
+
obj = _create_mesh_object(bpy, _blender_name(node), geometry.points, [])
|
|
150
|
+
obj["viser_point_precision"] = geometry.precision
|
|
151
|
+
return obj
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _create_line_segments_node(bpy: Any, node: NodeManifest) -> Any:
|
|
155
|
+
geometry = cast(LineSegmentsGeometry, node.geometry)
|
|
156
|
+
vertices = [endpoint for segment in geometry.points for endpoint in segment]
|
|
157
|
+
edges = [(index, index + 1) for index in range(0, len(vertices), 2)]
|
|
158
|
+
return _create_mesh_object(bpy, _blender_name(node), vertices, [], edges)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def _create_icosphere_node(bpy: Any, node: NodeManifest) -> Any:
|
|
162
|
+
geometry = cast(IcosphereGeometry, node.geometry)
|
|
163
|
+
bpy.ops.mesh.primitive_ico_sphere_add(
|
|
164
|
+
subdivisions=int(geometry.subdivisions),
|
|
165
|
+
radius=float(geometry.radius),
|
|
166
|
+
)
|
|
167
|
+
return _active_object(bpy)
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def _create_cylinder_node(bpy: Any, node: NodeManifest) -> Any:
|
|
171
|
+
geometry = cast(CylinderGeometry, node.geometry)
|
|
172
|
+
bpy.ops.mesh.primitive_cylinder_add(
|
|
173
|
+
vertices=int(geometry.radial_segments),
|
|
174
|
+
radius=float(geometry.radius),
|
|
175
|
+
depth=float(geometry.height),
|
|
176
|
+
)
|
|
177
|
+
return _active_object(bpy)
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def _create_node_object(bpy: Any, node: NodeManifest) -> Any:
|
|
181
|
+
obj = OBJECT_BUILDERS[node.kind](bpy, node)
|
|
182
|
+
obj.name = _blender_name(node)
|
|
183
|
+
obj["viser_node_id"] = node.node_id
|
|
184
|
+
obj["viser_name"] = node.name
|
|
185
|
+
obj["viser_kind"] = node.kind
|
|
186
|
+
obj["viser_implicit"] = node.implicit
|
|
187
|
+
obj.rotation_mode = "QUATERNION"
|
|
188
|
+
|
|
189
|
+
_apply_style(bpy, obj, node)
|
|
190
|
+
return obj
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def _apply_style(bpy: Any, obj: Any, node: NodeManifest) -> None:
|
|
194
|
+
style = node.style
|
|
195
|
+
if style.scale is not None:
|
|
196
|
+
obj.scale = _scale_vector(style.scale)
|
|
197
|
+
if style.wireframe:
|
|
198
|
+
obj.display_type = "WIRE"
|
|
199
|
+
if obj.type == "MESH":
|
|
200
|
+
obj.show_wire = True
|
|
201
|
+
obj.show_all_edges = True
|
|
202
|
+
if style.cast_shadow is not None and hasattr(obj, "visible_shadow"):
|
|
203
|
+
obj.visible_shadow = bool(style.cast_shadow)
|
|
204
|
+
if style.receive_shadow is not None:
|
|
205
|
+
obj["viser_receive_shadow"] = bool(style.receive_shadow)
|
|
206
|
+
if style.point_size is not None:
|
|
207
|
+
obj["viser_point_size"] = float(style.point_size)
|
|
208
|
+
if style.point_shape is not None:
|
|
209
|
+
obj["viser_point_shape"] = str(style.point_shape)
|
|
210
|
+
if style.line_width is not None:
|
|
211
|
+
obj["viser_line_width"] = float(style.line_width)
|
|
212
|
+
if style.side is not None:
|
|
213
|
+
obj["viser_side"] = style.side
|
|
214
|
+
if style.material is not None:
|
|
215
|
+
obj["viser_material"] = style.material
|
|
216
|
+
|
|
217
|
+
material = _ensure_material(bpy, f"{_blender_name(node)}_material", style)
|
|
218
|
+
if material is not None and obj.type == "MESH":
|
|
219
|
+
obj.data.materials.append(material)
|
|
220
|
+
elif style.color is not None and hasattr(obj, "color"):
|
|
221
|
+
rgba = _normalized_rgba(_first_color_leaf(style.color), style.opacity)
|
|
222
|
+
obj.color = rgba
|
|
223
|
+
|
|
224
|
+
if obj.type == "MESH" and style.flat_shading is not None:
|
|
225
|
+
for polygon in obj.data.polygons:
|
|
226
|
+
polygon.use_smooth = not style.flat_shading
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def _normalized_rgba(color: list[int], opacity: float | None) -> list[float]:
|
|
230
|
+
rgba = [float(channel) / 255.0 for channel in color]
|
|
231
|
+
if len(rgba) == 3:
|
|
232
|
+
rgba.append(1.0 if opacity is None else float(opacity))
|
|
233
|
+
elif opacity is not None:
|
|
234
|
+
rgba[3] = float(opacity)
|
|
235
|
+
return rgba
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def _scale_vector(scale: float | list[float]) -> tuple[float, float, float]:
|
|
239
|
+
if isinstance(scale, float):
|
|
240
|
+
return (scale, scale, scale)
|
|
241
|
+
return (float(scale[0]), float(scale[1]), float(scale[2]))
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def _add_empty_object(bpy: Any, empty_type: str) -> Any:
|
|
245
|
+
bpy.ops.object.empty_add(type=empty_type)
|
|
246
|
+
return _active_object(bpy)
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
def _active_object(bpy: Any) -> Any:
|
|
250
|
+
obj = bpy.context.active_object
|
|
251
|
+
assert obj is not None
|
|
252
|
+
return obj
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def _apply_transform_keyframes(obj: Any, node: NodeManifest) -> None:
|
|
256
|
+
for keyframe in node.transform_keyframes:
|
|
257
|
+
if keyframe.position is not None:
|
|
258
|
+
obj.location = keyframe.position
|
|
259
|
+
obj.keyframe_insert(data_path="location", frame=keyframe.frame)
|
|
260
|
+
if keyframe.rotation_wxyz is not None:
|
|
261
|
+
obj.rotation_quaternion = keyframe.rotation_wxyz
|
|
262
|
+
obj.keyframe_insert(data_path="rotation_quaternion", frame=keyframe.frame)
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def _apply_visibility_keyframes(obj: Any, node: NodeManifest) -> None:
|
|
266
|
+
for keyframe in node.visibility_keyframes:
|
|
267
|
+
obj.hide_viewport = not keyframe.visible
|
|
268
|
+
obj.hide_render = not keyframe.visible
|
|
269
|
+
obj.keyframe_insert(data_path="hide_viewport", frame=keyframe.frame)
|
|
270
|
+
obj.keyframe_insert(data_path="hide_render", frame=keyframe.frame)
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
def _apply_geometry_keyframes(obj: Any, node: NodeManifest) -> None:
|
|
274
|
+
if not node.geometry_keyframes:
|
|
275
|
+
return
|
|
276
|
+
|
|
277
|
+
basis = obj.shape_key_add(name="Basis", from_mix=False)
|
|
278
|
+
basis.interpolation = "KEY_LINEAR"
|
|
279
|
+
previous_shape_name: str | None = None
|
|
280
|
+
|
|
281
|
+
for keyframe in node.geometry_keyframes:
|
|
282
|
+
shape_key = obj.shape_key_add(
|
|
283
|
+
name=f"frame_{keyframe.frame:04d}", from_mix=False
|
|
284
|
+
)
|
|
285
|
+
shape_key.interpolation = "KEY_LINEAR"
|
|
286
|
+
vertices = _geometry_vertices(keyframe.geometry)
|
|
287
|
+
for vertex, coord in zip(shape_key.data, vertices, strict=True):
|
|
288
|
+
vertex.co = coord
|
|
289
|
+
|
|
290
|
+
start_frame = max(node.create_frame, keyframe.frame - 1)
|
|
291
|
+
shape_key.value = 0.0
|
|
292
|
+
shape_key.keyframe_insert(data_path="value", frame=start_frame)
|
|
293
|
+
shape_key.value = 1.0
|
|
294
|
+
shape_key.keyframe_insert(data_path="value", frame=keyframe.frame)
|
|
295
|
+
|
|
296
|
+
if previous_shape_name is not None:
|
|
297
|
+
previous_shape = obj.data.shape_keys.key_blocks[previous_shape_name]
|
|
298
|
+
previous_shape.value = 0.0
|
|
299
|
+
previous_shape.keyframe_insert(data_path="value", frame=keyframe.frame)
|
|
300
|
+
|
|
301
|
+
previous_shape_name = shape_key.name
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def _geometry_vertices(geometry: GeometryData) -> list[list[float]]:
|
|
305
|
+
if isinstance(geometry, MeshGeometry):
|
|
306
|
+
return geometry.vertices
|
|
307
|
+
if isinstance(geometry, PointCloudGeometry):
|
|
308
|
+
return geometry.points
|
|
309
|
+
if isinstance(geometry, LineSegmentsGeometry):
|
|
310
|
+
return [endpoint for segment in geometry.points for endpoint in segment]
|
|
311
|
+
raise RuntimeError(
|
|
312
|
+
f"Geometry updates are not supported for {type(geometry).__name__}."
|
|
313
|
+
)
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def _set_constant_interpolation(bpy: Any) -> None:
|
|
317
|
+
for action in bpy.data.actions:
|
|
318
|
+
for curve in _action_fcurves(action):
|
|
319
|
+
for keyframe in curve.keyframe_points:
|
|
320
|
+
keyframe.interpolation = "CONSTANT"
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
def _action_fcurves(action: Any) -> Any:
|
|
324
|
+
fcurves = getattr(action, "fcurves", None)
|
|
325
|
+
if fcurves is not None:
|
|
326
|
+
return fcurves
|
|
327
|
+
|
|
328
|
+
layered_fcurves: list[Any] = []
|
|
329
|
+
for layer in action.layers:
|
|
330
|
+
for strip in layer.strips:
|
|
331
|
+
for channelbag in strip.channelbags:
|
|
332
|
+
layered_fcurves.extend(channelbag.fcurves)
|
|
333
|
+
return layered_fcurves
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
ObjectBuilder = Callable[[Any, NodeManifest], Any]
|
|
337
|
+
|
|
338
|
+
OBJECT_BUILDERS: dict[str, ObjectBuilder] = {
|
|
339
|
+
"root": _create_group_object,
|
|
340
|
+
"group": _create_group_object,
|
|
341
|
+
"frame": _create_frame_object,
|
|
342
|
+
"mesh": _create_mesh_node,
|
|
343
|
+
"point_cloud": _create_point_cloud_node,
|
|
344
|
+
"line_segments": _create_line_segments_node,
|
|
345
|
+
"icosphere": _create_icosphere_node,
|
|
346
|
+
"cylinder": _create_cylinder_node,
|
|
347
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import json
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from ._bpy_backend import convert_manifest
|
|
8
|
+
from ._normalize import normalize_recording
|
|
9
|
+
from ._recording import load_viser_recording
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def main() -> None:
|
|
13
|
+
args = _parse_args()
|
|
14
|
+
manifest = normalize_recording(load_viser_recording(args.input_path))
|
|
15
|
+
|
|
16
|
+
if args.emit_manifest is not None:
|
|
17
|
+
_check_writable(args.emit_manifest, overwrite=args.overwrite)
|
|
18
|
+
args.emit_manifest.write_text(json.dumps(manifest.to_jsonable(), indent=2))
|
|
19
|
+
|
|
20
|
+
if args.validate_only:
|
|
21
|
+
return
|
|
22
|
+
|
|
23
|
+
_check_writable(args.output_path, overwrite=args.overwrite)
|
|
24
|
+
convert_manifest(manifest, args.output_path)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _parse_args() -> argparse.Namespace:
|
|
28
|
+
parser = argparse.ArgumentParser(
|
|
29
|
+
prog="viser2blender",
|
|
30
|
+
description=(
|
|
31
|
+
"Convert a strict supported subset of .viser recordings into .blend "
|
|
32
|
+
"files. Requires bpy>=5.1.0."
|
|
33
|
+
),
|
|
34
|
+
)
|
|
35
|
+
parser.add_argument("input_path", type=Path, help="Input .viser recording.")
|
|
36
|
+
parser.add_argument("output_path", type=Path, help="Output .blend path.")
|
|
37
|
+
parser.add_argument(
|
|
38
|
+
"--emit-manifest",
|
|
39
|
+
type=Path,
|
|
40
|
+
default=None,
|
|
41
|
+
help="Write the normalized manifest JSON for inspection.",
|
|
42
|
+
)
|
|
43
|
+
parser.add_argument(
|
|
44
|
+
"--validate-only",
|
|
45
|
+
action="store_true",
|
|
46
|
+
help="Validate and normalize the .viser file without invoking Blender.",
|
|
47
|
+
)
|
|
48
|
+
parser.add_argument(
|
|
49
|
+
"--overwrite",
|
|
50
|
+
action="store_true",
|
|
51
|
+
help="Allow overwriting output files.",
|
|
52
|
+
)
|
|
53
|
+
return parser.parse_args()
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _check_writable(path: Path, *, overwrite: bool) -> None:
|
|
57
|
+
if path.exists() and not overwrite:
|
|
58
|
+
raise FileExistsError(f"{path} already exists. Use --overwrite to replace it.")
|