-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathusd-to-json.cc
231 lines (165 loc) · 4.41 KB
/
usd-to-json.cc
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// SPDX-License-Identifier: MIT
// Copyright 2022 - Present, Syoyo Fujita.
#include "usd-to-json.hh"
#include "tinyusdz.hh"
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
#endif
// nlohmann json
#include "external/jsonhpp/nlohmann/json.hpp"
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#include "common-macros.inc"
#include "pprinter.hh"
using namespace nlohmann;
namespace tinyusdz {
namespace {
json ToJSON(tinyusdz::Xform& xform) {
json j;
j["name"] = xform.name;
j["typeName"] = "Xform";
if (xform.xformOps.size()) {
json jxformOpOrder;
std::vector<std::string> ops;
for (const auto &xformOp : xform.xformOps) {
ops.push_back(xformOp.suffix);
}
j["xformOpOrder"] = ops;
}
return j;
}
json ToJSON(tinyusdz::GeomMesh& mesh) {
json j;
#if 0
if (mesh.points.size()) {
j["points"] = mesh.points;
}
if (mesh.faceVertexCounts.size()) {
j["faceVertexCounts"] = mesh.faceVertexCounts;
}
if (mesh.faceVertexIndices.size()) {
j["faceVertexIndices"] = mesh.faceVertexIndices;
}
{
auto normals = mesh.normals.buffer.GetAsVec3fArray();
if (normals.size()) {
j["normals"] = normals;
}
}
// TODO: Subdivision surface
j["doubleSided"] = mesh.doubleSided;
j["purpose"] = mesh.purpose;
{
std::string v = "inherited";
if (mesh.visibility == tinyusdz::VisibilityInvisible) {
v = "invisible";
}
j["visibility"] = v;
}
if (mesh.extent.Valid()) {
j["extent"] = mesh.extent.to_array();
}
// subd
{
std::string scheme = "none";
if (mesh.subdivisionScheme == tinyusdz::SubdivisionSchemeCatmullClark) {
scheme = "catmullClark";
} else if (mesh.subdivisionScheme == tinyusdz::SubdivisionSchemeLoop) {
scheme = "loop";
} else if (mesh.subdivisionScheme == tinyusdz::SubdivisionSchemeBilinear) {
scheme = "bilinear";
}
j["subdivisionScheme"] = scheme;
if (mesh.cornerIndices.size()) {
j["cornerIndices"] = mesh.cornerIndices;
}
if (mesh.cornerSharpnesses.size()) {
j["cornerSharpness"] = mesh.cornerSharpnesses;
}
if (mesh.creaseIndices.size()) {
j["creaseIndices"] = mesh.creaseIndices;
}
if (mesh.creaseLengths.size()) {
j["creaseLengths"] = mesh.creaseLengths;
}
if (mesh.creaseSharpnesses.size()) {
j["creaseSharpnesses"] = mesh.creaseSharpnesses;
}
if (mesh.interpolateBoundary.size()) {
j["interpolateBoundary"] = mesh.interpolateBoundary;
}
}
#endif
return j;
}
json ToJSON(tinyusdz::GeomBasisCurves& curves) {
json j;
return j;
}
json ToJSON(const tinyusdz::value::Value &v) {
if (auto pv = v.get_value<tinyusdz::Xform>()) {
return ToJSON(pv.value());
}
return json();
}
nonstd::expected<json, std::string> ToJSON(const tinyusdz::StageMetas& metas) {
json j;
if (metas.upAxis.authored()) {
j["upAxis"] = to_string(metas.upAxis.get_value());
}
if (metas.comment.value.size()) {
// TODO: escape and quote
j["comment"] = metas.comment.value;
}
return j;
}
bool PrimToJSONRec(json &root, const tinyusdz::Prim& prim, int depth) {
json j = ToJSON(prim.data());
json jchildren = json::object();
// TODO: Traverse Prim according to primChildren.
for (const auto &child : prim.children()) {
json cj;
if (!PrimToJSONRec(cj, child, depth+1)) {
return false;
}
std::string cname = child.element_name();
jchildren[cname] = cj;
}
if (jchildren.size()) {
j["primChildren"] = jchildren;
}
root[prim.element_name()] = j;
return true;
}
} // namespace
nonstd::expected<std::string, std::string> ToJSON(
const tinyusdz::Stage& stage) {
json j; // root
auto jstageMetas = ToJSON(stage.metas());
if (!jstageMetas) {
return nonstd::make_unexpected(jstageMetas.error());
}
// Stage metadatum is represented as properties.
if (!jstageMetas->is_null()) {
j["properties"] = *jstageMetas;
}
j["version"] = 1.0;
json cj;
for (const auto& item : stage.root_prims()) {
if (!PrimToJSONRec(cj, item, 0)) {
return nonstd::make_unexpected("Failed to convert Prim to JSON.");
}
}
j["primChildren"] = cj;
tinyusdz::GeomMesh mesh;
json jmesh = ToJSON(mesh);
(void)jmesh;
tinyusdz::GeomBasisCurves curves;
json jcurves = ToJSON(curves);
(void)jcurves;
std::string str = j.dump(/* indent*/ 2);
return str;
}
} // namespace tinyusdz