-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathtydra-api-main.cc
405 lines (332 loc) · 13.4 KB
/
tydra-api-main.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include <algorithm>
#include <iostream>
#include <sstream>
#include "tinyusdz.hh"
#include "tydra/attribute-eval.hh"
#include "tydra/render-data.hh"
#include "tydra/scene-access.hh"
#include "tydra/shader-network.hh"
#include "usdShade.hh"
#include "pprinter.hh"
#include "prim-pprint.hh"
#include "value-pprint.hh"
#include "value-types.hh"
static std::string GetFileExtension(const std::string &filename) {
if (filename.find_last_of('.') != std::string::npos)
return filename.substr(filename.find_last_of('.') + 1);
return "";
}
static std::string str_tolower(std::string s) {
std::transform(s.begin(), s.end(), s.begin(),
// static_cast<int(*)(int)>(std::tolower) // wrong
// [](int c){ return std::tolower(c); } // wrong
// [](char c){ return std::tolower(c); } // wrong
[](unsigned char c) { return std::tolower(c); } // correct
);
return s;
}
// key = Full absolute prim path(e.g. `/bora/dora`)
using XformMap = std::map<std::string, const tinyusdz::Xform *>;
using MeshMap = std::map<std::string, const tinyusdz::GeomMesh *>;
using MaterialMap = std::map<std::string, const tinyusdz::Material *>;
using PreviewSurfaceMap =
std::map<std::string, std::pair<const tinyusdz::Shader *, const tinyusdz::UsdPreviewSurface *>>;
using UVTextureMap = std::map<std::string, std::pair<const tinyusdz::Shader *, const tinyusdz::UsdUVTexture *>>;
using PrimvarReader_float2Map =
std::map<std::string, std::pair<const tinyusdz::Shader *, const tinyusdz::UsdPrimvarReader_float2 *>>;
#if 0
template <typename T>
static bool TraverseRec(const std::string &path_prefix,
const tinyusdz::Prim &prim, uint32_t depth,
std::map<std::string, const T *> &itemmap) {
if (depth > 1024 * 128) {
// Too deep
return false;
}
std::string prim_abs_path = path_prefix + "/" + prim.local_path().full_path_name();
if (prim.is<tinyusdz::Material>()) {
if (const T *pv = prim.as<T>()) {
std::cout << "Path : <" << prim_abs_path << "> is "
<< tinyusdz::value::TypeTraits<T>::type_name() << ".\n";
itemmap[prim_abs_path] = pv;
}
}
for (const auto &child : prim.children()) {
if (!TraverseRec(prim_abs_path, child, depth + 1, itemmap)) {
return false;
}
}
return true;
}
template <typename T>
static bool TraverseShaderRec(const std::string &path_prefix,
const tinyusdz::Prim &prim, uint32_t depth,
std::map<std::string, const T *> &itemmap) {
if (depth > 1024 * 128) {
// Too deep
return false;
}
std::string prim_abs_path = path_prefix + "/" + prim.local_path().full_path_name();
// First test if Shader prim.
if (const tinyusdz::Shader *ps = prim.as<tinyusdz::Shader>()) {
// Concrete Shader object(e.g. UsdUVTexture) is stored in .data.
if (const T *s = ps->value.as<T>()) {
std::cout << "Path : <" << prim_abs_path << "> is "
<< tinyusdz::value::TypeTraits<T>::type_name() << ".\n";
itemmap[prim_abs_path] = s;
}
}
for (const auto &child : prim.children()) {
if (!TraverseShaderRec(prim_abs_path, child, depth + 1, itemmap)) {
return false;
}
}
return true;
}
static void TraverseMaterial(const tinyusdz::Stage &stage, MaterialMap &m) {
for (const auto &prim : stage.GetRootPrims()) {
TraverseRec(/* root */ "", prim, 0, m);
}
}
static void TraversePreviewSurface(const tinyusdz::Stage &stage,
PreviewSurfaceMap &m) {
for (const auto &prim : stage.GetRootPrims()) {
TraverseShaderRec(/* root */ "", prim, 0, m);
}
}
static void TraverseUVTexture(const tinyusdz::Stage &stage, UVTextureMap &m) {
for (const auto &prim : stage.GetRootPrims()) {
TraverseShaderRec(/* root */ "", prim, 0, m);
}
}
static void TraversePrimvarReader_float2(const tinyusdz::Stage &stage,
PrimvarReader_float2Map &m) {
for (const auto &prim : stage.GetRootPrims()) {
TraverseShaderRec(/* root */ "", prim, 0, m);
}
}
#endif
int main(int argc, char **argv) {
if (argc < 2) {
std::cout << "Need USD file with Material/Shader(e.g. `<tinyusdz>/models/texturescube.usda`)\n" << std::endl;
return EXIT_FAILURE;
}
std::string filepath = argv[1];
std::string warn;
std::string err;
std::string ext = str_tolower(GetFileExtension(filepath));
tinyusdz::Stage stage;
if (!tinyusdz::IsUSD(filepath)) {
std::cerr << "File not found or not a USD format: " << filepath << "\n";
}
bool ret = tinyusdz::LoadUSDFromFile(filepath, &stage, &warn, &err);
if (!warn.empty()) {
std::cerr << "WARN : " << warn << "\n";
}
if (!err.empty()) {
std::cerr << "ERR : " << err << "\n";
}
if (!ret) {
std::cerr << "Failed to load USD file: " << filepath << "\n";
return EXIT_FAILURE;
}
// bool is_usdz = tinyusdz::IsUSDZ(filepath);
std::string s = stage.ExportToString();
std::cout << s << "\n";
std::cout << "--------------------------------------"
<< "\n";
// Visit all Prims in the Stage.
auto prim_visit_fun = [](const tinyusdz::Path &abs_path, const tinyusdz::Prim &prim, const int32_t level, void *userdata, std::string *err) -> bool {
(void)err;
std::cout << tinyusdz::pprint::Indent(level) << "[" << level << "] (" << prim.data().type_name() << ") " << prim.local_path().prim_part() << " : AbsPath " << tinyusdz::to_string(abs_path) << "\n";
// Use as() or is() for Prim specific processing.
if (const tinyusdz::Material *pm = prim.as<tinyusdz::Material>()) {
(void)pm;
std::cout << tinyusdz::pprint::Indent(level) << " Got Material!\n";
// return false + `err` empty if you want to terminate traversal earlier.
//return false;
}
return true;
};
void *userdata = nullptr;
tinyusdz::tydra::VisitPrims(stage, prim_visit_fun, userdata);
std::cout << "--------------------------------------"
<< "\n";
// Compute Xform of each Prim at time t.
{
tinyusdz::tydra::XformNode xformnode;
double t = tinyusdz::value::TimeCode::Default();
tinyusdz::value::TimeSampleInterpolationType tinterp = tinyusdz::value::TimeSampleInterpolationType::Held; // Held or Linear
if (!tinyusdz::tydra::BuildXformNodeFromStage(stage, &xformnode, t, tinterp)) {
std::cerr << "BuildXformNodeFromStage error.\n";
} else {
std::cout << tinyusdz::tydra::DumpXformNode(xformnode) << "\n";
}
}
// Mapping hold the pointer to concrete Prim object,
// So stage content should not be changed(no Prim addition/deletion).
XformMap xformmap;
MeshMap meshmap;
MaterialMap matmap;
PreviewSurfaceMap surfacemap;
UVTextureMap texmap;
PrimvarReader_float2Map preadermap;
// Collect and make full path <-> Prim/Shader mapping
tinyusdz::tydra::ListPrims(stage, xformmap);
tinyusdz::tydra::ListPrims(stage, meshmap);
tinyusdz::tydra::ListPrims(stage, matmap);
tinyusdz::tydra::ListShaders(stage, surfacemap);
tinyusdz::tydra::ListShaders(stage, texmap);
tinyusdz::tydra::ListShaders(stage, preadermap);
//
// Query example
//
for (const auto &item : matmap) {
nonstd::expected<const tinyusdz::Prim*, std::string> mat = stage.GetPrimAtPath(tinyusdz::Path(item.first, /* prop name */""));
if (mat) {
std::cout << "Found Material <" << item.first << "> from Stage:\n";
if (const tinyusdz::Material *mp = mat.value()->as<tinyusdz::Material>()) { // this should be true though.
std::cout << tinyusdz::to_string(*mp) << "\n";
}
} else {
std::cerr << "Err: " << mat.error() << "\n";
}
}
for (const auto &item : surfacemap) {
// Returned Prim is Shader class
nonstd::expected<const tinyusdz::Prim*, std::string> shader = stage.GetPrimAtPath(tinyusdz::Path(item.first, /* prop name */""));
if (shader) {
std::cout << "Found Shader(UsdPreviewSurface) <" << item.first << "> from Stage:\n";
const tinyusdz::Shader *sp = shader.value()->as<tinyusdz::Shader>();
if (sp) { // this should be true though.
if (const tinyusdz::UsdPreviewSurface *surf = sp->value.as<tinyusdz::UsdPreviewSurface>()) {
// Print Shader
std::cout << tinyusdz::to_string(*sp) << "\n";
}
}
} else {
std::cerr << "Err: " << shader.error() << "\n";
}
}
for (const auto &item : texmap) {
// Returned Prim is Shader class
nonstd::expected<const tinyusdz::Prim*, std::string> shader = stage.GetPrimAtPath(tinyusdz::Path(item.first, /* prop name */""));
if (shader) {
std::cout << "Found Shader(UsdUVTexture) <" << item.first << "> from Stage:\n";
const tinyusdz::Shader *sp = shader.value()->as<tinyusdz::Shader>();
if (sp) { // this should be true though.
if (const tinyusdz::UsdUVTexture *tex = sp->value.as<tinyusdz::UsdUVTexture>()) {
std::cout << tinyusdz::to_string(*sp);
}
}
} else {
std::cerr << "Err: " << shader.error() << "\n";
}
}
for (const auto &item : preadermap) {
// Returned Prim is Shader class
nonstd::expected<const tinyusdz::Prim*, std::string> shader = stage.GetPrimAtPath(tinyusdz::Path(item.first, /* prop name */""));
if (shader) {
std::cout << "Found Shader(UsdPrimvarReader_float2) <" << item.first << "> from Stage:\n";
const tinyusdz::Shader *sp = shader.value()->as<tinyusdz::Shader>();
if (sp) { // this should be true though.
if (const tinyusdz::UsdPrimvarReader_float2 *preader = sp->value.as<tinyusdz::UsdPrimvarReader_float2>()) {
std::cout << tinyusdz::to_string(*sp) << "\n";
}
}
} else {
std::cerr << "Err: " << shader.error() << "\n";
}
}
//
// -- Querying Parent Prim
//
for (const auto &item : surfacemap) {
// Usually Parent is Material
std::string err;
if (const tinyusdz::Prim *p = tinyusdz::tydra::GetParentPrim(stage, tinyusdz::Path(item.first, /* property */""), &err)) {
std::cout << "Input path = " << tinyusdz::to_string(item.first) << "\n";
std::cout << "Parent prim = " << tinyusdz::prim::print_prim(*p) << "\n";
} else {
std::cerr << err;
}
}
std::cout << "GetProperty example -------------\n";
for (const auto &item : xformmap) {
std::string err;
tinyusdz::Property prop;
if (tinyusdz::tydra::GetProperty(*item.second, "xformOp:transform", &prop, &err)) {
std::cout << "Property value = " << print_prop(prop, "xformOp:transform", 0) << "\n";
} else {
std::cerr << err;
}
}
//
// Get bound Material
//
// - GetBoundMaterial:
// - Look Directly boundMaterial to the Prim.
// - If no material is asigned to the Prim, look into parent's BoundMaterial settings.
// - Also considers BindMaterial strength `bindMaterialAs` Prim metadatum
// - [ ] TODO: Consider materialBindingCollection
// - GetDirectlyBoundMaterial: Get BoundMaterial assigned to the Prim being queried.
//
// Usually GetBoundMaterial is what you want.
//
// See https://openusd.org/release/wp_usdshade.html for more details about Material assignment.
//
std::cout << "GetBoundMaterial example -------------\n";
for (const auto &item : meshmap) {
tinyusdz::Path matPath;
const tinyusdz::Material *material{nullptr};
std::string err;
bool ret = tinyusdz::tydra::GetDirectlyBoundMaterial(stage, tinyusdz::Path(item.first, ""), /* purpose */"", &matPath, &material, &err);
std::cout << "Prim : " << item.first << "\n";
if (ret) {
std::cout << "has directlyBoundMaterial: " << matPath.full_path_name() << "\n";
} else if (err.empty()) {
std::cout << "no directlyBoundMaterial" << "\n";
} else {
std::cout << "GetDirectlyBoundMaterial failed: " << err << "\n";
}
ret = tinyusdz::tydra::GetBoundMaterial(stage, tinyusdz::Path(item.first, ""), /* purpose */"", &matPath, &material, &err);
if (ret) {
std::cout << item.first << " has bound Material. Material Path = " << matPath << "\n";
std::cout << "mat = " << material << "\n";
if (material) {
std::cout << to_string(*material, /* indent */1) << "\n";
}
} else {
std::cout << "Bound material not found for Prim path : " << item.first << "\n";
}
}
//
// Shader attribute evaluation example.
//
std::cout << "EvaluateAttribute example -------------\n";
for (const auto &item : preadermap) {
// Returned Prim is Shader class
nonstd::expected<const tinyusdz::Prim*, std::string> shader = stage.GetPrimAtPath(tinyusdz::Path(item.first, /* prop name */""));
if (shader) {
std::cout << "Shader(UsdPrimvarReader_float2) <" << item.first << "> from Stage:\n";
const tinyusdz::Shader *sp = shader.value()->as<tinyusdz::Shader>();
if (sp) { // this should be true though.
if (const tinyusdz::UsdPrimvarReader_float2 *preader = sp->value.as<tinyusdz::UsdPrimvarReader_float2>()) {
tinyusdz::tydra::TerminalAttributeValue tav;
std::string err;
bool ret = tinyusdz::tydra::EvaluateAttribute(stage, *shader.value(), "inputs:varname", &tav, &err);
if (!ret) {
std::cout << "Resolving `inputs:varname` failed: " << err << "\n";
}
std::cout << "type = " << tav.type_name() << "\n";
if (auto pv = tav.as<tinyusdz::value::token>()) {
std::cout << "inputs:varname = " << (*pv) << "\n";
}
}
}
} else {
std::cerr << "Err: " << shader.error() << "\n";
}
}
return 0;
}