diff --git a/CMakeLists.txt b/CMakeLists.txt index 2686d95e..e65466a0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1130,6 +1130,7 @@ if(TINYUSDZ_BUILD_EXAMPLES) add_subdirectory(examples/tydra_api) add_subdirectory(examples/tydra_to_renderscene) add_subdirectory(examples/api_tutorial) + add_subdirectory(examples/file_format) if (TINYUSDZ_WITH_JSON) add_subdirectory(examples/usd_to_json) endif() diff --git a/data/fileformat_my.usda b/data/fileformat_my.usda new file mode 100644 index 00000000..c53b5b82 --- /dev/null +++ b/data/fileformat_my.usda @@ -0,0 +1,10 @@ +#usda 1.0 + +# example USDA for fileformat example: "my" custom fileformat. +def "muda" ( + references = @bora.my@ + ) +{ + +} + diff --git a/examples/file_format/CMakeLists.txt b/examples/file_format/CMakeLists.txt new file mode 100644 index 00000000..6800be66 --- /dev/null +++ b/examples/file_format/CMakeLists.txt @@ -0,0 +1,15 @@ +# Assume this cmake is called from tinyusdz root(../../) +set(EXAMPLE_TARGET "file_format_example") + +set(TINYUSDZ_API_TUTORIAL_SOURCES + file-format-example.cc + ) + +add_executable(${EXAMPLE_TARGET} ${TINYUSDZ_API_TUTORIAL_SOURCES}) +add_sanitizers(${EXAMPLE_TARGET}) + +target_include_directories(${EXAMPLE_TARGET} PRIVATE ${PROJECT_SOURCE_DIR}/src) +target_link_libraries(${EXAMPLE_TARGET} tinyusdz_static) + + +set_target_properties(${EXAMPLE_TARGET} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}") diff --git a/examples/file_format/file-format-example.cc b/examples/file_format/file-format-example.cc new file mode 100644 index 00000000..b784f031 --- /dev/null +++ b/examples/file_format/file-format-example.cc @@ -0,0 +1,104 @@ +// All-in-one TinyUSDZ core +#include "tinyusdz.hh" + +// Import to_string() and operator<< features +#include +#include "pprinter.hh" +#include "value-pprint.hh" +#include "prim-pprint.hh" + +// Tydra is a collection of APIs to access/convert USD Prim data +// (e.g. Can get Attribute by name) +// See /examples/tydra_api for more Tydra API examples. +#include "tydra/scene-access.hh" + +std::map g_map; + +// +// custom File-format APIs. +// +static bool MyCheck(const tinyusdz::AssetInfo &asset, std::string *warn, std::string *err, void *user_data) { + return true; +} + +static bool MyRead(const tinyusdz::AssetInfo &asset, tinyusdz::PrimSpec &ps/* inout */, std::string *warn, std::string *err, void *user_data) { + std::string key = asset.identifier.GetAssetPath(); + + if (g_map.count(key)) { + float val = g_map[key]; + + tinyusdz::Attribute attr; + attr.set_value(val); + attr.set_name("myval"); + attr.variability() = tinyusdz::Variability::Uniform; + + ps.props()["myval"] = tinyusdz::Property(attr, /* custom */false); + + return true; + + } else { + if (err) { + (*err) += "`" + key + "` not found.\n"; + } + return false; + } +} + +static bool MyWrite(const tinyusdz::AssetInfo &asset, const tinyusdz::PrimSpec &ps, std::string *err, void *user_data) { + // TOOD + return false; +} + + +int main(int argc, char **argv) { + g_map["bora"] = 3.14f; + g_map["dora"] = 6.14f; + + tinyusdz::FileFormatHandler my_handler; + my_handler.extension = "my"; + my_handler.description = "Custom fileformat example."; + my_handler.checker = MyCheck; + my_handler.reader= MyRead; + my_handler.writer = MyWrite; + my_handler.userdata = nullptr; + + tinyusdz::USDLoadOptions options; + options.fileformats["my"] = my_handler; + + tinyusdz::Stage stage; // empty scene + + std::string input_usd_filepath = "../data/fileformat_my.usda"; + if (argc > 1) { + input_usd_filepath = argv[1]; + } + + std::string warn, err; + + tinyusdz::Layer layer; + bool ret = tinyusdz::LoadLayerFromFile(input_usd_filepath, &layer, &warn, &err, options); + + if (warn.size()) { + std::cout << "WARN: " << warn << "\n"; + } + + if (!ret) { + std::cerr << err << "\n"; + exit(-1); + } + + // dummy Asset resolver. + tinyusdz::AssetResolutionResolver resolver; + + // Do `references` composition to materialize `references = @***.my@` + tinyusdz::Layer composited_layer; + if (!tinyusdz::CompositeReferences(resolver, layer, &composited_layer, &warn, &err)) { + std::cerr << "Failed to composite `references`: " << err << "\n"; + return -1; + } + + + // Print USD scene as Ascii. + std::cout << composited_layer << "\n"; + + return EXIT_SUCCESS; +} diff --git a/src/tinyusdz.hh b/src/tinyusdz.hh index 727f3254..6bf96102 100644 --- a/src/tinyusdz.hh +++ b/src/tinyusdz.hh @@ -98,7 +98,7 @@ struct FileFormatHandler FileFormatCheckFunction checker{nullptr}; FileFormatReadFunction reader{nullptr}; FileFormatWriteFunction writer{nullptr}; - void *_userdata{nullptr}; + void *userdata{nullptr}; }; struct USDLoadOptions { @@ -153,7 +153,7 @@ struct USDLoadOptions { /// /// User-defined fileformat hander. - /// key = file(asset) extension. + /// key = file(asset) extension(`.` excluded. example: 'mtlx', 'obj'). /// std::map fileformats;