DATAKIT SDK  V2026.4
USD Write Sample
//start_of_mainusdmeshwritemain
//! \brief Sample 1: writes a cube and a cylinder mesh in a two-node hierarchy into a USD file
Dtk_ErrorStatus UsdwThreeMeshes( const Dtk_string& inOutputFileName )
{ // SAMPLE 1
Dtk_MeshPtr cubeMesh, cylinderMesh, wireMesh;
Dtk_bool unusedIsNew; // not needed for this sample
// Create geometry
cylinderMesh = sampleWriter::CreateMeshCylinder( 30 );
// Initialise the writer; binary file with .usd extension
WriterOptions options;
Dtk_UsdWriter usdWriter( inOutputFileName, err, options );
if( err != dtkNoError )
return err;
// Create the root Xform prim
usdWriter.OpenInstance( "RootNode", -1, unusedIsNew );
// Add a child prim for the cube.
usdWriter.OpenInstance( "Cube", -1, unusedIsNew );
usdWriter.WriteMesh( cubeMesh );
usdWriter.CloseLastInstance();
// Add a child prim for the cylinder
usdWriter.OpenInstance( "Cylinder", -1, unusedIsNew );
usdWriter.WriteMesh( cylinderMesh );
usdWriter.CloseLastInstance();
// Add a child prim for the wire
usdWriter.OpenInstance( "Wire", -1, unusedIsNew );
usdWriter.WriteMesh( wireMesh );
usdWriter.CloseLastInstance();
// Close the root prim — triggers the actual file write
usdWriter.CloseLastInstance();
return dtkNoError;
}
//! \brief 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.
Dtk_ErrorStatus UsdwInstances( const Dtk_string& inOutputFileName )
{ // SAMPLE 2
Dtk_transfo cubeTransfo;
Dtk_bool isNewNode; // true only for the first occurrence of a given protoID
// Initialise the writer
WriterOptions options;
Dtk_UsdWriter usdWriter( inOutputFileName, err, options );
if( err != dtkNoError )
return err;
Dtk_bool unusedIsNew;
usdWriter.OpenInstance( "RootNode", -1, unusedIsNew );
for( int instanceIdx = 0; instanceIdx < 10; ++instanceIdx )
{
// All calls share prototype ID 1 so they reference the same prim
usdWriter.OpenInstance( "Cube", 1, isNewNode, cubeTransfo );
if( isNewNode )
{
usdWriter.WriteMesh( cubeMesh );
}
usdWriter.CloseLastInstance();
// Shift the transform for the next instance along the X axis.
cubeTransfo.addTranslate( Dtk_dir( 10, 0, 0 ) );
}
usdWriter.CloseLastInstance();
return dtkNoError;
}
DtkErrorStatus UsdWriteSample( const Dtk_string& inResultDirectory )
{
Dtk_string outputDirectory, fileName;
std::cout << std::endl << "----------------------------------------------" << std::endl;
// Choose the output directory and create it if necessary
outputDirectory = inResultDirectory + L"Usd/";
outputDirectory.FixPathSeparator();
outputDirectory.mkdir();
std::cout << "Usd Write start" << std::endl;
std::cout << "sample 1" << std::endl;
// Sample 1: simple three-mesh hierarchy.
fileName = outputDirectory + L"threemeshes.usd";
Dtk_ErrorStatus errorStatus = UsdwThreeMeshes( fileName );
if( errorStatus == dtkNoError )
std::cout << "=> " << fileName.c_str() << std::endl;
else
std::cout << "error : " << dtkTypeError( errorStatus ).c_str() << std::endl;
std::cout << "sample 2" << std::endl;
// Sample 2: geometry instancing with USD references
fileName = outputDirectory + L"instances.usd";
errorStatus = UsdwInstances( fileName );
if( errorStatus == dtkNoError )
std::cout << "=> " << fileName.c_str() << std::endl;
else
std::cout << "error : " << dtkTypeError( errorStatus ).c_str() << std::endl;
std::cout << "Usd Write end" << std::endl;
return errorStatus;
}
//end_of_mainusdmeshwritemain
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
UsdWriteSample
DtkErrorStatus UsdWriteSample(const Dtk_string &inResultDirectory)
Definition: testlibusdwrite.cpp:94
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