Skip to content

Commit 4f0a56e

Browse files
committed
Add --parse-only flag(Read USD only)
Simplify USD load code.
1 parent f93caa5 commit 4f0a56e

File tree

1 file changed

+60
-69
lines changed

1 file changed

+60
-69
lines changed

examples/tusdcat/main.cc

+60-69
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "str-util.hh"
1010
#include "io-util.hh"
1111

12+
#include "tydra/scene-access.hh"
13+
1214
struct CompositionFeatures {
1315
bool subLayers{true};
1416
bool inherits{true};
@@ -31,9 +33,8 @@ static std::string str_tolower(std::string s) {
3133
return s;
3234
}
3335

34-
int main(int argc, char **argv) {
35-
if (argc < 2) {
36-
std::cout << "Usage tusdcat [--flatten] [--composition=STRLIST] [--relative] [--extract-variants] input.usda/usdc/usdz\n";
36+
void print_help() {
37+
std::cout << "Usage tusdcat [--flatten] [--composition=STRLIST] [--relative] [--extract-variants] [--parse-only] input.usda/usdc/usdz\n";
3738
std::cout << "\n --flatten (not fully implemented yet) Do composition(load sublayers, refences, payload, evaluate `over`, inherit, variants..)";
3839
std::cout << " --composition: Specify which composition feature to be "
3940
"enabled(valid when `--flatten` is supplied). Comma separated "
@@ -43,12 +44,20 @@ int main(int argc, char **argv) {
4344
"--composition=r,p --composition=references,subLayers\n";
4445
std::cout << "\n --extract-variants (w.i.p) Dump variants information to .json\n";
4546
std::cout << "\n --relative (not implemented yet) Print Path as relative Path\n";
47+
std::cout << "\n --parse-only Parse USD file only(Check if input USD is valid or not)\n";
48+
49+
}
50+
51+
int main(int argc, char **argv) {
52+
if (argc < 2) {
53+
print_help();
4654
return EXIT_FAILURE;
4755
}
4856

4957
bool has_flatten{false};
5058
bool has_relative{false};
5159
bool has_extract_variants{false};
60+
bool parse_only{false};
5261

5362
constexpr int kMaxIteration = 128;
5463

@@ -59,10 +68,15 @@ int main(int argc, char **argv) {
5968

6069
for (size_t i = 1; i < argc; i++) {
6170
std::string arg = argv[i];
62-
if (arg.compare("--flatten") == 0) {
71+
if ((arg.compare("-h") == 0) || (arg.compare("--help") ==0)) {
72+
print_help();
73+
return EXIT_FAILURE;
74+
} else if (arg.compare("--flatten") == 0) {
6375
has_flatten = true;
6476
} else if (arg.compare("--relative") == 0) {
6577
has_relative = true;
78+
} else if (arg.compare("--parse-only") == 0) {
79+
parse_only = true;
6680
} else if (arg.compare("--extract-variants") == 0) {
6781
has_extract_variants = true;
6882
} else if (tinyusdz::startsWith(arg, "--composition=")) {
@@ -119,6 +133,11 @@ int main(int argc, char **argv) {
119133

120134
if (has_flatten) {
121135

136+
if (parse_only) {
137+
std::cerr << "--flatten and --parse-only cannot be specified at a time\n";
138+
return EXIT_FAILURE;
139+
}
140+
122141
// TODO: flatten for USDZ
123142
if (tinyusdz::IsUSDZ(filepath)) {
124143

@@ -305,6 +324,7 @@ int main(int argc, char **argv) {
305324
std::cout << "# of composition iteration to resolve fully: " << (i + 1) << "\n";
306325
break;
307326
}
327+
308328
}
309329

310330
if (has_extract_variants) {
@@ -317,80 +337,51 @@ int main(int argc, char **argv) {
317337

318338
}
319339

320-
} else {
340+
tinyusdz::Stage comp_stage;
341+
ret = LayerToStage(src_layer, &comp_stage, &warn, &err);
342+
if (warn.size()) {
343+
std::cout << warn<< "\n";
344+
}
321345

322-
tinyusdz::Stage stage;
346+
if (!ret) {
347+
std::cerr << err << "\n";
348+
}
349+
350+
std::cout << comp_stage.ExportToString() << "\n";
323351

324-
if (ext.compare("usdc") == 0) {
325-
tinyusdz::USDLoadOptions options;
326-
options.do_composition = has_flatten;
352+
using MeshMap = std::map<std::string, const tinyusdz::GeomMesh *>;
353+
MeshMap meshmap;
327354

328-
bool ret = tinyusdz::LoadUSDCFromFile(filepath, &stage, &warn, &err, options);
329-
if (!warn.empty()) {
330-
std::cerr << "WARN : " << warn << "\n";
331-
}
332-
if (!err.empty()) {
333-
std::cerr << "ERR : " << err << "\n";
334-
//return EXIT_FAILURE;
335-
}
355+
tinyusdz::tydra::ListPrims(comp_stage, meshmap);
336356

337-
if (!ret) {
338-
std::cerr << "Failed to load USDC file: " << filepath << "\n";
339-
return EXIT_FAILURE;
340-
}
341-
} else if (ext.compare("usda") == 0) {
342-
tinyusdz::USDLoadOptions options;
343-
options.do_composition = has_flatten;
357+
for (const auto &item : meshmap) {
344358

345-
bool ret = tinyusdz::LoadUSDAFromFile(filepath, &stage, &warn, &err, options);
346-
if (!warn.empty()) {
347-
std::cerr << "WARN : " << warn << "\n";
348-
}
349-
if (!err.empty()) {
350-
std::cerr << "ERR : " << err << "\n";
351-
//return EXIT_FAILURE;
352-
}
359+
std::cout << "Prim : " << item.first << "\n";
360+
}
353361

354-
if (!ret) {
355-
std::cerr << "Failed to load USDA file: " << filepath << "\n";
356-
return EXIT_FAILURE;
357-
}
358-
} else if (ext.compare("usdz") == 0) {
359-
if (has_flatten) {
360-
std::cout << "--flatten is ignored for USDZ model at the moment.\n";
361-
}
362-
//std::cout << "usdz\n";
363-
bool ret = tinyusdz::LoadUSDZFromFile(filepath, &stage, &warn, &err);
364-
if (!warn.empty()) {
365-
std::cerr << "WARN : " << warn << "\n";
366-
}
367-
if (!err.empty()) {
368-
std::cerr << "ERR : " << err << "\n";
369-
//return EXIT_FAILURE;
370-
}
362+
} else {
371363

372-
if (!ret) {
373-
std::cerr << "Failed to load USDZ file: " << filepath << "\n";
374-
return EXIT_FAILURE;
375-
}
376-
} else {
377-
tinyusdz::USDLoadOptions options;
378-
options.do_composition = has_flatten;
364+
tinyusdz::Stage stage;
379365

380-
// try to auto detect format.
381-
bool ret = tinyusdz::LoadUSDFromFile(filepath, &stage, &warn, &err, options);
382-
if (!warn.empty()) {
383-
std::cerr << "WARN : " << warn << "\n";
384-
}
385-
if (!err.empty()) {
386-
std::cerr << "ERR : " << err << "\n";
387-
//return EXIT_FAILURE;
388-
}
366+
tinyusdz::USDLoadOptions options;
389367

390-
if (!ret) {
391-
std::cerr << "Failed to load USD file: " << filepath << "\n";
392-
return EXIT_FAILURE;
393-
}
368+
// auto detect format.
369+
bool ret = tinyusdz::LoadUSDFromFile(filepath, &stage, &warn, &err, options);
370+
if (!warn.empty()) {
371+
std::cerr << "WARN : " << warn << "\n";
372+
}
373+
if (!err.empty()) {
374+
std::cerr << "ERR : " << err << "\n";
375+
//return EXIT_FAILURE;
376+
}
377+
378+
if (!ret) {
379+
std::cerr << "Failed to load USD file: " << filepath << "\n";
380+
return EXIT_FAILURE;
381+
}
382+
383+
if (parse_only) {
384+
return EXIT_SUCCESS;
394385
}
395386

396387
std::string s = stage.ExportToString(has_relative);

0 commit comments

Comments
 (0)