From e5b3b9b9cb9ada73b6431faea82f490f09f7e2a2 Mon Sep 17 00:00:00 2001 From: Jesse Yurkovich Date: Fri, 3 Jan 2025 20:36:07 +0100 Subject: [PATCH] Cleanup: USD: various non-functional changes - Pass large std::function by const ref - Shift the mesh material assignment helpers inside main namespace - Use MaterialFaceGroups type alias in more places - Remove unused bmain argument from import_mesh_skel_bindings Pull Request: https://projects.blender.org/blender/blender/pulls/132586 --- source/blender/io/usd/intern/usd_reader_mesh.cc | 16 +++++++--------- source/blender/io/usd/intern/usd_reader_stage.cc | 3 +-- source/blender/io/usd/intern/usd_reader_stage.hh | 2 +- source/blender/io/usd/intern/usd_skel_convert.cc | 7 ++----- source/blender/io/usd/intern/usd_skel_convert.hh | 9 ++------- source/blender/io/usd/intern/usd_writer_mesh.cc | 5 +++-- source/blender/io/usd/intern/usd_writer_mesh.hh | 6 +++--- 7 files changed, 19 insertions(+), 29 deletions(-) diff --git a/source/blender/io/usd/intern/usd_reader_mesh.cc b/source/blender/io/usd/intern/usd_reader_mesh.cc index 45f9c9b3e20..9a7b89ce38e 100644 --- a/source/blender/io/usd/intern/usd_reader_mesh.cc +++ b/source/blender/io/usd/intern/usd_reader_mesh.cc @@ -9,7 +9,6 @@ #include "usd.hh" #include "usd_attribute_utils.hh" #include "usd_hash_types.hh" -#include "usd_hook.hh" #include "usd_mesh_utils.hh" #include "usd_reader_material.hh" #include "usd_skel_convert.hh" @@ -59,8 +58,10 @@ static const pxr::TfToken UVMap("UVMap", pxr::TfToken::Immortal); static const pxr::TfToken normalsPrimvar("normals", pxr::TfToken::Immortal); } // namespace usdtokens +namespace blender::io::usd { + namespace utils { -using namespace blender::io::usd; + static pxr::UsdShadeMaterial compute_bound_material(const pxr::UsdPrim &prim, eUSDMtlPurpose mtl_purpose) { @@ -93,11 +94,10 @@ static pxr::UsdShadeMaterial compute_bound_material(const pxr::UsdPrim &prim, static void assign_materials(Main *bmain, Object *ob, const blender::Map &mat_index_map, - const blender::io::usd::USDImportParams ¶ms, + const USDImportParams ¶ms, pxr::UsdStageRefPtr stage, - const blender::io::usd::ImportSettings &settings) + const ImportSettings &settings) { - using namespace blender::io::usd; if (!(stage && bmain && ob)) { return; } @@ -109,7 +109,7 @@ static void assign_materials(Main *bmain, USDMaterialReader mat_reader(params, bmain); for (const auto item : mat_index_map.items()) { - Material *assigned_mat = blender::io::usd::find_existing_material( + Material *assigned_mat = find_existing_material( item.key, params, settings.mat_name_to_mat, settings.usd_path_to_mat_name); if (!assigned_mat) { /* Blender material doesn't exist, so create it now. */ @@ -170,8 +170,6 @@ static void assign_materials(Main *bmain, } // namespace utils -namespace blender::io::usd { - void USDMeshReader::create_object(Main *bmain, const double /*motionSampleTime*/) { Mesh *mesh = BKE_mesh_add(bmain, name_.c_str()); @@ -222,7 +220,7 @@ void USDMeshReader::read_object_data(Main *bmain, const double motionSampleTime) } if (import_params_.import_skeletons) { - import_mesh_skel_bindings(bmain, object_, prim_, reports()); + import_mesh_skel_bindings(object_, prim_, reports()); } USDXformReader::read_object_data(bmain, motionSampleTime); diff --git a/source/blender/io/usd/intern/usd_reader_stage.cc b/source/blender/io/usd/intern/usd_reader_stage.cc index 63813046df0..6c501b09255 100644 --- a/source/blender/io/usd/intern/usd_reader_stage.cc +++ b/source/blender/io/usd/intern/usd_reader_stage.cc @@ -21,7 +21,6 @@ #include "usd_reader_xform.hh" #include "usd_utils.hh" -#include #include #include #include @@ -180,7 +179,7 @@ static void find_prefix_to_skip(pxr::UsdStageRefPtr stage, ImportSettings &setti USDStageReader::USDStageReader(pxr::UsdStageRefPtr stage, const USDImportParams ¶ms, - std::function get_cache_file_fn) + const std::function &get_cache_file_fn) : stage_(stage), params_(params) { convert_to_z_up(stage_, settings_); diff --git a/source/blender/io/usd/intern/usd_reader_stage.hh b/source/blender/io/usd/intern/usd_reader_stage.hh index a87158b58bd..e16baa50e4e 100644 --- a/source/blender/io/usd/intern/usd_reader_stage.hh +++ b/source/blender/io/usd/intern/usd_reader_stage.hh @@ -59,7 +59,7 @@ class USDStageReader { public: USDStageReader(pxr::UsdStageRefPtr stage, const USDImportParams ¶ms, - std::function get_cache_file_fn = {}); + const std::function &get_cache_file_fn = {}); ~USDStageReader(); diff --git a/source/blender/io/usd/intern/usd_skel_convert.cc b/source/blender/io/usd/intern/usd_skel_convert.cc index db04c2565d5..dfa54ed4147 100644 --- a/source/blender/io/usd/intern/usd_skel_convert.cc +++ b/source/blender/io/usd/intern/usd_skel_convert.cc @@ -964,12 +964,9 @@ void import_skeleton(Main *bmain, } } -void import_mesh_skel_bindings(Main *bmain, - Object *mesh_obj, - const pxr::UsdPrim &prim, - ReportList *reports) +void import_mesh_skel_bindings(Object *mesh_obj, const pxr::UsdPrim &prim, ReportList *reports) { - if (!(bmain && mesh_obj && mesh_obj->type == OB_MESH && prim)) { + if (!(mesh_obj && mesh_obj->type == OB_MESH && prim)) { return; } diff --git a/source/blender/io/usd/intern/usd_skel_convert.hh b/source/blender/io/usd/intern/usd_skel_convert.hh index 21cbbcef7d0..66d28e9cd11 100644 --- a/source/blender/io/usd/intern/usd_skel_convert.hh +++ b/source/blender/io/usd/intern/usd_skel_convert.hh @@ -4,7 +4,6 @@ #pragma once #include "BLI_map.hh" -#include "BLI_vector.hh" #include #include @@ -70,16 +69,12 @@ void import_skeleton(Main *bmain, * modifier on the given mesh object. If the USD prim does not have a skeleton * binding defined, this function is a no-op. * - * \param bmain: Main pointer - * \param obj: Mesh object to which an armature modifier will be added + * \param mesh_obj: Mesh object to which an armature modifier will be added * \param prim: The USD primitive from which skinning data will be imported * \param reports: the storage for potential warning or error reports (generated using BKE_report * API). */ -void import_mesh_skel_bindings(Main *bmain, - Object *mesh_obj, - const pxr::UsdPrim &prim, - ReportList *reports); +void import_mesh_skel_bindings(Object *mesh_obj, const pxr::UsdPrim &prim, ReportList *reports); /** * Map an object to its USD prim export path. diff --git a/source/blender/io/usd/intern/usd_writer_mesh.cc b/source/blender/io/usd/intern/usd_writer_mesh.cc index f6616ac4d50..27053352281 100644 --- a/source/blender/io/usd/intern/usd_writer_mesh.cc +++ b/source/blender/io/usd/intern/usd_writer_mesh.cc @@ -34,6 +34,7 @@ #include "DEG_depsgraph.hh" #include "DNA_key_types.h" +#include "DNA_material_types.h" #include "DNA_modifier_types.h" #include "CLG_log.h" @@ -286,7 +287,7 @@ struct USDMeshData { pxr::VtArray points; pxr::VtIntArray face_vertex_counts; pxr::VtIntArray face_indices; - Map face_groups; + MaterialFaceGroups face_groups; /* The length of this array specifies the number of creases on the surface. Each element gives * the number of (must be adjacent) vertices in each crease, whose indices are linearly laid out @@ -664,7 +665,7 @@ void USDGenericMeshWriter::assign_materials(const HierarchyContext &context, auto subset_material_api = pxr::UsdShadeMaterialBindingAPI(subset_prim); subset_material_api.Bind(usd_material); /* Apply the #MaterialBindingAPI applied schema, as required by USD. */ - subset_material_api.Apply(subset_prim); + pxr::UsdShadeMaterialBindingAPI::Apply(subset_prim); } } diff --git a/source/blender/io/usd/intern/usd_writer_mesh.hh b/source/blender/io/usd/intern/usd_writer_mesh.hh index 872096e952a..0165f74c495 100644 --- a/source/blender/io/usd/intern/usd_writer_mesh.hh +++ b/source/blender/io/usd/intern/usd_writer_mesh.hh @@ -19,6 +19,9 @@ namespace blender::io::usd { struct USDMeshData; +/* Mapping from material slot number to array of face indices with that material. */ +using MaterialFaceGroups = Map>; + /* Writer for USD geometry. Does not assume the object is a mesh object. */ class USDGenericMeshWriter : public USDAbstractWriter { public: @@ -32,9 +35,6 @@ class USDGenericMeshWriter : public USDAbstractWriter { virtual void free_export_mesh(Mesh *mesh); private: - /* Mapping from material slot number to array of face indices with that material. */ - using MaterialFaceGroups = Map; - void write_mesh(HierarchyContext &context, Mesh *mesh, const SubsurfModifierData *subsurfData); pxr::TfToken get_subdiv_scheme(const SubsurfModifierData *subsurfData); void write_subdiv(const pxr::TfToken &subdiv_scheme,