aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--generat3d/dbz.py80
-rw-r--r--tests/test_dbz.py79
2 files changed, 159 insertions, 0 deletions
diff --git a/generat3d/dbz.py b/generat3d/dbz.py
new file mode 100644
index 0000000..a71fbd5
--- /dev/null
+++ b/generat3d/dbz.py
@@ -0,0 +1,80 @@
+# 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 gzip
+import json
+import math
+import os
+import tempfile
+from pathlib import Path
+
+from . import __version__
+from .evaluate import evaluate
+
+
+def orientation_quat(deg):
+ """DSON orientation (Euler degrees, XYZ) to a quaternion [x, y, z, w]."""
+ hx, hy, hz = (math.radians(d) / 2 for d in deg)
+ cx, sx = math.cos(hx), math.sin(hx)
+ cy, sy = math.cos(hy), math.sin(hy)
+ cz, sz = math.cos(hz), math.sin(hz)
+ # q = qz * qy * qx (X applied first)
+ return [sx * cy * cz - cx * sy * sz,
+ cx * sy * cz + sx * cy * sz,
+ cx * cy * sz - sx * sy * cz,
+ cx * cy * cz + sx * sy * sz]
+
+
+def _node(n, r):
+ return {"name": n.name, "id": n.id, "label": n.label,
+ "center_point": r["center_point"], "end_point": r["end_point"],
+ "orientation": orientation_quat(r["orientation"]),
+ "rotation_order": n.rotation_order}
+
+
+def build(scene, roots):
+ """The .dbz document for a loaded scene."""
+ verts, rest = evaluate(scene)
+ fig = scene.figure
+ root, bones = fig.nodes[0], fig.nodes[1:]
+ entry = _node(root, rest[root.id])
+ entry.update({
+ "class": "DzFigure",
+ "material groups": fig.material_groups,
+ "num verts": len(verts),
+ "vertices": verts.tolist(),
+ "bones": [_node(b, rest[b.id]) | {"origin": rest[b.id]["center_point"]}
+ for b in bones],
+ })
+ return {"application": "export_to_blender",
+ "version": f"generat3d {__version__}",
+ "filepath": str(scene.path),
+ "rootpaths": {"content": [str(r) for r in roots]},
+ "figures": [entry, {"name": "dummy", "num verts": 0}]}
+
+
+def write(doc, out):
+ """Write doc as gzipped JSON. A temp file plus rename: no partial file on failure."""
+ out = Path(out)
+ fd, tmp = tempfile.mkstemp(dir=out.parent, prefix=out.name, suffix=".tmp")
+ try:
+ with os.fdopen(fd, "wb") as f:
+ f.write(gzip.compress(json.dumps(doc).encode()))
+ os.replace(tmp, out)
+ except BaseException:
+ os.unlink(tmp)
+ raise
diff --git a/tests/test_dbz.py b/tests/test_dbz.py
new file mode 100644
index 0000000..7bcf69a
--- /dev/null
+++ b/tests/test_dbz.py
@@ -0,0 +1,79 @@
+# 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 gzip
+import io
+import json
+import math
+import tempfile
+import unittest
+from pathlib import Path
+
+from generat3d.dbz import build, orientation_quat, write
+from generat3d.library import Library
+from generat3d.scene import load
+
+DATA = Path(__file__).parent / "data"
+
+
+class DbzTest(unittest.TestCase):
+ def setUp(self):
+ with contextlib.redirect_stderr(io.StringIO()):
+ self.doc = build(load(DATA / "scene.duf", Library([DATA / "lib"])), [DATA / "lib"])
+
+ def test_header(self):
+ self.assertEqual(self.doc["application"], "export_to_blender")
+ self.assertEqual(self.doc["figures"][-1], {"name": "dummy", "num verts": 0})
+
+ def test_figure_entry(self):
+ fig = self.doc["figures"][0]
+ self.assertEqual((fig["name"], fig["id"], fig["label"], fig["class"]),
+ ("Fig", "Fig", "Test Figure", "DzFigure"))
+ self.assertEqual(fig["num verts"], 3)
+ self.assertEqual(fig["vertices"][1], [1.5, 2.0, 0.0])
+ self.assertEqual(fig["material groups"], ["Skin"])
+ self.assertEqual(fig["orientation"], [0.0, 0.0, 0.0, 1.0])
+ self.assertNotIn("ws_transform", fig)
+
+ def test_bones(self):
+ hip = self.doc["figures"][0]["bones"][0]
+ self.assertEqual(hip["name"], "hip")
+ self.assertEqual(hip["center_point"], [0, 7, 0])
+ self.assertEqual(hip["origin"], [0, 7, 0])
+ self.assertEqual(hip["rotation_order"], "YZX")
+
+ def test_orientation_quat(self):
+ x, y, z, w = orientation_quat([90, 0, 0])
+ self.assertAlmostEqual(x, math.sqrt(0.5))
+ self.assertAlmostEqual(w, math.sqrt(0.5))
+ self.assertAlmostEqual(y, 0)
+ self.assertAlmostEqual(z, 0)
+
+ def test_write_is_gzip_json_and_atomic(self):
+ with tempfile.TemporaryDirectory() as d:
+ out = Path(d) / "s.dbz"
+ write(self.doc, out)
+ self.assertEqual(json.loads(gzip.decompress(out.read_bytes())), self.doc)
+ self.assertEqual([p.name for p in Path(d).iterdir()], ["s.dbz"])
+
+ def test_failed_write_leaves_nothing(self):
+ with tempfile.TemporaryDirectory() as d:
+ out = Path(d) / "s.dbz"
+ with self.assertRaises(TypeError):
+ write({"bad": object()}, out)
+ self.assertEqual(list(Path(d).iterdir()), [])