DATAKIT SDK  V2026.4
testlibusdwrite.cpp File Reference

Functions

Dtk_ErrorStatus UsdwInstances (const Dtk_string &inOutputFileName)
 Sample 2: writes 10 re-instanced cubes into a USD file, demonstrating USD referencing. More...
 
DtkErrorStatus UsdWriteSample (const Dtk_string &inResultDirectory)
 
Dtk_ErrorStatus UsdwThreeMeshes (const Dtk_string &inOutputFileName)
 Sample 1: writes a cube and a cylinder mesh in a two-node hierarchy into a USD file. More...
 

Function Documentation

◆ UsdwInstances()

Dtk_ErrorStatus UsdwInstances ( const Dtk_string &  inOutputFileName)

Sample 2: writes 10 re-instanced cubes into a USD file, demonstrating USD referencing.

The first OpenInstance() call with prototype ID 1 creates the real Xform+Mesh prim All subsequent calls with the same prototype ID emit a USD reference to that prim, each with its own transform, avoiding geometry duplication.

60 { // SAMPLE 2
62  Dtk_transfo cubeTransfo;
63  Dtk_bool isNewNode; // true only for the first occurrence of a given protoID
64 
66  // Initialise the writer
67  WriterOptions options;
68  Dtk_UsdWriter usdWriter( inOutputFileName, err, options );
69  if( err != dtkNoError )
70  return err;
71 
72  Dtk_bool unusedIsNew;
73  usdWriter.OpenInstance( "RootNode", -1, unusedIsNew );
74 
75  for( int instanceIdx = 0; instanceIdx < 10; ++instanceIdx )
76  {
77  // All calls share prototype ID 1 so they reference the same prim
78  usdWriter.OpenInstance( "Cube", 1, isNewNode, cubeTransfo );
79  if( isNewNode )
80  {
81  usdWriter.WriteMesh( cubeMesh );
82  }
83  usdWriter.CloseLastInstance();
84 
85  // Shift the transform for the next instance along the X axis.
86  cubeTransfo.addTranslate( Dtk_dir( 10, 0, 0 ) );
87  }
88 
89  usdWriter.CloseLastInstance();
90  return dtkNoError;
91 }

◆ UsdWriteSample()

DtkErrorStatus UsdWriteSample ( const Dtk_string &  inResultDirectory)
95 {
96  Dtk_string outputDirectory, fileName;
97  std::cout << std::endl << "----------------------------------------------" << std::endl;
98 
99  // Choose the output directory and create it if necessary
100  outputDirectory = inResultDirectory + L"Usd/";
101  outputDirectory.FixPathSeparator();
102  outputDirectory.mkdir();
103 
104  std::cout << "Usd Write start" << std::endl;
105 
106  std::cout << "sample 1" << std::endl;
107  // Sample 1: simple three-mesh hierarchy.
108  fileName = outputDirectory + L"threemeshes.usd";
109  Dtk_ErrorStatus errorStatus = UsdwThreeMeshes( fileName );
110  if( errorStatus == dtkNoError )
111  std::cout << "=> " << fileName.c_str() << std::endl;
112  else
113  std::cout << "error : " << dtkTypeError( errorStatus ).c_str() << std::endl;
114 
115  std::cout << "sample 2" << std::endl;
116  // Sample 2: geometry instancing with USD references
117  fileName = outputDirectory + L"instances.usd";
118  errorStatus = UsdwInstances( fileName );
119  if( errorStatus == dtkNoError )
120  std::cout << "=> " << fileName.c_str() << std::endl;
121  else
122  std::cout << "error : " << dtkTypeError( errorStatus ).c_str() << std::endl;
123 
124  std::cout << "Usd Write end" << std::endl;
125 
126  return errorStatus;
127 }

◆ UsdwThreeMeshes()

Dtk_ErrorStatus UsdwThreeMeshes ( const Dtk_string &  inOutputFileName)

Sample 1: writes a cube and a cylinder mesh in a two-node hierarchy into a USD file.

