From b030bc4671d662491c05ab42a603cb00fe8aa948 Mon Sep 17 00:00:00 2001 From: makowalski Date: Fri, 23 Jul 2021 21:25:21 -0400 Subject: USD import: use pointers for out parameters. Changed return parameter types from references to pointers, to improve code readability, per suggestion from Sybren. --- source/blender/io/usd/intern/usd_capi_import.cc | 20 +++++--- .../blender/io/usd/intern/usd_reader_material.cc | 58 ++++++++++++---------- source/blender/io/usd/intern/usd_reader_material.h | 8 +-- source/blender/io/usd/intern/usd_reader_mesh.cc | 30 +++++++---- source/blender/io/usd/intern/usd_reader_mesh.h | 2 +- source/blender/io/usd/intern/usd_reader_xform.cc | 13 +++-- source/blender/io/usd/intern/usd_reader_xform.h | 2 +- 7 files changed, 77 insertions(+), 56 deletions(-) diff --git a/source/blender/io/usd/intern/usd_capi_import.cc b/source/blender/io/usd/intern/usd_capi_import.cc index 7e6259459d3..8255fca284c 100644 --- a/source/blender/io/usd/intern/usd_capi_import.cc +++ b/source/blender/io/usd/intern/usd_capi_import.cc @@ -103,21 +103,25 @@ static bool gather_objects_paths(const pxr::UsdPrim &object, ListBase *object_pa /* Update the given import settings with the global rotation matrix to orient * imported objects with Z-up, if necessary */ -static void convert_to_z_up(pxr::UsdStageRefPtr stage, ImportSettings &r_settings) +static void convert_to_z_up(pxr::UsdStageRefPtr stage, ImportSettings *r_settings) { if (!stage || pxr::UsdGeomGetStageUpAxis(stage) == pxr::UsdGeomTokens->z) { return; } - r_settings.do_convert_mat = true; + if (!r_settings) { + return; + } + + r_settings->do_convert_mat = true; /* Rotate 90 degrees about the X-axis. */ float rmat[3][3]; float axis[3] = {1.0f, 0.0f, 0.0f}; axis_angle_normalized_to_mat3(rmat, axis, M_PI / 2.0f); - unit_m4(r_settings.conversion_mat); - copy_m4_m3(r_settings.conversion_mat, rmat); + unit_m4(r_settings->conversion_mat); + copy_m4_m3(r_settings->conversion_mat, rmat); } enum { @@ -211,7 +215,7 @@ static void import_startjob(void *customdata, short *stop, short *do_update, flo return; } - convert_to_z_up(stage, data->settings); + convert_to_z_up(stage, &data->settings); /* Set up the stage for animated data. */ if (data->params.set_frame_range) { @@ -524,7 +528,7 @@ CacheArchiveHandle *USD_create_handle(struct Main * /*bmain*/, USDImportParams params{}; blender::io::usd::ImportSettings settings{}; - convert_to_z_up(stage, settings); + convert_to_z_up(stage, &settings); USDStageReader *stage_reader = new USDStageReader(stage, params, settings); @@ -560,7 +564,7 @@ void USD_get_transform(struct CacheReader *reader, Object *object = usd_reader->object(); if (object->parent == nullptr) { /* No parent, so local space is the same as world space. */ - usd_reader->read_matrix(r_mat_world, time, scale, is_constant); + usd_reader->read_matrix(r_mat_world, time, scale, &is_constant); return; } @@ -568,7 +572,7 @@ void USD_get_transform(struct CacheReader *reader, BKE_object_get_parent_matrix(object, object->parent, mat_parent); float mat_local[4][4]; - usd_reader->read_matrix(mat_local, time, scale, is_constant); + usd_reader->read_matrix(mat_local, time, scale, &is_constant); mul_m4_m4m4(r_mat_world, mat_parent, object->parentinv); mul_m4_m4m4(r_mat_world, r_mat_world, mat_local); } diff --git a/source/blender/io/usd/intern/usd_reader_material.cc b/source/blender/io/usd/intern/usd_reader_material.cc index cfa068f75b0..02ed7c35e57 100644 --- a/source/blender/io/usd/intern/usd_reader_material.cc +++ b/source/blender/io/usd/intern/usd_reader_material.cc @@ -245,19 +245,23 @@ namespace { /* Compute the x- and y-coordinates for placing a new node in an unoccupied region of * the column with the given index. Returns the coordinates in r_locx and r_locy and * updates the column-occupancy information in r_ctx. */ -void compute_node_loc(const int column, float &r_locx, float &r_locy, NodePlacementContext &r_ctx) +void compute_node_loc(const int column, float *r_locx, float *r_locy, NodePlacementContext *r_ctx) { - r_locx = r_ctx.origx - column * r_ctx.horizontal_step; + if (!(r_locx && r_locy && r_ctx)) { + return; + } + + (*r_locx) = r_ctx->origx - column * r_ctx->horizontal_step; - if (column >= r_ctx.column_offsets.size()) { - r_ctx.column_offsets.push_back(0.0f); + if (column >= r_ctx->column_offsets.size()) { + r_ctx->column_offsets.push_back(0.0f); } - r_locy = r_ctx.origy - r_ctx.column_offsets[column]; + (*r_locy) = r_ctx->origy - r_ctx->column_offsets[column]; /* Record the y-offset of the occupied region in * the column, including padding. */ - r_ctx.column_offsets[column] += r_ctx.vertical_step + 10.0f; + r_ctx->column_offsets[column] += r_ctx->vertical_step + 10.0f; } } // End anonymous namespace. @@ -369,46 +373,46 @@ void USDMaterialReader::set_principled_node_inputs(bNode *principled, /* Recursively set the principled shader inputs. */ if (pxr::UsdShadeInput diffuse_input = usd_shader.GetInput(usdtokens::diffuseColor)) { - set_node_input(diffuse_input, principled, "Base Color", ntree, column, context); + set_node_input(diffuse_input, principled, "Base Color", ntree, column, &context); } if (pxr::UsdShadeInput emissive_input = usd_shader.GetInput(usdtokens::emissiveColor)) { - set_node_input(emissive_input, principled, "Emission", ntree, column, context); + set_node_input(emissive_input, principled, "Emission", ntree, column, &context); } if (pxr::UsdShadeInput specular_input = usd_shader.GetInput(usdtokens::specularColor)) { - set_node_input(specular_input, principled, "Specular", ntree, column, context); + set_node_input(specular_input, principled, "Specular", ntree, column, &context); } if (pxr::UsdShadeInput metallic_input = usd_shader.GetInput(usdtokens::metallic)) { ; - set_node_input(metallic_input, principled, "Metallic", ntree, column, context); + set_node_input(metallic_input, principled, "Metallic", ntree, column, &context); } if (pxr::UsdShadeInput roughness_input = usd_shader.GetInput(usdtokens::roughness)) { - set_node_input(roughness_input, principled, "Roughness", ntree, column, context); + set_node_input(roughness_input, principled, "Roughness", ntree, column, &context); } if (pxr::UsdShadeInput clearcoat_input = usd_shader.GetInput(usdtokens::clearcoat)) { - set_node_input(clearcoat_input, principled, "Clearcoat", ntree, column, context); + set_node_input(clearcoat_input, principled, "Clearcoat", ntree, column, &context); } if (pxr::UsdShadeInput clearcoat_roughness_input = usd_shader.GetInput( usdtokens::clearcoatRoughness)) { set_node_input( - clearcoat_roughness_input, principled, "Clearcoat Roughness", ntree, column, context); + clearcoat_roughness_input, principled, "Clearcoat Roughness", ntree, column, &context); } if (pxr::UsdShadeInput opacity_input = usd_shader.GetInput(usdtokens::opacity)) { - set_node_input(opacity_input, principled, "Alpha", ntree, column, context); + set_node_input(opacity_input, principled, "Alpha", ntree, column, &context); } if (pxr::UsdShadeInput ior_input = usd_shader.GetInput(usdtokens::ior)) { - set_node_input(ior_input, principled, "IOR", ntree, column, context); + set_node_input(ior_input, principled, "IOR", ntree, column, &context); } if (pxr::UsdShadeInput normal_input = usd_shader.GetInput(usdtokens::normal)) { - set_node_input(normal_input, principled, "Normal", ntree, column, context); + set_node_input(normal_input, principled, "Normal", ntree, column, &context); } } @@ -418,9 +422,9 @@ void USDMaterialReader::set_node_input(const pxr::UsdShadeInput &usd_input, const char *dest_socket_name, bNodeTree *ntree, const int column, - NodePlacementContext &r_ctx) const + NodePlacementContext *r_ctx) const { - if (!(usd_input && dest_node)) { + if (!(usd_input && dest_node && r_ctx)) { return; } @@ -487,9 +491,9 @@ void USDMaterialReader::follow_connection(const pxr::UsdShadeInput &usd_input, const char *dest_socket_name, bNodeTree *ntree, int column, - NodePlacementContext &r_ctx) const + NodePlacementContext *r_ctx) const { - if (!(usd_input && dest_node && dest_socket_name && ntree)) { + if (!(usd_input && dest_node && dest_socket_name && ntree && r_ctx)) { return; } @@ -524,7 +528,7 @@ void USDMaterialReader::follow_connection(const pxr::UsdShadeInput &usd_input, /* The normal texture input requires creating a normal map node. */ float locx = 0.0f; float locy = 0.0f; - compute_node_loc(column + 1, locx, locy, r_ctx); + compute_node_loc(column + 1, &locx, &locy, r_ctx); bNode *normal_map = add_node(nullptr, ntree, SH_NODE_NORMAL_MAP, locx, locy); @@ -555,15 +559,15 @@ void USDMaterialReader::convert_usd_uv_texture(const pxr::UsdShadeShader &usd_sh const char *dest_socket_name, bNodeTree *ntree, const int column, - NodePlacementContext &r_ctx) const + NodePlacementContext *r_ctx) const { - if (!usd_shader || !dest_node || !ntree || !dest_socket_name || !bmain_) { + if (!usd_shader || !dest_node || !ntree || !dest_socket_name || !bmain_ || !r_ctx) { return; } float locx = 0.0f; float locy = 0.0f; - compute_node_loc(column, locx, locy, r_ctx); + compute_node_loc(column, &locx, &locy, r_ctx); /* Create the Texture Image node. */ bNode *tex_image = add_node(nullptr, ntree, SH_NODE_TEX_IMAGE, locx, locy); @@ -660,15 +664,15 @@ void USDMaterialReader::convert_usd_primvar_reader_float2( const char *dest_socket_name, bNodeTree *ntree, const int column, - NodePlacementContext &r_ctx) const + NodePlacementContext *r_ctx) const { - if (!usd_shader || !dest_node || !ntree || !dest_socket_name || !bmain_) { + if (!usd_shader || !dest_node || !ntree || !dest_socket_name || !bmain_ || !r_ctx) { return; } float locx = 0.0f; float locy = 0.0f; - compute_node_loc(column, locx, locy, r_ctx); + compute_node_loc(column, &locx, &locy, r_ctx); /* Create the UV Map node. */ bNode *uv_map = add_node(nullptr, ntree, SH_NODE_UVMAP, locx, locy); diff --git a/source/blender/io/usd/intern/usd_reader_material.h b/source/blender/io/usd/intern/usd_reader_material.h index 5e9353ba72e..3e8fc675931 100644 --- a/source/blender/io/usd/intern/usd_reader_material.h +++ b/source/blender/io/usd/intern/usd_reader_material.h @@ -100,14 +100,14 @@ class USDMaterialReader { const char *dest_socket_name, bNodeTree *ntree, int column, - NodePlacementContext &r_ctx) const; + NodePlacementContext *r_ctx) const; void follow_connection(const pxr::UsdShadeInput &usd_input, bNode *dest_node, const char *dest_socket_name, bNodeTree *ntree, int column, - NodePlacementContext &r_ctx) const; + NodePlacementContext *r_ctx) const; void convert_usd_uv_texture(const pxr::UsdShadeShader &usd_shader, const pxr::TfToken &usd_source_name, @@ -115,7 +115,7 @@ class USDMaterialReader { const char *dest_socket_name, bNodeTree *ntree, int column, - NodePlacementContext &r_ctx) const; + NodePlacementContext *r_ctx) const; void load_tex_image(const pxr::UsdShadeShader &usd_shader, bNode *tex_image) const; @@ -125,7 +125,7 @@ class USDMaterialReader { const char *dest_socket_name, bNodeTree *ntree, int column, - NodePlacementContext &r_ctx) const; + NodePlacementContext *r_ctx) const; }; } // namespace blender::io::usd diff --git a/source/blender/io/usd/intern/usd_reader_mesh.cc b/source/blender/io/usd/intern/usd_reader_mesh.cc index ce2572179e6..c1e0152a808 100644 --- a/source/blender/io/usd/intern/usd_reader_mesh.cc +++ b/source/blender/io/usd/intern/usd_reader_mesh.cc @@ -57,13 +57,17 @@ static const pxr::TfToken normalsPrimvar("normals", pxr::TfToken::Immortal); namespace utils { /* Very similar to abc mesh utils. */ -static void build_mat_map(const Main *bmain, std::map &mat_map) +static void build_mat_map(const Main *bmain, std::map *r_mat_map) { + if (r_mat_map == nullptr) { + return; + } + Material *material = static_cast(bmain->materials.first); for (; material; material = static_cast(material->id.next)) { /* We have to do this because the stored material name is coming directly from usd. */ - mat_map[pxr::TfMakeValidIdentifier(material->id.name + 2)] = material; + (*r_mat_map)[pxr::TfMakeValidIdentifier(material->id.name + 2)] = material; } } @@ -94,7 +98,7 @@ static void assign_materials(Main *bmain, /* TODO(kevin): use global map? */ std::map mat_map; - build_mat_map(bmain, mat_map); + build_mat_map(bmain, &mat_map); blender::io::usd::USDMaterialReader mat_reader(params, bmain); @@ -676,8 +680,12 @@ void USDMeshReader::read_mesh_sample(ImportSettings *settings, void USDMeshReader::assign_facesets_to_mpoly(double motionSampleTime, MPoly *mpoly, const int /* totpoly */, - std::map &r_mat_map) + std::map *r_mat_map) { + if (r_mat_map == nullptr) { + return; + } + /* Find the geom subsets that have bound materials. * We don't call pxr::UsdShadeMaterialBindingAPI::GetMaterialBindSubsets() * because this function returns only those subsets that are in the 'materialBind' @@ -705,11 +713,11 @@ void USDMeshReader::assign_facesets_to_mpoly(double motionSampleTime, continue; } - if (r_mat_map.find(subset_mtl_path) == r_mat_map.end()) { - r_mat_map[subset_mtl_path] = 1 + current_mat++; + if (r_mat_map->find(subset_mtl_path) == r_mat_map->end()) { + (*r_mat_map)[subset_mtl_path] = 1 + current_mat++; } - const int mat_idx = r_mat_map[subset_mtl_path] - 1; + const int mat_idx = (*r_mat_map)[subset_mtl_path] - 1; pxr::UsdAttribute indicesAttribute = subset.GetIndicesAttr(); pxr::VtIntArray indices; @@ -722,7 +730,7 @@ void USDMeshReader::assign_facesets_to_mpoly(double motionSampleTime, } } - if (r_mat_map.empty()) { + if (r_mat_map->empty()) { pxr::UsdShadeMaterialBindingAPI api = pxr::UsdShadeMaterialBindingAPI(prim_); if (pxr::UsdShadeMaterial mtl = api.ComputeBoundMaterial()) { @@ -730,7 +738,7 @@ void USDMeshReader::assign_facesets_to_mpoly(double motionSampleTime, pxr::SdfPath mtl_path = mtl.GetPath(); if (!mtl_path.IsEmpty()) { - r_mat_map.insert(std::make_pair(mtl.GetPath(), 1)); + r_mat_map->insert(std::make_pair(mtl.GetPath(), 1)); } } } @@ -743,7 +751,7 @@ void USDMeshReader::readFaceSetsSample(Main *bmain, Mesh *mesh, const double mot } std::map mat_map; - assign_facesets_to_mpoly(motionSampleTime, mesh->mpoly, mesh->totpoly, mat_map); + assign_facesets_to_mpoly(motionSampleTime, mesh->mpoly, mesh->totpoly, &mat_map); utils::assign_materials(bmain, object_, mat_map, this->import_params_, this->prim_.GetStage()); } @@ -836,7 +844,7 @@ Mesh *USDMeshReader::read_mesh(Mesh *existing_mesh, size_t num_polys = active_mesh->totpoly; if (num_polys > 0 && import_params_.import_materials) { std::map mat_map; - assign_facesets_to_mpoly(motionSampleTime, active_mesh->mpoly, num_polys, mat_map); + assign_facesets_to_mpoly(motionSampleTime, active_mesh->mpoly, num_polys, &mat_map); } } diff --git a/source/blender/io/usd/intern/usd_reader_mesh.h b/source/blender/io/usd/intern/usd_reader_mesh.h index c95c4198fb5..71c0860454c 100644 --- a/source/blender/io/usd/intern/usd_reader_mesh.h +++ b/source/blender/io/usd/intern/usd_reader_mesh.h @@ -76,7 +76,7 @@ class USDMeshReader : public USDGeomReader { void assign_facesets_to_mpoly(double motionSampleTime, struct MPoly *mpoly, int totpoly, - std::map &r_mat_map); + std::map *r_mat_map); void read_mpolys(Mesh *mesh); void read_uvs(Mesh *mesh, double motionSampleTime, bool load_uvs = false); diff --git a/source/blender/io/usd/intern/usd_reader_xform.cc b/source/blender/io/usd/intern/usd_reader_xform.cc index 2041349931c..a7acffe6401 100644 --- a/source/blender/io/usd/intern/usd_reader_xform.cc +++ b/source/blender/io/usd/intern/usd_reader_xform.cc @@ -51,7 +51,7 @@ void USDXformReader::read_object_data(Main * /* bmain */, const double motionSam bool is_constant; float transform_from_usd[4][4]; - read_matrix(transform_from_usd, motionSampleTime, import_params_.scale, is_constant); + read_matrix(transform_from_usd, motionSampleTime, import_params_.scale, &is_constant); if (!is_constant) { bConstraint *con = BKE_constraint_add_for_object( @@ -73,9 +73,12 @@ void USDXformReader::read_object_data(Main * /* bmain */, const double motionSam void USDXformReader::read_matrix(float r_mat[4][4] /* local matrix */, const float time, const float scale, - bool &is_constant) + bool *r_is_constant) { - is_constant = true; + if (r_is_constant) { + *r_is_constant = true; + } + unit_m4(r_mat); pxr::UsdGeomXformable xformable; @@ -92,7 +95,9 @@ void USDXformReader::read_matrix(float r_mat[4][4] /* local matrix */, return; } - is_constant = !xformable.TransformMightBeTimeVarying(); + if (r_is_constant) { + *r_is_constant = !xformable.TransformMightBeTimeVarying(); + } pxr::GfMatrix4d usd_local_xf; bool reset_xform_stack; diff --git a/source/blender/io/usd/intern/usd_reader_xform.h b/source/blender/io/usd/intern/usd_reader_xform.h index fefe1f85a09..bdb483c408e 100644 --- a/source/blender/io/usd/intern/usd_reader_xform.h +++ b/source/blender/io/usd/intern/usd_reader_xform.h @@ -41,7 +41,7 @@ class USDXformReader : public USDPrimReader { void create_object(Main *bmain, double motionSampleTime) override; void read_object_data(Main *bmain, double motionSampleTime) override; - void read_matrix(float r_mat[4][4], const float time, const float scale, bool &is_constant); + void read_matrix(float r_mat[4][4], const float time, const float scale, bool *r_is_constant); bool use_parent_xform() const { -- cgit v1.2.3