diff options
Diffstat (limited to 'source/blender/io/usd')
-rw-r--r-- | source/blender/io/usd/intern/usd_capi.cc | 31 | ||||
-rw-r--r-- | source/blender/io/usd/intern/usd_writer_abstract.cc | 14 | ||||
-rw-r--r-- | source/blender/io/usd/intern/usd_writer_abstract.h | 4 | ||||
-rw-r--r-- | source/blender/io/usd/intern/usd_writer_mesh.cc | 27 | ||||
-rw-r--r-- | source/blender/io/usd/tests/usd_stage_creation_test.cc | 23 | ||||
-rw-r--r-- | source/blender/io/usd/usd.h | 1 |
6 files changed, 66 insertions, 34 deletions
diff --git a/source/blender/io/usd/intern/usd_capi.cc b/source/blender/io/usd/intern/usd_capi.cc index 98aef62f38e..52075728e3e 100644 --- a/source/blender/io/usd/intern/usd_capi.cc +++ b/source/blender/io/usd/intern/usd_capi.cc @@ -20,6 +20,7 @@ #include "usd.h" #include "usd_hierarchy_iterator.h" +#include <pxr/base/plug/registry.h> #include <pxr/pxr.h> #include <pxr/usd/usd/stage.h> #include <pxr/usd/usdGeom/tokens.h> @@ -32,6 +33,7 @@ #include "DNA_scene_types.h" +#include "BKE_appdir.h" #include "BKE_blender_version.h" #include "BKE_context.h" #include "BKE_global.h" @@ -59,6 +61,21 @@ struct ExportJobData { bool export_ok; }; +static void ensure_usd_plugin_path_registered(void) +{ + static bool plugin_path_registered = false; + if (plugin_path_registered) { + return; + } + plugin_path_registered = true; + + /* Tell USD which directory to search for its JSON files. If 'datafiles/usd' + * does not exist, the USD library will not be able to read or write any files. */ + const std::string blender_usd_datafiles = BKE_appdir_folder_id(BLENDER_DATAFILES, "usd"); + /* The trailing slash indicates to the USD library that the path is a directory. */ + pxr::PlugRegistry::GetInstance().RegisterPlugins(blender_usd_datafiles + "/"); +} + static void export_startjob(void *customdata, /* Cannot be const, this function implements wm_jobs_start_callback. * NOLINTNEXTLINE: readability-non-const-parameter. */ @@ -75,8 +92,12 @@ static void export_startjob(void *customdata, // Construct the depsgraph for exporting. Scene *scene = DEG_get_input_scene(data->depsgraph); - ViewLayer *view_layer = DEG_get_input_view_layer(data->depsgraph); - DEG_graph_build_from_view_layer(data->depsgraph, data->bmain, scene, view_layer); + if (data->params.visible_objects_only) { + DEG_graph_build_from_view_layer(data->depsgraph); + } + else { + DEG_graph_build_for_all_objects(data->depsgraph); + } BKE_scene_graph_update_tagged(data->depsgraph, data->bmain); *progress = 0.0f; @@ -122,7 +143,7 @@ static void export_startjob(void *customdata, // Update the scene for the next frame to render. scene->r.cfra = static_cast<int>(frame); scene->r.subframe = frame - scene->r.cfra; - BKE_scene_graph_update_for_newframe(data->depsgraph, data->bmain); + BKE_scene_graph_update_for_newframe(data->depsgraph); iter.set_export_frame(frame); iter.iterate_and_write(); @@ -142,7 +163,7 @@ static void export_startjob(void *customdata, // Finish up by going back to the keyframe that was current before we started. if (CFRA != orig_frame) { CFRA = orig_frame; - BKE_scene_graph_update_for_newframe(data->depsgraph, data->bmain); + BKE_scene_graph_update_for_newframe(data->depsgraph); } data->export_ok = true; @@ -176,6 +197,8 @@ bool USD_export(bContext *C, ViewLayer *view_layer = CTX_data_view_layer(C); Scene *scene = CTX_data_scene(C); + blender::io::usd::ensure_usd_plugin_path_registered(); + blender::io::usd::ExportJobData *job = static_cast<blender::io::usd::ExportJobData *>( MEM_mallocN(sizeof(blender::io::usd::ExportJobData), "ExportJobData")); diff --git a/source/blender/io/usd/intern/usd_writer_abstract.cc b/source/blender/io/usd/intern/usd_writer_abstract.cc index a416941fb4d..4910b7f11dd 100644 --- a/source/blender/io/usd/intern/usd_writer_abstract.cc +++ b/source/blender/io/usd/intern/usd_writer_abstract.cc @@ -114,6 +114,20 @@ pxr::UsdShadeMaterial USDAbstractWriter::ensure_usd_material(Material *material) return usd_material; } +void USDAbstractWriter::write_visibility(const HierarchyContext &context, + const pxr::UsdTimeCode timecode, + pxr::UsdGeomImageable &usd_geometry) +{ + pxr::UsdAttribute attr_visibility = usd_geometry.CreateVisibilityAttr(pxr::VtValue(), true); + + const bool is_visible = context.is_object_visible( + usd_export_context_.export_params.evaluation_mode); + const pxr::TfToken visibility = is_visible ? pxr::UsdGeomTokens->inherited : + pxr::UsdGeomTokens->invisible; + + usd_value_writer_.SetAttribute(attr_visibility, pxr::VtValue(visibility), timecode); +} + } // namespace usd } // namespace io } // namespace blender diff --git a/source/blender/io/usd/intern/usd_writer_abstract.h b/source/blender/io/usd/intern/usd_writer_abstract.h index a689deaf0d8..248bdd22a3b 100644 --- a/source/blender/io/usd/intern/usd_writer_abstract.h +++ b/source/blender/io/usd/intern/usd_writer_abstract.h @@ -72,6 +72,10 @@ class USDAbstractWriter : public AbstractHierarchyWriter { pxr::UsdTimeCode get_export_time_code() const; pxr::UsdShadeMaterial ensure_usd_material(Material *material); + + void write_visibility(const HierarchyContext &context, + const pxr::UsdTimeCode timecode, + pxr::UsdGeomImageable &usd_geometry); }; } // namespace usd diff --git a/source/blender/io/usd/intern/usd_writer_mesh.cc b/source/blender/io/usd/intern/usd_writer_mesh.cc index bd2c549e729..75d1ca605d4 100644 --- a/source/blender/io/usd/intern/usd_writer_mesh.cc +++ b/source/blender/io/usd/intern/usd_writer_mesh.cc @@ -42,6 +42,8 @@ #include "DNA_object_fluidsim_types.h" #include "DNA_particle_types.h" +#include <iostream> + namespace blender { namespace io { namespace usd { @@ -52,27 +54,10 @@ USDGenericMeshWriter::USDGenericMeshWriter(const USDExporterContext &ctx) : USDA bool USDGenericMeshWriter::is_supported(const HierarchyContext *context) const { - Object *object = context->object; - bool is_dupli = context->duplicator != nullptr; - int base_flag; - - if (is_dupli) { - /* Construct the object's base flags from its dupli-parent, just like is done in - * deg_objects_dupli_iterator_next(). Without this, the visibility check below will fail. Doing - * this here, instead of a more suitable location in AbstractHierarchyIterator, prevents - * copying the Object for every dupli. */ - base_flag = object->base_flag; - object->base_flag = context->duplicator->base_flag | BASE_FROM_DUPLI; - } - - int visibility = BKE_object_visibility(object, - usd_export_context_.export_params.evaluation_mode); - - if (is_dupli) { - object->base_flag = base_flag; + if (usd_export_context_.export_params.visible_objects_only) { + return context->is_object_visible(usd_export_context_.export_params.evaluation_mode); } - - return (visibility & OB_VISIBLE_SELF) != 0; + return true; } void USDGenericMeshWriter::do_write(HierarchyContext &context) @@ -169,6 +154,8 @@ void USDGenericMeshWriter::write_mesh(HierarchyContext &context, Mesh *mesh) const pxr::SdfPath &usd_path = usd_export_context_.usd_path; pxr::UsdGeomMesh usd_mesh = pxr::UsdGeomMesh::Define(stage, usd_path); + write_visibility(context, timecode, usd_mesh); + USDMeshData usd_mesh_data; get_geometry_data(mesh, usd_mesh_data); diff --git a/source/blender/io/usd/tests/usd_stage_creation_test.cc b/source/blender/io/usd/tests/usd_stage_creation_test.cc index e6bd0bab6ce..3d28d8f8f5a 100644 --- a/source/blender/io/usd/tests/usd_stage_creation_test.cc +++ b/source/blender/io/usd/tests/usd_stage_creation_test.cc @@ -17,6 +17,8 @@ * All rights reserved. */ #include "testing/testing.h" + +#include <pxr/base/plug/registry.h> #include <pxr/usd/usd/stage.h> #include <string> @@ -26,11 +28,6 @@ #include "BKE_appdir.h" -extern "C" { -/* Workaround to make it possible to pass a path at runtime to USD. See creator.c. */ -void usd_initialise_plugin_path(const char *datafiles_usd_path); -} - namespace blender::io::usd { class USDStageCreationTest : public testing::Test { @@ -44,9 +41,16 @@ TEST_F(USDStageCreationTest, JSONFileLoadingTest) } char usd_datafiles_dir[FILE_MAX]; - BLI_path_join(usd_datafiles_dir, FILE_MAX, release_dir.c_str(), "datafiles", "usd", nullptr); + const size_t path_len = BLI_path_join( + usd_datafiles_dir, FILE_MAX, release_dir.c_str(), "datafiles", "usd", nullptr); + + /* BLI_path_join removes trailing slashes, but the USD library requires one in order to recognise + * the path as directory. */ + BLI_assert(path_len + 1 < FILE_MAX); + usd_datafiles_dir[path_len] = '/'; + usd_datafiles_dir[path_len + 1] = '\0'; - usd_initialise_plugin_path(usd_datafiles_dir); + pxr::PlugRegistry::GetInstance().RegisterPlugins(usd_datafiles_dir); /* Simply the ability to create a USD Stage for a specific filename means that the extension * has been recognized by the USD library, and that a USD plugin has been loaded to write such @@ -61,9 +65,8 @@ TEST_F(USDStageCreationTest, JSONFileLoadingTest) unlink(filename.c_str()); } else { - FAIL() << "unable to find suitable USD plugin to write " << filename - << "; re-run with the environment variable PXR_PATH_DEBUG non-empty to see which paths " - "are considered."; + FAIL() << "unable to find suitable USD plugin to write " << filename << "; looked in " + << usd_datafiles_dir; } } diff --git a/source/blender/io/usd/usd.h b/source/blender/io/usd/usd.h index f2826cd1d7c..b9ea90736ff 100644 --- a/source/blender/io/usd/usd.h +++ b/source/blender/io/usd/usd.h @@ -35,6 +35,7 @@ struct USDExportParams { bool export_normals; bool export_materials; bool selected_objects_only; + bool visible_objects_only; bool use_instancing; enum eEvaluationMode evaluation_mode; }; |