-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
api-tutorial-main.cc
745 lines (612 loc) · 23.2 KB
/
api-tutorial-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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
#include <iostream>
// All-in-one TinyUSDZ core, but pprint feature is not included to speed up compilation.
#include "tinyusdz.hh"
// Import to_string() and operator<< features
#include "pprinter.hh"
#include "prim-pprint.hh"
#include "value-pprint.hh"
#if defined(TINYUSDZ_WITH_TYDRA)
// Tydra is a collection of APIs to access/convert USD Prim data
// (e.g. Get Prim's attribute by name)
// See <tinyusdz>/examples/tydra_api for more info about Tydra API.
#include "tydra/scene-access.hh"
#endif
//
// create a Scene
//
void CreateScene(tinyusdz::Stage *stage) {
// TinyUSDZ API does not use mutex, smart pointers(e.g. unique_ptr/shared_ptr) and C++
// exception. Also threading is opional in TinyUSDZ and currently
// multi-threading is not used. `value::token` implementation is simply copy
// string for each instances(like C++11 ABI std::string). (on the contrary,
// pxrUSD's token implementation uses string-to-id, which requires mutex lock
// to make stringId unique. But we think this is inefficient on modern
// multi-core CPU, since they locks the mutex every `token` value
// instanciation)
//
// TinyUSDZ's API is not fully multi-thread safe, thus if you want to
// manipulate a scene(Stage) in multi-threaded context, The app must take care
// of resource locks in the app layer.
std::string err;
//
// Create simple material with UsdPrevieSurface.
//
tinyusdz::Material mat;
mat.name = "mat";
tinyusdz::Shader shader; // Shader container
shader.name = "defaultPBR";
{
tinyusdz::UsdPreviewSurface surfaceShader; // Concrete Shader node object
//
// Asssign actual shader object to Shader::value.
// Also do not forget set its shader node type name through Shader::info_id
//
shader.info_id = tinyusdz::kUsdPreviewSurface; // "UsdPreviewSurface" token
//
// Currently no shader network/connection API.
// Manually construct it.
//
surfaceShader.outputsSurface.set_authored(
true); // Author `token outputs:surface`
surfaceShader.metallic = 0.3f;
// TODO: UsdUVTexture, UsdPrimvarReader***, UsdTransform2d
// Connect to UsdPreviewSurface's outputs:surface by setting targetPath.
//
// token outputs:surface = </mat/defaultPBR.outputs:surface>
mat.surface.set(tinyusdz::Path(/* prim path */ "/mat/defaultPBR",
/* prop path */ "outputs:surface"));
//
// Shaer::value is `value::Value` type, so can use '=' to assign Shader
// object.
//
shader.value = std::move(surfaceShader);
}
tinyusdz::Prim shaderPrim(shader);
tinyusdz::Prim matPrim(mat);
// matPrim.children().emplace_back(std::move(shaderPrim)); // no uniqueness
// check
// Use add_child() to ensure child Prim has unique name.
if (!matPrim.add_child(std::move(shaderPrim),
/* rename Prim name if_required */ true, &err)) {
std::cerr << "Failed to constrcut Scene: " << err << "\n";
exit(-1);
}
//
// To construct Prim, first create concrete Prim object(e.g. Xform, GeomMesh),
// then add it to tinyusdz::Prim.
//
//
tinyusdz::Xform xform;
{
xform.name = "root"; // Prim's name(elementPath)
{
tinyusdz::XformOp op;
op.op_type = tinyusdz::XformOp::OpType::Transform;
tinyusdz::value::matrix4d a0;
tinyusdz::value::matrix4d b0;
tinyusdz::Identity(&a0);
tinyusdz::Identity(&b0);
a0.m[1][1] = 2.1;
// column major, so [3][0], [3][1], [3][2] = translate X, Y, Z
b0.m[3][0] = 1.0;
b0.m[3][1] = 3.1;
b0.m[3][2] = 5.1;
tinyusdz::value::matrix4d transform = a0 * b0;
op.set_value(transform);
// `xformOpOrder`(token[]) is represented as std::vector<XformOp>
xform.xformOps.push_back(op);
}
{
// `xformOp:***` attribute is represented as XformOp class
tinyusdz::XformOp op;
op.op_type = tinyusdz::XformOp::OpType::Translate;
tinyusdz::value::double3 translate;
translate[0] = 1.0;
translate[1] = 2.0;
translate[2] = 3.0;
op.set_value(translate);
// `xformOpOrder`(token[]) is represented as std::vector<XformOp>
xform.xformOps.push_back(op);
}
{
// .suffix will be prepended to `xformOp:translate`
// 'xformOp:translate:move'
tinyusdz::XformOp op;
op.op_type = tinyusdz::XformOp::OpType::Translate;
op.suffix = "move";
tinyusdz::value::double3 translate;
// TimeSamples value can be added with `set_timesample`
// NOTE: TimeSamples data will be automatically sorted by time when using
// it.
translate[0] = 0.0;
translate[1] = 0.0;
translate[2] = 0.0;
op.set_timesample(0.0, translate);
translate[0] = 1.0;
translate[1] = 0.1;
translate[2] = 0.3;
op.set_timesample(1.0, translate);
xform.xformOps.push_back(op);
}
}
tinyusdz::GeomMesh mesh;
{
mesh.name = "quad";
{
std::vector<tinyusdz::value::point3f> pts;
pts.push_back({0.0f, 0.0f, 0.0f});
pts.push_back({1.0f, 0.0f, 0.0f});
pts.push_back({1.0f, 1.0f, 0.0f});
pts.push_back({0.0f, 1.0f, 0.0f});
mesh.points.set_value(pts);
}
{
// quad plane composed of 2 triangles.
std::vector<int> indices;
std::vector<int> counts;
counts.push_back(3);
counts.push_back(3);
mesh.faceVertexCounts.set_value(counts);
indices.push_back(0);
indices.push_back(1);
indices.push_back(2);
indices.push_back(0);
indices.push_back(2);
indices.push_back(3);
mesh.faceVertexIndices.set_value(indices);
}
// primvar and custom attribute can be added to generic Property container
// `props`(map<std::string, Property>)
{
// primvar is simply an attribute with prefix `primvars:`
//
// texCoord2f[] primvars:uv = [ ... ] ( interpolation = "vertex" )
// int[] primvars:uv:indices = [ ... ]
//
{
tinyusdz::Attribute uvAttr;
std::vector<tinyusdz::value::texcoord2f> uvs;
uvs.push_back({0.0f, 0.0f});
uvs.push_back({1.0f, 0.0f});
uvs.push_back({1.0f, 1.0f});
uvs.push_back({0.0f, 1.0f});
// Fast path. Set the value directly to Attribute.
uvAttr.set_value(uvs);
// or we can first build primvar::PrimVar
// tinyusdz::primvar::PrimVar uvVar;
// uvVar.set_value(uvs);
// uvAttr.set_var(std::move(uvVar));
// Currently `interpolation` is described in Attribute metadataum.
// You can set builtin(predefined) Attribute Metadatum(e.g.
// `interpolation`, `hidden`) through `metas()`.
uvAttr.metas().interpolation = tinyusdz::Interpolation::Vertex;
tinyusdz::Property uvProp(uvAttr);
mesh.props.emplace("primvars:uv", uvProp);
// ----------------------
tinyusdz::Attribute uvIndexAttr;
std::vector<int> uvIndices;
// FIXME: Validate
uvIndices.push_back(0);
uvIndices.push_back(1);
uvIndices.push_back(3);
uvIndices.push_back(2);
tinyusdz::primvar::PrimVar uvIndexVar;
uvIndexVar.set_value(uvIndices);
uvIndexAttr.set_var(std::move(uvIndexVar));
// Or you can use this approach(if you want to keep a copy of PrimVar
// data)
// uvIndexAttr.set_var(uvIndexVar);
tinyusdz::Property uvIndexProp(uvIndexAttr);
mesh.props.emplace("primvars:uv:indices", uvIndexProp);
}
// `custom uniform double myvalue = 3.0 ( hidden = 0 )`
{
tinyusdz::Attribute attrib;
double myvalue = 3.0;
tinyusdz::primvar::PrimVar var;
var.set_value(myvalue);
attrib.set_var(std::move(var));
attrib.variability() = tinyusdz::Variability::Uniform;
attrib.metas().hidden = false;
// NOTE: `custom` keyword would be deprecated in the future USD syntax,
// so you can set it false.
tinyusdz::Property prop(attrib, /* custom*/ true);
mesh.props.emplace("myvalue", prop);
}
// Add Primvar through GeomPrimvar;
{
tinyusdz::GeomPrimvar uvPrimvar;
uvPrimvar.set_name("my_uv");
std::vector<tinyusdz::value::texcoord2f> uvs;
uvs.push_back({0.0f, 0.0f});
uvs.push_back({1.0f, 0.0f});
uvs.push_back({1.0f, 1.0f});
uvs.push_back({0.0f, 1.0f});
uvPrimvar.set_value(uvs); // value at 'default' time
uvPrimvar.set_interpolation(tinyusdz::Interpolation::Vertex);
std::vector<int> uvIndices;
uvIndices.push_back(0);
uvIndices.push_back(1);
uvIndices.push_back(3);
uvIndices.push_back(2);
// set indices at 'default' time.
uvPrimvar.set_default_indices(uvIndices);
// NOTE: You can use `uvPrimvar.set_timesampled_indices` to set time-varying indices
// {
// tinyusdz::TypedTimeSamples<std::vector<int>> uvIndicesTimeSampled;
// std::vector<int> uvIndices3;
// uvIndices3.push_back(1);
// uvIndices3.push_back(0);
// uvIndices3.push_back(2);
// uvIndices3.push_back(3);
// uvIndicesTimeSampled.add_sample(1.0, uvIndices);
// uvIndicesTimeSampled.add_sample(3.0, uvIndices3);
// uvPrimvar.set_timesampled_indices(uvIndicesTimeSampled);
// }
// primvar name is extracted from Primvar::name
std::string err;
if (!mesh.set_primvar(uvPrimvar, &err)) {
std::cerr << "Failed to add Primar: " << err << "\n";
}
}
}
// Material binding(`rel material:binding` is done by manually setup
// targetPath.
tinyusdz::Relationship materialBinding;
materialBinding.set(
tinyusdz::Path(/* prim path */ "/mat/defaultPBR", /* prop path */ ""));
mesh.materialBinding = materialBinding;
// TODO: Explicitly author MaterialBindingAPI apiSchema in Mesh Prim.
}
tinyusdz::GeomSphere sphere1;
{
sphere1.name = "sphere";
sphere1.radius = 3.14;
}
tinyusdz::GeomSphere sphere2;
{
sphere2.name =
"sphere"; // name will be modified to be unique at add_child().
sphere2.radius = 1.05;
}
//
// Create Scene(Stage) hierarchy.
// You can add child Prim to parent Prim's `children()`.
//
// [Xform]
// |
// +- [Mesh]
// +- [Sphere0]
// +- [Sphere1]
//
// [Material]
// |
// +- [Shader]
//
// Prim's elementName is read from concrete Prim class(GeomMesh::name,
// Xform::name, ...)
tinyusdz::Prim meshPrim(mesh);
{
// `references`, `paylaod`
{
tinyusdz::Reference ref;
ref.asset_path = tinyusdz::value::AssetPath("submesh-000.usd");
std::vector<tinyusdz::Reference> referencesList;
referencesList.push_back(ref);
meshPrim.metas().references =
std::make_pair(tinyusdz::ListEditQual::Append, referencesList);
}
{
tinyusdz::Payload pl;
pl.asset_path = tinyusdz::value::AssetPath("submesh-payload-000.usd");
std::vector<tinyusdz::Payload> payloadList;
payloadList.push_back(pl);
meshPrim.metas().payload =
std::make_pair(tinyusdz::ListEditQual::Append, payloadList);
}
}
tinyusdz::Prim spherePrim(sphere1);
{
// variantSet is maniuplated in Prim.
// Currently we don't provide easy API for variantSet.
// Need to add variantSet information manually for a while.
tinyusdz::VariantSelectionMap vsmap;
// List of variantSet in the Prim.
std::vector<std::string> variantSetList;
variantSetList.push_back("colorVariant");
// key = variantSet name, value = default Variant selection
vsmap.emplace("colorVariant", "red");
spherePrim.metas().variants = vsmap;
spherePrim.metas().variantSets =
std::make_pair(tinyusdz::ListEditQual::Append, variantSetList);
// VariantSet is composed of metas + properties + childPrims
tinyusdz::VariantSet variantSet;
tinyusdz::Variant redVariant;
redVariant.metas().comment = "red color";
tinyusdz::value::color3f redColor({1.0f, 0.0f, 0.0f});
tinyusdz::Attribute redColorAttr;
redColorAttr.set_value(redColor);
redVariant.properties().emplace("mycolor", redColorAttr);
// TODO: Add example to add childPrims under Variant
// redVariant.primChildren().emplace(...)
tinyusdz::Variant greenVariant;
greenVariant.metas().comment = "green color";
tinyusdz::value::color3f greenColor({0.0f, 1.0f, 0.0f});
tinyusdz::Attribute greenColorAttr;
greenColorAttr.set_value(greenColor);
greenVariant.properties().emplace("mycolor", greenColorAttr);
variantSet.name = "red";
variantSet.variantSet.emplace("red", redVariant);
variantSet.name = "green";
variantSet.variantSet.emplace("green", greenVariant);
spherePrim.variantSets().emplace("colorVariant", variantSet);
}
tinyusdz::Prim spherePrim2(sphere2);
tinyusdz::Prim xformPrim(xform);
// xformPrim.children().emplace_back(std::move(meshPrim));
// xformPrim.children().emplace_back(std::move(spherePrim));
// Use add_child() to ensure child Prim has unique name.
if (!xformPrim.add_child(std::move(meshPrim),
/* rename Prim name if_required */ true, &err)) {
std::cerr << "Failed to constrcut Scene: " << err << "\n";
exit(-1);
}
if (!xformPrim.add_child(std::move(spherePrim),
/* rename Prim name if_required */ true, &err)) {
std::cerr << "Failed to constrcut Scene: " << err << "\n";
}
#if 1
// Must set rename Prim arg `true`, otherwise `add_child` fails since
// spherePrim2 does not have valid & unique Prim name.
if (!xformPrim.add_child(std::move(spherePrim2),
/* rename Prim name if_required */ true, &err)) {
std::cerr << "Failed to constrcut Scene: " << err << "\n";
exit(-1);
}
if (xformPrim.children().size() != 3) {
std::cerr << "Internal error. num child Prims must be 3, but got "
<< xformPrim.children().size() << "\n";
exit(-1);
}
// If you want to specify the appearance/traversal order of child Prim(e.g.
// Showing Prim tree in GUI, Ascii output), set "primChildren"(token[])
// metadata xfromPrim.metas().primChildren.size() must be identical to
// xformPrim.children().size()
xformPrim.metas().primChildren.push_back(
tinyusdz::value::token(xformPrim.children()[1].element_name()));
xformPrim.metas().primChildren.push_back(
tinyusdz::value::token(xformPrim.children()[0].element_name()));
xformPrim.metas().primChildren.push_back(
tinyusdz::value::token(xformPrim.children()[2].element_name()));
#else
// You can replace(or add if corresponding Prim does not exist) existing child
// Prim using replace_child()
if (!xformPrim.replace_child("sphere", std::move(spherePrim2), &err)) {
std::cerr << "Failed to constrcut Scene: " << err << "\n";
exit(-1);
}
if (xformPrim.children().size() != 2) {
std::cerr << "Internal error. num child Prims must be 2, but got "
<< xformPrim.children().size() << "\n";
exit(-1);
}
#endif
///
/// Add subLayers
///
std::vector<tinyusdz::SubLayer> sublayers;
tinyusdz::SubLayer slayer0;
slayer0.assetPath = tinyusdz::value::AssetPath("sublayer-000.usd");
sublayers.push_back(slayer0);
stage->metas().subLayers = sublayers;
stage->metas().defaultPrim =
tinyusdz::value::token(xformPrim.element_name()); // token
if (!stage->add_root_prim(std::move(xformPrim))) {
std::cerr << "Failed to add Prim to Stage root: " << stage->get_error()
<< "\n";
exit(-1);
}
if (!stage->add_root_prim(std::move(matPrim))) {
std::cerr << "Failed to add Prim to Stage root: " << stage->get_error()
<< "\n";
exit(-1);
}
// You can replace(or add if given Prim name does not exist in the Stage) root
// Prim using `replace_root_prim`.
#if 0
{
tinyusdz::Scope scope;
tinyusdz::Prim scopePrim(scope);
if (!stage->replace_root_prim(/* root Prim name */"root", std::move(scopePrim))) {
std::cerr << "Failed to replace Prim to Stage root: " << stage->get_error() << "\n";
exit(-1);
}
}
#endif
// You can add Stage metadatum through Stage::metas()
stage->metas().comment = "Generated by TinyUSDZ api_tutorial.";
{
// Dictionary(alias to CustomDataType) is similar to VtDictionary.
// it is a map<string, MetaVariable>
// MetaVariable is similar to Value, but accepts limited variation of
// types(double, token, string, float3[], ...)
tinyusdz::Dictionary customData;
tinyusdz::MetaVariable metavar;
double mycustom = 1.3;
metavar.set_value("mycustom", mycustom);
std::string mystring = "hello";
tinyusdz::MetaVariable metavar2("mystring", mystring);
customData.emplace("mycustom", metavar);
customData.emplace("mystring", metavar2);
customData.emplace("myvalue", 2.45); // Construct MetaVariables implicitly
// You can also use SetCustomDataByKey to set custom value with key having
// namespaces(':')
tinyusdz::MetaVariable intval = int(5);
tinyusdz::SetCustomDataByKey("mydict:myval", intval,
/* inout */ customData);
stage->metas().customLayerData = customData;
}
// Commit Stage.
// Internally, it calls Stage::compute_absolute_prim_path_and_assign_prim_id()
// to compute absolute Prim path and assign unique Prim id to each Prims in
// the Stage. NOTE: Stage::metas() is not affected by `commit` API, so you can
// call `commit` before manipulating StageMetas through `Stage::metas()`
if (!stage->commit()) {
std::cerr << "Failed to commit Stage. ERR: " << stage->get_error() << "\n";
exit(-1);
}
}
int main(int argc, char **argv) {
tinyusdz::Stage stage; // empty scene
CreateScene(&stage);
if (stage.get_warning().size()) {
std::cout << "WARN in Stage: " << stage.get_warning() << "\n";
}
// Print USD scene as Ascii.
std::cout << to_string(stage) << "\n";
// std::cout << stage.ExportToString() << "\n"; // you can also use pxrUSD
// compatible ExportToString().
// Dump Prim tree info.
std::cout << stage.dump_prim_tree() << "\n";
{
tinyusdz::Path path(/* absolute prim path */ "/root",
/* property path */ "");
const tinyusdz::Prim *prim{nullptr};
std::string err;
bool ret = stage.find_prim_at_path(path, prim, &err);
if (ret) {
std::cout << "Found Prim at path: " << tinyusdz::to_string(path) << "\n";
std::cout << "Prim ID: " << prim->prim_id() << "\n";
std::cout << "Prim's absolute_path: "
<< tinyusdz::to_string(prim->absolute_path()) << "\n";
} else {
std::cerr << err << "\n";
}
if (!prim) {
// This should not be happen though.
std::cerr << "Prim is null\n";
return -1;
}
if (!prim->is<tinyusdz::Xform>()) {
std::cerr << "Expected Xform prim."
<< "\n";
return -1;
}
// Cast to Xform
const tinyusdz::Xform *xform = prim->as<tinyusdz::Xform>();
if (!xform) {
std::cerr << "Expected Xform prim."
<< "\n";
return -1;
}
}
// find Prim by prim_id
{
uint64_t prim_id = 2;
const tinyusdz::Prim *prim{nullptr};
std::string err;
bool ret = stage.find_prim_by_prim_id(prim_id, prim, &err);
if (ret && prim) {
std::cout << "Found Prim by ID: " << prim_id << "\n";
std::cout << "Prim's absolute_path: "
<< tinyusdz::to_string(prim->absolute_path()) << "\n";
} else {
std::cerr << err << "\n";
}
}
// GetAttribute and GeomPrimvar
{
tinyusdz::Path path(/* absolute prim path */ "/root/quad",
/* property path */ "");
const tinyusdz::Prim *prim{nullptr};
std::string err;
bool ret = stage.find_prim_at_path(path, prim, &err);
if (ret && prim) {
std::cout << "Found Prim at path: " << tinyusdz::to_string(path) << "\n";
std::cout << "Prim ID: " << prim->prim_id() << "\n";
std::cout << "Prim's absolute_path: "
<< tinyusdz::to_string(prim->absolute_path()) << "\n";
} else {
std::cerr << err << "\n";
}
if (!prim) {
// This should not be happen though.
std::cerr << "Prim is null\n";
return -1;
}
const tinyusdz::GeomMesh *mesh = prim->as<tinyusdz::GeomMesh>();
if (!mesh) {
std::cerr << "Expected GeomMesh.\n";
return -1;
}
#if defined(TINYUSDZ_WITH_TYDRA)
tinyusdz::Attribute attr;
// TODO: Use EvaluateAttribute
if (tinyusdz::tydra::GetAttribute(*prim, "points", &attr, &err)) {
std::cout << "point attribute type = " << attr.type_name() << "\n";
// Ensure Attribute has a value(not Attribute connection)
if (attr.is_value()) {
std::vector<tinyusdz::value::point3f> pts;
if (attr.is_timesamples()) {
// TODO: timesamples
} else {
if (attr.get_value(&pts)) {
std::cout << "point attribute value = " << pts << "\n";
}
}
}
} else {
std::cerr << err << "\n";
}
#else
// limitation: `points` attribute cannot be attribute connection
std::cout << "point attribute value = " << mesh->get_points() << "\n";
#endif
// GeomPrimvar
// Access GeomPrimvar
{
std::cout << "uv is primvar? " << mesh->has_primvar("uv") << "\n";
tinyusdz::GeomPrimvar primvar;
std::string err;
if (mesh->get_primvar("uv", &primvar, &err)) {
std::cout << "uv primvar has default indices? " << primvar.has_default_indices()
<< "\n";
std::cout << "uv primvar has timesampled indices? " << primvar.has_timesampled_indices()
<< "\n";
} else {
std::cerr << "get_primvar(\"uv\") failed. err = " << err << "\n";
}
// Equivalent to pxr::UsdGeomPrimvar::ComputeFlattened().
// elems[i] = values[indices[i]]
tinyusdz::value::Value value;
if (primvar.flatten_with_indices(&value, &err)) {
// value;:Value can contain any types, but value.array_size() should
// work well only for primvar types(e.g. `float[]`, `color3f[]`) It
// would report 0 for non-primvar types(e.g.`std::vector<Xform>`)
std::cout << "uv primvars. array size = " << value.array_size() << "\n";
std::cout << "uv primvars. expand_by_indices result = "
<< tinyusdz::value::pprint_value(value) << "\n";
} else {
std::cerr << "expand_by_indices failed. err = " << err << "\n";
}
// Typed version
std::vector<tinyusdz::value::texcoord2f> uvs;
if (primvar.flatten_with_indices(&uvs, &err)) {
// value;:Value can contain any types, but value.array_size() should
// work well only for primvar types(e.g. `float[]`, `color3f[]`) It
// would report 0 for non-primvar types(e.g.`std::vector<Xform>`)
std::cout << "uv primvars. array size = " << uvs.size() << "\n";
std::cout << "uv primvars. expand_by_indices result = "
<< tinyusdz::value::pprint_value(uvs) << "\n";
} else {
std::cerr << "expand_by_indices failed. err = " << err << "\n";
}
std::vector<tinyusdz::GeomPrimvar> gpvars = mesh->get_primvars();
std::cout << "# of primvars = " << gpvars.size();
for (const auto &item : gpvars) {
std::cout << " primvar = " << item.name() << "\n";
}
}
}
return EXIT_SUCCESS;
}