Blender V5.3
usd_reader_points.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
7
8#include "BKE_geometry_set.hh"
9#include "BKE_object.hh"
10#include "BKE_pointcloud.hh"
11
12#include "BLI_span.hh"
13
14#include "DNA_object_types.h"
16
17#include <pxr/usd/usdGeom/primvar.h>
18#include <pxr/usd/usdGeom/primvarsAPI.h>
19
20namespace blender::io::usd {
21
23{
24 PointCloud *pointcloud = BKE_pointcloud_add(bmain, name_.c_str());
26 object_->data = id_cast<ID *>(pointcloud);
27}
28
29void USDPointsReader::read_object_data(Main *bmain, pxr::UsdTimeCode time)
30{
31 const USDMeshReadParams params = create_mesh_read_params(time.GetValue(),
33
34 PointCloud *pointcloud = id_cast<PointCloud *>(object_->data);
35
38
39 read_geometry(geometry_set, params, nullptr);
40
41 PointCloud *read_pointcloud =
42 geometry_set.get_component_for_write<bke::PointCloudComponent>().release();
43
44 if (read_pointcloud != pointcloud) {
45 BKE_pointcloud_nomain_to_pointcloud(read_pointcloud, pointcloud);
46 }
47
48 if (is_animated()) {
49 /* If the point cloud has animated positions or attributes, we add the cache
50 * modifier. */
52 }
53
54 /* Update the transform. */
56}
57
59 USDMeshReadParams params,
60 const char ** /*r_err_str*/)
61{
62 PointCloud *pointcloud = geometry_set.get_pointcloud_for_write();
63
64 /* Get the existing point cloud data. */
65 pxr::VtVec3fArray usd_positions;
66 points_prim_.GetPointsAttr().Get(&usd_positions, params.motion_sample_time);
67
68 if (pointcloud->totpoint != usd_positions.size()) {
69 /* Size changed so we must reallocate. */
70 pointcloud = BKE_pointcloud_new_nomain(PointCloudType::Points, usd_positions.size());
71 }
72
73 /* Update point positions and radii */
74 static_assert(sizeof(pxr::GfVec3f) == sizeof(float3));
75 MutableSpan<float3> positions = pointcloud->positions_for_write();
76 positions.copy_from(Span(usd_positions.cdata(), usd_positions.size()).cast<float3>());
77
78 pxr::VtFloatArray usd_widths;
79 points_prim_.GetWidthsAttr().Get(&usd_widths, params.motion_sample_time);
80
81 if (!usd_widths.empty()) {
82 Span<float> widths = Span(usd_widths.cdata(), usd_widths.size());
83
84 const pxr::TfToken widths_interp = points_prim_.GetWidthsInterpolation();
85 if (widths_interp == pxr::UsdGeomTokens->constant) {
86 set_single_value(pointcloud->attributes_for_write(),
87 "radius",
90 bke::AttributeInitValue(widths[0] / 2.0f));
91 }
92 else {
93 MutableSpan<float> radii = pointcloud->radius_for_write();
94 for (const int i_point : IndexRange(std::min(radii.size(), widths.size()))) {
95 radii[i_point] = widths[i_point] / 2.0f;
96 }
97 }
98 }
99
100 /* TODO: Once Blender supports custom normals for points, we can consider reading in normals.
101 * See UsdGeomPointBased::GetNormalsAttr */
102
103 /* Read in IDs, velocity, and generic data. */
104 this->read_ids(pointcloud, params.motion_sample_time);
105 this->read_velocities(pointcloud, params.motion_sample_time);
106 this->read_custom_data(pointcloud, params.motion_sample_time);
107
108 geometry_set.replace_pointcloud(pointcloud);
109}
110
111void USDPointsReader::read_ids(PointCloud *pointcloud, const pxr::UsdTimeCode time) const
112{
113 pxr::VtInt64Array usd_ids;
114 points_prim_.GetIdsAttr().Get(&usd_ids, time);
115
116 if (!usd_ids.empty()) {
117 bke::MutableAttributeAccessor attributes = pointcloud->attributes_for_write();
120
121 const Span<int64_t> usd_data(usd_ids.cdata(), usd_ids.size());
122 for (const int i_point : IndexRange(std::min(ids.span.size(), usd_data.size()))) {
123 /* Blender only supports int ID attributes so we have to narrow the value. */
124 ids.span[i_point] = int(usd_ids[i_point]);
125 }
126
127 ids.finish();
128 }
129}
130
131void USDPointsReader::read_velocities(PointCloud *pointcloud, const pxr::UsdTimeCode time) const
132{
133 pxr::VtVec3fArray velocities;
134 points_prim_.GetVelocitiesAttr().Get(&velocities, time);
135
136 if (!velocities.empty()) {
137 bke::MutableAttributeAccessor attributes = pointcloud->attributes_for_write();
140
141 Span<pxr::GfVec3f> usd_data(velocities.cdata(), velocities.size());
142 velocity.span.copy_from(usd_data.cast<float3>());
143 velocity.finish();
144 }
145}
146
147void USDPointsReader::read_custom_data(PointCloud *pointcloud, const pxr::UsdTimeCode time) const
148{
149 pxr::UsdGeomPrimvarsAPI pv_api(points_prim_);
150
151 std::vector<pxr::UsdGeomPrimvar> primvars = pv_api.GetPrimvarsWithValues();
152 for (const pxr::UsdGeomPrimvar &pv : primvars) {
153 const pxr::SdfValueTypeName pv_type = pv.GetTypeName();
154 if (!pv_type.IsArray()) {
155 continue; /* Skip non-array primvar attributes. */
156 }
157
159 const std::optional<bke::AttrType> type = convert_usd_type_to_blender(pv_type);
160 if (!type.has_value()) {
161 return;
162 }
163
164 bke::MutableAttributeAccessor attributes = pointcloud->attributes_for_write();
165 copy_primvar_to_blender_attribute(pv, time, *type, domain, {}, attributes);
166 }
167}
168
170{
171 if (!points_prim_) {
172 return false;
173 }
174
175 bool is_animated = false;
176
177 is_animated |= points_prim_.GetPointsAttr().ValueMightBeTimeVarying();
178
179 is_animated |= points_prim_.GetVelocitiesAttr().ValueMightBeTimeVarying();
180
181 is_animated |= points_prim_.GetWidthsAttr().ValueMightBeTimeVarying();
182
183 pxr::UsdGeomPrimvarsAPI pv_api(points_prim_);
184 std::vector<pxr::UsdGeomPrimvar> primvars = pv_api.GetPrimvarsWithValues();
185 for (const pxr::UsdGeomPrimvar &pv : primvars) {
186 is_animated |= pv.ValueMightBeTimeVarying();
187 }
188
189 return is_animated;
190}
191
192} // namespace blender::io::usd
General operations, lookup, etc. for blender objects.
General operations for point clouds.
Object is a sort of wrapper for general info.
constexpr IndexRange()=default
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr void copy_from(Span< T > values) const
Definition BLI_span.hh:745
constexpr Span()=default
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr const char * c_str() const
GSpanAttributeWriter lookup_or_add_for_write_only_span(StringRef name, AttrDomain domain, AttrType data_type)
void read_velocities(PointCloud *pointcloud, const pxr::UsdTimeCode time) const
void create_object(Main *bmain) override
void read_geometry(bke::GeometrySet &geometry_set, USDMeshReadParams params, const char **r_err_str) override
void read_custom_data(PointCloud *pointcloud, const pxr::UsdTimeCode time) const
void read_ids(PointCloud *pointcloud, const pxr::UsdTimeCode time) const
void read_object_data(Main *bmain, pxr::UsdTimeCode time) override
const USDImportParams & import_params_
void read_object_data(Main *bmain, pxr::UsdTimeCode time) override
void set_single_value(bke::MutableAttributeAccessor attributes, const StringRef attr_name, const bke::AttrDomain domain, const bke::AttrType data_type, const bke::AttributeInit &value)
USDMeshReadParams create_mesh_read_params(const double motion_sample_time, const int read_flags)
void copy_primvar_to_blender_attribute(const pxr::UsdGeomPrimvar &primvar, const pxr::UsdTimeCode time, const bke::AttrType data_type, const bke::AttrDomain domain, const OffsetIndices< int > face_indices, bke::MutableAttributeAccessor attributes)
std::optional< bke::AttrType > convert_usd_type_to_blender(const pxr::SdfValueTypeName usd_type)
void BKE_pointcloud_nomain_to_pointcloud(PointCloud *pointcloud_src, PointCloud *pointcloud_dst)
PointCloud * BKE_pointcloud_add(Main *bmain, const char *name)
PointCloud * BKE_pointcloud_new_nomain(PointCloudType type, int totpoint)
VecBase< float, 3 > float3
Object * BKE_object_add_only_object(Main *bmain, ObjectType type, const char *name) ATTR_RETURNS_NONNULL
Definition object.cc:2265
GeometryComponent & get_component_for_write(GeometryComponent::Type component_type)
PointCloud * get_pointcloud_for_write()
void replace_pointcloud(PointCloud *pointcloud, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
static GeometrySet from_pointcloud(PointCloud *pointcloud, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
MeshSeqCacheModifierReadFlag mesh_read_flag
Definition usd.hh:192