aboutsummaryrefslogtreecommitdiffstats
path: root/generat3d/dbz.py
blob: e5b1aea805762fd416da4bf13db561f9eeb541a7 (plain)
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
# 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 json
import math
import os
import tempfile
from pathlib import Path

from . import Error, __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({
        "name": fig.name,
        "label": fig.label,
        "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)
    tmp = None
    try:
        fd, tmp = tempfile.mkstemp(dir=out.parent, prefix=out.name, suffix=".tmp")
        with os.fdopen(fd, "wb") as f:
            f.write(gzip.compress(json.dumps(doc).encode()))
        os.replace(tmp, out)
    except BaseException as e:
        if tmp is not None:
            with contextlib.suppress(OSError):
                os.unlink(tmp)
        if isinstance(e, OSError):
            raise Error(f"cannot write {out}: {e.strerror or e}") from None
        raise