Blender V5.3
usd_reader_particlefield.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
7#include "IO_gsplat.hh"
8#include "IO_validate.hh"
9
10#include "BLI_span.hh"
11#include "BLI_task.hh"
12
13#include "BKE_geometry_set.hh"
14#include "BKE_main.hh"
15#include "BKE_object.hh"
16#include "BKE_pointcloud.hh"
17
18#include "DNA_object_types.h"
20
21#include "CLG_log.h"
22
23namespace blender::io::usd {
24
25static CLG_LogRef LOG = {"io.usd"};
26
27/*
28 * The UsdVolParticleField3DGaussianSplat schema allows for attributes to be stored as either float
29 * or half on a per-attribute basis. The following wrapper classes allow callers to read the data
30 * without needing to know the underlying type being used.
31 */
32
33template<typename UsdFloatT, typename UsdHalfT> struct UsdWrappedData {
34 UsdFloatT usd_float;
35 UsdHalfT usd_half;
36 bool uses_float = false;
37
38 template<typename Fn> UsdWrappedData(Fn uses_float_fn, pxr::UsdTimeCode time)
39 {
40 pxr::UsdAttribute usd_attr;
41 if (uses_float_fn(&usd_attr)) {
42 usd_attr.Get(&usd_float, time);
43 uses_float = true;
44 }
45 else {
46 usd_attr.Get(&usd_half, time);
47 uses_float = false;
48 }
49 }
50
51 size_t size()
52 {
53 return uses_float ? usd_float.size() : usd_half.size();
54 }
55};
56
57struct UsdWrappedFloat : UsdWrappedData<pxr::VtFloatArray, pxr::VtHalfArray> {
58 template<typename Fn>
59 UsdWrappedFloat(Fn uses_float_fn, pxr::UsdTimeCode time) : UsdWrappedData(uses_float_fn, time)
60 {
61 }
62
63 float operator[](size_t i) const
64 {
65 if (uses_float) {
66 return usd_float.AsConst()[i];
67 }
68
69 return usd_half.AsConst()[i];
70 }
71};
72
73struct UsdWrappedVec3 : UsdWrappedData<pxr::VtVec3fArray, pxr::VtVec3hArray> {
74 template<typename Fn>
75 UsdWrappedVec3(Fn uses_float_fn, pxr::UsdTimeCode time) : UsdWrappedData(uses_float_fn, time)
76 {
77 }
78
79 float3 operator[](size_t i) const
80 {
81 if (uses_float) {
82 const pxr::GfVec3f vec = usd_float.AsConst()[i];
83 return float3(vec[0], vec[1], vec[2]);
84 }
85
86 const pxr::GfVec3h vec = usd_half.AsConst()[i];
87 return float3(vec[0], vec[1], vec[2]);
88 }
89};
90
91struct UsdWrappedQuat : UsdWrappedData<pxr::VtQuatfArray, pxr::VtQuathArray> {
92 template<typename Fn>
93 UsdWrappedQuat(Fn uses_float_fn, pxr::UsdTimeCode time) : UsdWrappedData(uses_float_fn, time)
94 {
95 }
96
98 {
99 if (uses_float) {
100 const pxr::GfQuatf quat = usd_float.AsConst()[i];
101 return math::Quaternion(
102 quat.GetReal(), quat.GetImaginary()[0], quat.GetImaginary()[1], quat.GetImaginary()[2]);
103 }
104
105 const pxr::GfQuath quat = usd_half.AsConst()[i];
106 return math::Quaternion(
107 quat.GetReal(), quat.GetImaginary()[0], quat.GetImaginary()[1], quat.GetImaginary()[2]);
108 }
109};
110
112{
113 PointCloud *point_cloud = BKE_pointcloud_add(bmain, name_.c_str());
115 object_->data = id_cast<ID *>(point_cloud);
116}
117
118void USDParticleFieldReader::read_object_data(Main *bmain, pxr::UsdTimeCode time)
119{
120 const USDMeshReadParams params = create_mesh_read_params(time.GetValue(),
122
123 PointCloud *point_cloud = id_cast<PointCloud *>(object_->data);
124
127
128 read_geometry(geometry_set, params, nullptr);
129
130 PointCloud *read_point_cloud =
131 geometry_set.get_component_for_write<bke::PointCloudComponent>().release();
132
133 if (read_point_cloud != point_cloud) {
134 BKE_pointcloud_nomain_to_pointcloud(read_point_cloud, point_cloud);
135 }
136
137 if (is_animated()) {
139 }
140
142}
143
145 USDMeshReadParams params,
146 const char ** /*r_err_str*/)
147{
148 UsdWrappedVec3 usd_positions(
149 [&](pxr::UsdAttribute *attr) { return gsplat_prim_.UsesFloatPositions(attr); },
150 params.motion_sample_time);
151
152 UsdWrappedVec3 usd_scales(
153 [&](pxr::UsdAttribute *attr) { return gsplat_prim_.UsesFloatScales(attr); },
154 params.motion_sample_time);
155
156 UsdWrappedFloat usd_opacities(
157 [&](pxr::UsdAttribute *attr) { return gsplat_prim_.UsesFloatOpacities(attr); },
158 params.motion_sample_time);
159
160 UsdWrappedQuat usd_orientations(
161 [&](pxr::UsdAttribute *attr) { return gsplat_prim_.UsesFloatOrientations(attr); },
162 params.motion_sample_time);
163
164 UsdWrappedVec3 usd_coeffs(
165 [&](pxr::UsdAttribute *attr) { return gsplat_prim_.UsesFloatRadianceCoefficients(attr); },
166 params.motion_sample_time);
167
168 int usd_degree = 0;
169 gsplat_prim_.GetRadianceSphericalHarmonicsDegreeAttr().Get(&usd_degree,
170 params.motion_sample_time);
171
172 if (!validate::size_fits_in_int(usd_positions.size())) {
173 CLOG_WARN(&LOG,
174 "ParticleField '%s' has an unsupported number of points.",
175 this->prim_path().GetAsString().c_str());
176 return;
177 }
178
179 if (usd_degree < 0 || usd_degree > 4) {
180 CLOG_WARN(&LOG,
181 "ParticleField '%s' has an unsupported spherical harmonics degree.",
182 this->prim_path().GetAsString().c_str());
183 return;
184 }
185
186 if (usd_scales.size() != usd_positions.size() || usd_opacities.size() != usd_positions.size() ||
187 usd_orientations.size() != usd_positions.size())
188 {
189 CLOG_WARN(&LOG,
190 "ParticleField '%s' has incorrect data sizes.",
191 this->prim_path().GetAsString().c_str());
192 return;
193 }
194
195 const int usd_coeff_element_size = (usd_degree + 1) * (usd_degree + 1);
196 if (usd_coeffs.size() != usd_positions.size() * usd_coeff_element_size) {
197 CLOG_WARN(&LOG,
198 "ParticleField '%s' has an incorrect amount of spherical harmonics data.",
199 this->prim_path().GetAsString().c_str());
200 return;
201 }
202
203 PointCloud *point_cloud = geometry_set.get_pointcloud_for_write();
204 if (point_cloud->totpoint != usd_positions.size()) {
205 point_cloud = BKE_pointcloud_new_nomain(PointCloudType::GSplat, usd_positions.size());
206 }
207
208 MutableSpan<float3> positions = point_cloud->positions_for_write();
209 for (int64_t i = 0; i < positions.size(); i++) {
210 positions[i] = usd_positions[i];
211 }
212
213 gsplat::GsplatMutableAttributeAccessor accessor(*point_cloud, usd_degree);
214 MutableSpan<float4> radiance = accessor.radiance_base_for_write();
217 Span<MutableSpan<float3>> sh_attrs = accessor.sh_for_write();
218
219 const int num_sh_dimensions = usd_coeff_element_size - 1;
220 threading::parallel_for(positions.index_range(), 16384, [&](IndexRange range) {
221 for (const int64_t i : range) {
222 const int64_t sh_offset = (i * usd_coeff_element_size);
223
224 radiance[i] = float4(usd_coeffs[sh_offset], usd_opacities[i]);
225 scale[i] = usd_scales[i];
226 rotation[i] = math::normalize(usd_orientations[i]);
227
228 for (int dimension = 0; dimension < num_sh_dimensions; dimension++) {
229 sh_attrs[dimension][i] = usd_coeffs[sh_offset + dimension + 1];
230 }
231 }
232 });
233
234 accessor.finish();
235
236 geometry_set.replace_pointcloud(point_cloud);
237}
238
239bool USDParticleFieldReader::is_animated() const
240{
241 auto check_time_varying = [](auto &&get_attr_fn) {
242 pxr::UsdAttribute usd_attr;
243 get_attr_fn(&usd_attr);
244 return usd_attr.ValueMightBeTimeVarying();
245 };
246
247 bool is_animated = false;
248 is_animated |= check_time_varying(
249 [&](pxr::UsdAttribute *attr) { gsplat_prim_.UsesFloatPositions(attr); });
250 is_animated |= check_time_varying(
251 [&](pxr::UsdAttribute *attr) { gsplat_prim_.UsesFloatScales(attr); });
252 is_animated |= check_time_varying(
253 [&](pxr::UsdAttribute *attr) { gsplat_prim_.UsesFloatOpacities(attr); });
254 is_animated |= check_time_varying(
255 [&](pxr::UsdAttribute *attr) { gsplat_prim_.UsesFloatOrientations(attr); });
256 is_animated |= check_time_varying(
257 [&](pxr::UsdAttribute *attr) { gsplat_prim_.UsesFloatRadianceCoefficients(attr); });
258
259 return is_animated;
260}
261
262} // namespace blender::io::usd
General operations, lookup, etc. for blender objects.
General operations for point clouds.
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
Object is a sort of wrapper for general info.
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
constexpr const char * c_str() const
MutableSpan< math::Quaternion > rotations_for_write()
Definition gsplat.cc:48
Span< MutableSpan< float3 > > sh_for_write()
Definition gsplat.cc:53
void read_object_data(Main *bmain, pxr::UsdTimeCode time) override
void read_geometry(bke::GeometrySet &geometry_set, USDMeshReadParams params, const char **r_err_str) override
const USDImportParams & import_params_
void read_object_data(Main *bmain, pxr::UsdTimeCode time) override
float3x3 scale(float3x3 mat, float2 scale)
USDMeshReadParams create_mesh_read_params(const double motion_sample_time, const int read_flags)
bool size_fits_in_int(const int64_t size)
QuaternionBase< float > Quaternion
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
__int64 int64_t
Definition BKE_text.h:90
void BKE_pointcloud_nomain_to_pointcloud(PointCloud *pointcloud_src, PointCloud *pointcloud_dst)
PointCloud * BKE_pointcloud_add(Main *bmain, const char *name)
static CLG_LogRef LOG
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()
static GeometrySet from_pointcloud(PointCloud *pointcloud, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
MeshSeqCacheModifierReadFlag mesh_read_flag
Definition usd.hh:192
UsdWrappedData(Fn uses_float_fn, pxr::UsdTimeCode time)
UsdWrappedFloat(Fn uses_float_fn, pxr::UsdTimeCode time)
UsdWrappedQuat(Fn uses_float_fn, pxr::UsdTimeCode time)
math::Quaternion operator[](size_t i) const
UsdWrappedVec3(Fn uses_float_fn, pxr::UsdTimeCode time)