13 { // SAMPLE 1
14  Dtk_MeshPtr cubeMesh, cylinderMesh, wireMesh;
15  Dtk_bool unusedIsNew; // not needed for this sample
16 
17  // Create geometry
18  cubeMesh = sampleWriter::CreateMeshCube();
19  cylinderMesh = sampleWriter::CreateMeshCylinder( 30 );
20  wireMesh = sampleWriter::CreateMeshWire();
21 
23  // Initialise the writer; binary file with .usd extension
24  WriterOptions options;
25  Dtk_UsdWriter usdWriter( inOutputFileName, err, options );
26  if( err != dtkNoError )
27  return err;
28 
29  // Create the root Xform prim
30  usdWriter.OpenInstance( "RootNode", -1, unusedIsNew );
31 
32  // Add a child prim for the cube.
33  usdWriter.OpenInstance( "Cube", -1, unusedIsNew );
34  usdWriter.WriteMesh( cubeMesh );
35  usdWriter.CloseLastInstance();
36 
37  // Add a child prim for the cylinder
38  usdWriter.OpenInstance( "Cylinder", -1, unusedIsNew );
39  usdWriter.WriteMesh( cylinderMesh );
40  usdWriter.CloseLastInstance();
41 
42  // Add a child prim for the wire
43  usdWriter.OpenInstance( "Wire", -1, unusedIsNew );
44  usdWriter.WriteMesh( wireMesh );
45  usdWriter.CloseLastInstance();
46 
47  // Close the root prim — triggers the actual file write
48  usdWriter.CloseLastInstance();
49 
50  return dtkNoError;
51 }
Dtk_transfo
This is the Transformation dedicated class.
Definition: dtk_transfo.hpp:19
sampleWriter::CreateMeshCube
Dtk_MeshPtr CreateMeshCube()
Mesh Cube sample.
Definition: testcreatemesh.cpp:207
Dtk_string
This is a high level string class.
Definition: dtk_string.hpp:53
WriterOptions
Definition: usdw.hpp:20
Dtk_bool
char Dtk_bool
Definition: define.h:724
UsdwThreeMeshes
Dtk_ErrorStatus UsdwThreeMeshes(const Dtk_string &inOutputFileName)
Sample 1: writes a cube and a cylinder mesh in a two-node hierarchy into a USD file.
Definition: testlibusdwrite.cpp:12
sampleWriter::CreateMeshWire
Dtk_MeshPtr CreateMeshWire()
Wire mesh sample.
Definition: testcreatemesh.cpp:389
Dtk_ErrorStatus
Dtk_ErrorStatus
Definition: error_dtk.hpp:6
Dtk_SmartPtr
Definition: util_ptr_dtk.hpp:37
dtkTypeError
Dtk_string dtkTypeError(Dtk_Int32 errNumero)
Dtk_string::c_str
const char * c_str() const
Retrieve the ASCII conversion string.
sampleWriter::CreateMeshCylinder
Dtk_MeshPtr CreateMeshCylinder(int nbpoints)
Mesh Cylinder sample.
Definition: testcreatemesh.cpp:338
Dtk_string::mkdir
int mkdir() const
File Utility : Create a Directory.
Dtk_string::FixPathSeparator
void FixPathSeparator()
File Utility : Fixes path separator consistency. It lets you replace the '\' or '/' by the OS needed ...
UsdwInstances
Dtk_ErrorStatus UsdwInstances(const Dtk_string &inOutputFileName)
Sample 2: writes 10 re-instanced cubes into a USD file, demonstrating USD referencing.
Definition: testlibusdwrite.cpp:59
Dtk_UsdWriter
Definition: usdw.hpp:32
Dtk_transfo::addTranslate
void addTranslate(const Dtk_dir &V)
Translate the Dtk_transfo.
dtkNoError
@ dtkNoError
Definition: error_dtk.hpp:151
Dtk_dir
This is a mathematical direction class.
Definition: dtk_dir.hpp:14