1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# generat3D - evaluate Daz DSON scenes and write .dbz files
# Copyright (C) 2026 Danilo M. <danix@danix.xyz>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import contextlib
import io
import json
import tempfile
import unittest
from pathlib import Path
from generat3d import Error
from generat3d.library import Library
from generat3d.scene import load
DATA = Path(__file__).parent / "data"
def load_quiet(duf, extra_roots=()):
err = io.StringIO()
with contextlib.redirect_stderr(err):
scene = load(duf, Library([DATA / "lib", *extra_roots]))
return scene, err.getvalue()
class SceneTest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.dir = Path(self.tmp.name)
def tearDown(self):
self.tmp.cleanup()
def duf(self, scene):
p = self.dir / "s.duf"
p.write_text(json.dumps({"scene": scene}))
return p
def base_nodes(self):
return [{"id": "Fig", "url": "/data/Test/Fig/Fig.dsf#Fig", "name": "Fig"}]
def test_figure_and_nodes(self):
scene, _ = load_quiet(DATA / "scene.duf")
fig = scene.figure
self.assertEqual((fig.id, fig.name, fig.label), ("Fig", "Fig", "Test Figure"))
self.assertEqual([n.id for n in fig.nodes], ["Fig", "hip"])
self.assertEqual(fig.nodes[1].rotation_order, "YZX")
self.assertEqual(fig.material_groups, ["Skin"])
self.assertEqual(fig.verts.tolist(), [[0, 0, 0], [1, 0, 0], [0, 1, 0]])
def test_morphs_auto_loaded_and_broken_file_warns(self):
scene, err = load_quiet(DATA / "scene.duf")
self.assertEqual(sorted(scene.figure.morphs), ["Fig:Tall?value", "Fig:Wide?value"])
self.assertIn("Broken.dsf", err)
def test_dial_from_animations(self):
scene, _ = load_quiet(DATA / "scene.duf")
self.assertEqual(scene.graph.props["Fig:Tall?value"].value, 1)
def test_dial_from_modifiers(self):
scene, _ = load_quiet(self.duf({"nodes": self.base_nodes(), "modifiers": [
{"id": "Tall", "url": "/data/Test/Fig/Morphs/Test/Tall.dsf#Tall",
"parent": "#Fig", "channel": {"id": "value", "current_value": 0.25}}]}))
self.assertEqual(scene.graph.props["Fig:Tall?value"].value, 0.25)
def test_bone_channels_are_props(self):
scene, _ = load_quiet(DATA / "scene.duf")
self.assertEqual(scene.graph.props["hip:hip?center_point/y"].value, 5)
self.assertEqual(scene.graph.props["Fig:Fig?end_point/y"].value, 10)
def test_formulas_rewritten_to_keys(self):
scene, _ = load_quiet(DATA / "scene.duf")
ops = [f[1] for f in scene.graph.formulas["Fig:Wide?value"]]
self.assertEqual(ops[0][0], {"op": "push", "key": "Fig:Tall?value"})
self.assertEqual(ops[1][0], {"op": "push", "val": 0.0})
def test_missing_dialed_morphs_listed_and_fatal(self):
mods = [{"id": m, "url": f"/data/Test/Fig/Morphs/Test/{m}.dsf#{m}", "parent": "#Fig",
"channel": {"id": "value", "current_value": 1}} for m in ("Gone1", "Gone2")]
with self.assertRaisesRegex(Error, "(?s)Gone1.dsf.*Gone2.dsf"):
load_quiet(self.duf({"nodes": self.base_nodes(), "modifiers": mods}))
def test_missing_undialed_morph_is_fine(self):
mods = [{"id": "Gone", "url": "/data/Test/Fig/Morphs/Test/Gone.dsf#Gone",
"parent": "#Fig", "channel": {"id": "value", "current_value": 0}}]
load_quiet(self.duf({"nodes": self.base_nodes(), "modifiers": mods}))
def test_missing_figure_is_fatal(self):
with self.assertRaisesRegex(Error, "Nope.dsf"):
load_quiet(self.duf({"nodes": [{"id": "X", "url": "/data/Nope.dsf#X"}]}))
def test_no_figure_is_fatal(self):
with self.assertRaisesRegex(Error, "no figure"):
load_quiet(self.duf({"nodes": []}))
def test_two_figures_is_fatal(self):
nodes = self.base_nodes() + [{"id": "Fig2", "url": "/data/Test/Fig/Fig.dsf#Fig"}]
with self.assertRaisesRegex(Error, "one figure"):
load_quiet(self.duf({"nodes": nodes}))
def test_follower_skipped_with_warning(self):
nodes = self.base_nodes() + [{"id": "Shirt", "url": "/data/Test/Fig/Fig.dsf#Fig",
"name": "Shirt", "conform_target": "#Fig"}]
scene, err = load_quiet(self.duf({"nodes": nodes}))
self.assertEqual(scene.figure.id, "Fig")
self.assertIn("Shirt", err)
def test_pose_ignored_with_warning(self):
nodes = self.base_nodes() + [{"id": "hip", "url": "/data/Test/Fig/Fig.dsf#hip",
"rotation": [{"id": "x", "current_value": 30}]}]
_, err = load_quiet(self.duf({"nodes": nodes}))
self.assertIn("pose", err)
|