Blender V5.3
usd_reader_pointinstancer.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
7#include "BKE_attribute.hh"
8#include "BKE_geometry_set.hh"
9#include "BKE_modifier.hh"
10#include "BKE_node.hh"
12#include "BKE_node_runtime.hh"
14#include "BKE_object.hh"
15#include "BKE_pointcloud.hh"
16#include "BLI_listbase.hh"
18#include "BLI_string.hh"
19
21#include "DNA_node_types.h"
22
23#include <pxr/usd/usdGeom/pointInstancer.h>
24
25namespace blender::io::usd {
26
30static bNode *add_input_named_attrib_node(bNodeTree *ntree, const char *name, int8_t prop_type)
31{
33 auto *storage = reinterpret_cast<NodeGeometryInputNamedAttribute *>(node->storage);
34 storage->data_type = prop_type;
35
36 bNodeSocket *socket = bke::node_find_socket(*node, SOCK_IN, "Name"_ustr);
37 bNodeSocketValueString *str_value = static_cast<bNodeSocketValueString *>(socket->default_value);
38 BLI_strncpy(str_value->value, name, MAX_NAME);
39 return node;
40}
41
43{
44 PointCloud *pointcloud = BKE_pointcloud_add(bmain, name_.c_str());
46 this->object_->data = id_cast<ID *>(pointcloud);
47}
48
50 USDMeshReadParams params,
51 const char ** /*r_err_str*/)
52{
53 pxr::VtArray<pxr::GfVec3f> usd_positions;
54 pxr::VtArray<pxr::GfVec3f> usd_scales;
55 pxr::VtArray<pxr::GfQuath> usd_orientations;
56 pxr::VtInt64Array usd_ids;
57 pxr::VtArray<int> usd_proto_indices;
58 std::vector<bool> usd_mask = point_instancer_prim_.ComputeMaskAtTime(params.motion_sample_time);
59
60 point_instancer_prim_.GetPositionsAttr().Get(&usd_positions, params.motion_sample_time);
61 point_instancer_prim_.GetScalesAttr().Get(&usd_scales, params.motion_sample_time);
62 point_instancer_prim_.GetOrientationsAttr().Get(&usd_orientations, params.motion_sample_time);
63 point_instancer_prim_.GetIdsAttr().Get(&usd_ids, params.motion_sample_time);
64 point_instancer_prim_.GetProtoIndicesAttr().Get(&usd_proto_indices, params.motion_sample_time);
65
66 PointCloud *pointcloud = geometry_set.get_pointcloud_for_write();
67 if (pointcloud->totpoint != usd_positions.size()) {
68 /* Size changed so we must reallocate. */
69 pointcloud = BKE_pointcloud_new_nomain(PointCloudType::Points, usd_positions.size());
70 }
71
72 MutableSpan<float3> point_positions = pointcloud->positions_for_write();
73 point_positions.copy_from(Span(usd_positions.cdata(), usd_positions.size()).cast<float3>());
74
75 bke::MutableAttributeAccessor attributes = pointcloud->attributes_for_write();
76
77 bke::SpanAttributeWriter<float3> scales_attribute =
79
80 /* Here and below, handle the case where instancing attributes are empty or
81 * not of the expected size. */
82 if (usd_scales.size() < usd_positions.size()) {
83 scales_attribute.span.fill(float3(1.0f));
84 }
85
86 Span<pxr::GfVec3f> scales = Span(usd_scales.cdata(), usd_scales.size());
87 for (const int i : IndexRange(std::min(usd_scales.size(), usd_positions.size()))) {
88 scales_attribute.span[i] = float3(scales[i][0], scales[i][1], scales[i][2]);
89 }
90
91 scales_attribute.finish();
92
93 bke::SpanAttributeWriter<math::Quaternion> orientations_attribute =
96
97 if (usd_orientations.size() < usd_positions.size()) {
98 orientations_attribute.span.fill(math::Quaternion::identity());
99 }
100
101 Span<pxr::GfQuath> orientations = Span(usd_orientations.cdata(), usd_orientations.size());
102 for (const int i : IndexRange(std::min(usd_orientations.size(), usd_positions.size()))) {
103 orientations_attribute.span[i] = math::Quaternion(orientations[i].GetReal(),
104 orientations[i].GetImaginary()[0],
105 orientations[i].GetImaginary()[1],
106 orientations[i].GetImaginary()[2]);
107 }
108
109 orientations_attribute.finish();
110
111 if (!usd_ids.empty()) {
112 bke::SpanAttributeWriter<int> ids_attribute =
114
115 const Span<int64_t> usd_data(usd_ids.cdata(), usd_ids.size());
116 if (usd_data.size() < ids_attribute.span.size()) {
117 ids_attribute.span.fill(0);
118 }
119
120 for (const int i : IndexRange(std::min(usd_data.size(), ids_attribute.span.size()))) {
121 /* Blender only supports int ID attributes so we have to narrow the value. */
122 ids_attribute.span[i] = int(usd_data[i]);
123 }
124
125 ids_attribute.finish();
126 }
127
128 bke::SpanAttributeWriter<int> proto_indices_attribute =
129 attributes.lookup_or_add_for_write_only_span<int>("proto_index", bke::AttrDomain::Point);
130
131 if (usd_proto_indices.size() < usd_positions.size()) {
132 proto_indices_attribute.span.fill(0);
133 }
134
135 Span<int> proto_indices = Span(usd_proto_indices.cdata(), usd_proto_indices.size());
136 for (const int i : IndexRange(std::min(usd_proto_indices.size(), usd_positions.size()))) {
137 proto_indices_attribute.span[i] = proto_indices[i];
138 }
139
140 proto_indices_attribute.finish();
141
142 bke::SpanAttributeWriter<bool> mask_attribute =
144
145 if (usd_mask.size() < usd_positions.size()) {
146 mask_attribute.span.fill(true);
147 }
148
149 for (const int i : IndexRange(std::min(usd_mask.size(), usd_positions.size()))) {
150 mask_attribute.span[i] = usd_mask[i];
151 }
152
153 mask_attribute.finish();
154
155 geometry_set.replace_pointcloud(pointcloud);
156}
157
158void USDPointInstancerReader::read_object_data(Main *bmain, const pxr::UsdTimeCode time)
159{
160 PointCloud *pointcloud = id_cast<PointCloud *>(object_->data);
161
164
165 const USDMeshReadParams params = create_mesh_read_params(time.GetValue(),
167
168 read_geometry(geometry_set, params, nullptr);
169
170 PointCloud *read_pointcloud =
171 geometry_set.get_component_for_write<bke::PointCloudComponent>().release();
172
173 if (read_pointcloud != pointcloud) {
174 BKE_pointcloud_nomain_to_pointcloud(read_pointcloud, pointcloud);
175 }
176
177 if (is_animated()) {
178 /* If the point cloud has time-varying data, we add the cache modifier. */
180 }
181
185
186 NodesModifierData &nmd = *reinterpret_cast<NodesModifierData *>(md);
187 nmd.node_group = bke::node_tree_add_tree(bmain, "Instances", "GeometryNodeTree");
188
189 bNodeTree *ntree = nmd.node_group;
190
191 ntree->tree_interface.add_socket(
192 "Geometry", "", "NodeSocketGeometry", NODE_INTERFACE_SOCKET_OUTPUT, nullptr);
193 ntree->tree_interface.add_socket(
194 "Geometry", "", "NodeSocketGeometry", NODE_INTERFACE_SOCKET_INPUT, nullptr);
195 bNode *group_input = bke::node_add_static_node(nullptr, *ntree, NODE_GROUP_INPUT);
196 group_input->location[0] = -400.0f;
197 bNode *group_output = bke::node_add_static_node(nullptr, *ntree, NODE_GROUP_OUTPUT);
198 group_output->location[0] = 500.0f;
199 group_output->flag |= NODE_DO_OUTPUT;
200
201 bNode *instance_on_points_node = bke::node_add_static_node(
202 nullptr, *ntree, GEO_NODE_INSTANCE_ON_POINTS);
203 instance_on_points_node->location[0] = 300.0f;
205 *instance_on_points_node, SOCK_IN, "Pick Instance"_ustr);
206 socket->default_value_typed<bNodeSocketValueBoolean>()->value = true;
207
208 bNode *mask_attrib_node = add_input_named_attrib_node(ntree, "mask", CD_PROP_BOOL);
209 mask_attrib_node->location[0] = 100.0f;
210 mask_attrib_node->location[1] = -100.0f;
211
212 bNode *collection_info_node = bke::node_add_static_node(
213 nullptr, *ntree, GEO_NODE_COLLECTION_INFO);
214 collection_info_node->location[0] = 100.0f;
215 collection_info_node->location[1] = -300.0f;
216 socket = bke::node_find_socket(*collection_info_node, SOCK_IN, "Separate Children"_ustr);
217 socket->default_value_typed<bNodeSocketValueBoolean>()->value = true;
218
219 bNode *indices_attrib_node = add_input_named_attrib_node(ntree, "proto_index", CD_PROP_INT32);
220 indices_attrib_node->location[0] = 100.0f;
221 indices_attrib_node->location[1] = -500.0f;
222
223 bNode *rotation_attrib_node = add_input_named_attrib_node(
224 ntree, "orientation", CD_PROP_QUATERNION);
225 rotation_attrib_node->location[0] = 100.0f;
226 rotation_attrib_node->location[1] = -700.0f;
227
228 bNode *scale_attrib_node = add_input_named_attrib_node(ntree, "scale", CD_PROP_FLOAT3);
229 scale_attrib_node->location[0] = 100.0f;
230 scale_attrib_node->location[1] = -900.0f;
231
232 bke::node_add_link(*ntree,
233 *group_input,
234 *group_input->outputs.first(),
235 *instance_on_points_node,
236 *bke::node_find_socket(*instance_on_points_node, SOCK_IN, "Points"_ustr));
237
238 bke::node_add_link(*ntree,
239 *mask_attrib_node,
240 *bke::node_find_socket(*mask_attrib_node, SOCK_OUT, "Attribute"_ustr),
241 *instance_on_points_node,
242 *bke::node_find_socket(*instance_on_points_node, SOCK_IN, "Selection"_ustr));
243
245 *ntree,
246 *indices_attrib_node,
247 *bke::node_find_socket(*indices_attrib_node, SOCK_OUT, "Attribute"_ustr),
248 *instance_on_points_node,
249 *bke::node_find_socket(*instance_on_points_node, SOCK_IN, "Instance Index"_ustr));
250
251 bke::node_add_link(*ntree,
252 *scale_attrib_node,
253 *bke::node_find_socket(*scale_attrib_node, SOCK_OUT, "Attribute"_ustr),
254 *instance_on_points_node,
255 *bke::node_find_socket(*instance_on_points_node, SOCK_IN, "Scale"_ustr));
256
257 bke::node_add_link(*ntree,
258 *rotation_attrib_node,
259 *bke::node_find_socket(*rotation_attrib_node, SOCK_OUT, "Attribute"_ustr),
260 *instance_on_points_node,
261 *bke::node_find_socket(*instance_on_points_node, SOCK_IN, "Rotation"_ustr));
262
263 bke::node_add_link(*ntree,
264 *collection_info_node,
265 *bke::node_find_socket(*collection_info_node, SOCK_OUT, "Instances"_ustr),
266 *instance_on_points_node,
267 *bke::node_find_socket(*instance_on_points_node, SOCK_IN, "Instance"_ustr));
268
269 bke::node_add_link(*ntree,
270 *instance_on_points_node,
271 *bke::node_find_socket(*instance_on_points_node, SOCK_OUT, "Instances"_ustr),
272 *group_output,
273 *group_output->inputs.first());
274
276
278
280}
281
282pxr::SdfPathVector USDPointInstancerReader::proto_paths() const
283{
284 pxr::SdfPathVector paths;
285 point_instancer_prim_.GetPrototypesRel().GetTargets(&paths);
286
287 return paths;
288}
289
291{
292 /* create_object() should have been called already. */
294
296 if (!md) {
298 return;
299 }
300
301 NodesModifierData *nmd = reinterpret_cast<NodesModifierData *>(md);
302
303 bNodeTree *ntree = nmd->node_group;
304 if (!ntree) {
306 return;
307 }
308
309 bNode *collection_node = bke::node_find_node_by_name(*ntree, "Collection Info");
310 if (!collection_node) {
312 return;
313 }
314
315 bNodeSocket *sock = bke::node_find_socket(*collection_node, SOCK_IN, "Collection"_ustr);
316 if (!sock) {
318 return;
319 }
320
321 bNodeSocketValueCollection *socket_data = static_cast<bNodeSocketValueCollection *>(
322 sock->default_value);
323
324 if (socket_data->value != &coll) {
325 socket_data->value = &coll;
328 }
329}
330
332{
333 bool is_animated = false;
334 is_animated |= point_instancer_prim_.GetPositionsAttr().ValueMightBeTimeVarying();
335 is_animated |= point_instancer_prim_.GetScalesAttr().ValueMightBeTimeVarying();
336 is_animated |= point_instancer_prim_.GetOrientationsAttr().ValueMightBeTimeVarying();
337 is_animated |= point_instancer_prim_.GetProtoIndicesAttr().ValueMightBeTimeVarying();
338
339 return is_animated;
340}
341
342} // namespace blender::io::usd
#define NODE_GROUP_OUTPUT
Definition BKE_node.hh:869
#define NODE_GROUP_INPUT
Definition BKE_node.hh:868
#define GEO_NODE_INPUT_NAMED_ATTRIBUTE
#define GEO_NODE_INSTANCE_ON_POINTS
#define GEO_NODE_COLLECTION_INFO
General operations, lookup, etc. for blender objects.
General operations for point clouds.
Object groups, one object can be in many groups at once.
#define MAX_NAME
Definition DNA_defs.h:63
#define BLI_assert_unreachable()
constexpr IndexRange()=default
constexpr void copy_from(Span< T > values) const
Definition BLI_span.hh:745
constexpr Span()=default
constexpr const char * c_str() const
GSpanAttributeWriter lookup_or_add_for_write_only_span(StringRef name, AttrDomain domain, AttrType data_type)
void read_geometry(bke::GeometrySet &geometry_set, USDMeshReadParams params, const char **r_err_str) override
void set_collection(Main *bmain, Collection &coll)
void read_object_data(Main *bmain, pxr::UsdTimeCode time) override
const USDImportParams & import_params_
void read_object_data(Main *bmain, pxr::UsdTimeCode time) override
#define storage(slot, qualifiers)
bNode * node_find_node_by_name(bNodeTree &ntree, StringRefNull name)
Definition node.cc:4047
bNode * node_add_static_node(const bContext *C, bNodeTree &ntree, int type)
Definition node.cc:4283
bNodeLink & node_add_link(bNodeTree &ntree, bNode &fromnode, bNodeSocket &fromsock, bNode &tonode, bNodeSocket &tosock)
Definition node.cc:4638
bNodeTree * node_tree_add_tree(Main *bmain, StringRef name, StringRef idname)
Definition node.cc:4992
bNodeSocket * node_find_socket(bNode &node, eNodeSocketInOut in_out, UString identifier)
Definition node.cc:3036
USDMeshReadParams create_mesh_read_params(const double motion_sample_time, const int read_flags)
static bNode * add_input_named_attrib_node(bNodeTree *ntree, const char *name, int8_t prop_type)
QuaternionBase< float > Quaternion
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_pointcloud_nomain_to_pointcloud(PointCloud *pointcloud_src, PointCloud *pointcloud_dst)
void BKE_ntree_update_tag_socket_property(bNodeTree *ntree, bNodeSocket *socket)
void BKE_modifiers_persistent_uid_init(const Object &object, ModifierData &md)
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
ModifierData * BKE_modifier_new(int type)
PointCloud * BKE_pointcloud_add(Main *bmain, const char *name)
void BKE_ntree_update_after_single_tree_change(Main &bmain, bNodeTree &modified_tree, const NodeTreeUpdateExtraParams &params={})
ModifierData * BKE_modifiers_findby_type(const Object *ob, ModifierType type)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
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
void BKE_object_modifier_set_active(Object *ob, ModifierData *md)
Definition object.cc:1415
signed char int8_t
Definition BKE_text.h:76
ListBaseT< ModifierData > modifiers
bNodeTreeInterface tree_interface
ListBaseT< bNodeSocket > outputs
ListBaseT< bNodeSocket > inputs
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