diff --git a/source/blender/io/usd/intern/usd_reader_curve.cc b/source/blender/io/usd/intern/usd_reader_curve.cc index 19db0d8fdd6..678d2d22cf5 100644 --- a/source/blender/io/usd/intern/usd_reader_curve.cc +++ b/source/blender/io/usd/intern/usd_reader_curve.cc @@ -244,7 +244,7 @@ void USDCurvesReader::read_curve_sample(Curves *curves_id, const double motionSa if (!usdWidths.empty()) { bke::MutableAttributeAccessor attributes = curves.attributes_for_write(); - bke::SpanAttributeWriter radii = attributes.lookup_or_add_for_write_span( + bke::SpanAttributeWriter radii = attributes.lookup_or_add_for_write_only_span( "radius", bke::AttrDomain::Point); pxr::TfToken widths_interp = curve_prim_.GetWidthsInterpolation(); diff --git a/source/blender/io/usd/intern/usd_reader_mesh.cc b/source/blender/io/usd/intern/usd_reader_mesh.cc index aa376a68607..ee9f0d5aa70 100644 --- a/source/blender/io/usd/intern/usd_reader_mesh.cc +++ b/source/blender/io/usd/intern/usd_reader_mesh.cc @@ -22,6 +22,7 @@ #include "BKE_object.hh" #include "BKE_report.hh" +#include "BLI_array.hh" #include "BLI_map.hh" #include "BLI_math_vector_types.hh" #include "BLI_span.hh" @@ -32,8 +33,6 @@ #include "DNA_object_types.h" #include "DNA_windowmanager_types.h" -#include "MEM_guardedalloc.h" - #include #include #include @@ -436,7 +435,7 @@ void USDMeshReader::read_vertex_creases(Mesh *mesh, const double motionSampleTim } bke::MutableAttributeAccessor attributes = mesh->attributes_for_write(); - bke::SpanAttributeWriter creases = attributes.lookup_or_add_for_write_span( + bke::SpanAttributeWriter creases = attributes.lookup_or_add_for_write_only_span( "crease_vert", bke::AttrDomain::Point); for (size_t i = 0; i < corner_indices.size(); i++) { @@ -452,28 +451,23 @@ void USDMeshReader::read_velocities(Mesh *mesh, const double motionSampleTime) if (!velocities.empty()) { bke::MutableAttributeAccessor attributes = mesh->attributes_for_write(); - bke::GSpanAttributeWriter attribute = attributes.lookup_or_add_for_write_span( - "velocity", bke::AttrDomain::Point, CD_PROP_FLOAT3); + bke::SpanAttributeWriter velocity = + attributes.lookup_or_add_for_write_only_span("velocity", bke::AttrDomain::Point); Span usd_data(velocities.data(), velocities.size()); - attribute.span.typed().copy_from(usd_data.cast()); - - attribute.finish(); + velocity.span.copy_from(usd_data.cast()); + velocity.finish(); } } void USDMeshReader::process_normals_vertex_varying(Mesh *mesh) { - if (!mesh) { - return; - } - if (normals_.empty()) { return; } if (normals_.size() != mesh->verts_num) { - CLOG_WARN(&LOG, "Vertex varying normals count mismatch for mesh %s", prim_path_.c_str()); + CLOG_WARN(&LOG, "Vertex varying normals count mismatch for mesh '%s'", prim_path_.c_str()); return; } @@ -490,20 +484,17 @@ void USDMeshReader::process_normals_face_varying(Mesh *mesh) const /* Check for normals count mismatches to prevent crashes. */ if (normals_.size() != mesh->corners_num) { - CLOG_WARN(&LOG, "Loop normal count mismatch for mesh %s", mesh->id.name); + CLOG_WARN(&LOG, "Loop normal count mismatch for mesh '%s'", prim_path_.c_str()); return; } - long int loop_count = normals_.size(); - - float(*lnors)[3] = static_cast( - MEM_malloc_arrayN(loop_count, sizeof(float[3]), "USD::FaceNormals")); + Array corner_normals(mesh->corners_num); const OffsetIndices faces = mesh->faces(); for (const int i : faces.index_range()) { const IndexRange face = faces[i]; for (int j : face.index_range()) { - int blender_index = face.start() + j; + const int corner = face.start() + j; int usd_index = face.start(); if (is_left_handed_) { @@ -513,14 +504,11 @@ void USDMeshReader::process_normals_face_varying(Mesh *mesh) const usd_index += j; } - lnors[blender_index][0] = normals_[usd_index][0]; - lnors[blender_index][1] = normals_[usd_index][1]; - lnors[blender_index][2] = normals_[usd_index][2]; + corner_normals[corner] = detail::convert_value(normals_[usd_index]); } } - BKE_mesh_set_custom_normals(mesh, lnors); - MEM_freeN(lnors); + BKE_mesh_set_custom_normals(mesh, reinterpret_cast(corner_normals.data())); } void USDMeshReader::process_normals_uniform(Mesh *mesh) const @@ -531,25 +519,20 @@ void USDMeshReader::process_normals_uniform(Mesh *mesh) const /* Check for normals count mismatches to prevent crashes. */ if (normals_.size() != mesh->faces_num) { - CLOG_WARN(&LOG, "Uniform normal count mismatch for mesh %s", mesh->id.name); + CLOG_WARN(&LOG, "Uniform normal count mismatch for mesh '%s'", prim_path_.c_str()); return; } - float(*lnors)[3] = static_cast( - MEM_malloc_arrayN(mesh->corners_num, sizeof(float[3]), "USD::FaceNormals")); + Array corner_normals(mesh->corners_num); const OffsetIndices faces = mesh->faces(); for (const int i : faces.index_range()) { for (const int corner : faces[i]) { - lnors[corner][0] = normals_[i][0]; - lnors[corner][1] = normals_[i][1]; - lnors[corner][2] = normals_[i][2]; + corner_normals[corner] = detail::convert_value(normals_[i]); } } - BKE_mesh_set_custom_normals(mesh, lnors); - - MEM_freeN(lnors); + BKE_mesh_set_custom_normals(mesh, reinterpret_cast(corner_normals.data())); } void USDMeshReader::read_mesh_sample(ImportSettings *settings, @@ -563,9 +546,7 @@ void USDMeshReader::read_mesh_sample(ImportSettings *settings, if (new_mesh || (settings->read_flag & MOD_MESHSEQ_READ_VERT) != 0) { MutableSpan vert_positions = mesh->vert_positions_for_write(); - for (int i = 0; i < positions_.size(); i++) { - vert_positions[i] = {positions_[i][0], positions_[i][1], positions_[i][2]}; - } + vert_positions.copy_from(Span(positions_.data(), positions_.size()).cast()); mesh->tag_positions_changed(); read_vertex_creases(mesh, motionSampleTime); diff --git a/source/blender/io/usd/intern/usd_reader_pointinstancer.cc b/source/blender/io/usd/intern/usd_reader_pointinstancer.cc index d81e5942424..fe5f9f5f6f0 100644 --- a/source/blender/io/usd/intern/usd_reader_pointinstancer.cc +++ b/source/blender/io/usd/intern/usd_reader_pointinstancer.cc @@ -88,11 +88,8 @@ void USDPointInstancerReader::read_object_data(Main *bmain, const double motionS PointCloud *point_cloud = BKE_pointcloud_new_nomain(positions.size()); - MutableSpan positions_span = point_cloud->positions_for_write(); - - for (int i = 0; i < positions.size(); ++i) { - positions_span[i] = float3(positions[i][0], positions[i][1], positions[i][2]); - } + MutableSpan point_positions = point_cloud->positions_for_write(); + point_positions.copy_from(Span(positions.data(), positions.size()).cast()); bke::MutableAttributeAccessor attributes = point_cloud->attributes_for_write(); diff --git a/source/blender/io/usd/intern/usd_reader_points.cc b/source/blender/io/usd/intern/usd_reader_points.cc index 4a465efd64d..13c192eeaf3 100644 --- a/source/blender/io/usd/intern/usd_reader_points.cc +++ b/source/blender/io/usd/intern/usd_reader_points.cc @@ -102,7 +102,7 @@ void USDPointsReader::read_geometry(bke::GeometrySet &geometry_set, if (!usd_widths.empty()) { bke::MutableAttributeAccessor attributes = point_cloud->attributes_for_write(); - bke::SpanAttributeWriter radii = attributes.lookup_or_add_for_write_span( + bke::SpanAttributeWriter radii = attributes.lookup_or_add_for_write_only_span( "radius", bke::AttrDomain::Point); const pxr::TfToken widths_interp = points_prim_.GetWidthsInterpolation(); @@ -135,12 +135,12 @@ void USDPointsReader::read_velocities(PointCloud *point_cloud, const double moti if (!velocities.empty()) { bke::MutableAttributeAccessor attributes = point_cloud->attributes_for_write(); - bke::GSpanAttributeWriter attribute = attributes.lookup_or_add_for_write_span( - "velocity", bke::AttrDomain::Point, CD_PROP_FLOAT3); + bke::SpanAttributeWriter velocity = + attributes.lookup_or_add_for_write_only_span("velocity", bke::AttrDomain::Point); Span usd_data(velocities.data(), velocities.size()); - attribute.span.typed().copy_from(usd_data.cast()); - attribute.finish(); + velocity.span.copy_from(usd_data.cast()); + velocity.finish(); } } diff --git a/source/blender/io/usd/intern/usd_reader_shape.cc b/source/blender/io/usd/intern/usd_reader_shape.cc index 77078aeb242..bf44c35ddcb 100644 --- a/source/blender/io/usd/intern/usd_reader_shape.cc +++ b/source/blender/io/usd/intern/usd_reader_shape.cc @@ -265,12 +265,7 @@ Mesh *USDShapeReader::mesh_from_prim(Mesh *existing_mesh, } MutableSpan vert_positions = active_mesh->vert_positions_for_write(); - - for (int i = 0; i < positions.size(); i++) { - vert_positions[i][0] = positions[i][0]; - vert_positions[i][1] = positions[i][1]; - vert_positions[i][2] = positions[i][2]; - } + vert_positions.copy_from(Span(positions.data(), positions.size()).cast()); if (params.read_flags & MOD_MESHSEQ_READ_COLOR) { if (active_mesh != existing_mesh) {