Blender V5.3
usd_reader_shape.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Nvidia. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BKE_attribute.h"
6#include "BKE_geometry_set.hh"
7#include "BKE_lib_id.hh"
8#include "BKE_mesh.hh"
9#include "BKE_object.hh"
10#include "BKE_report.hh"
11
12#include "DNA_modifier_types.h"
13#include "DNA_object_types.h"
14
16#include "usd_mesh_utils.hh"
17#include "usd_reader_shape.hh"
18
19#include <pxr/usd/usdGeom/capsule.h>
20#include <pxr/usd/usdGeom/capsule_1.h>
21#include <pxr/usd/usdGeom/cone.h>
22#include <pxr/usd/usdGeom/cube.h>
23#include <pxr/usd/usdGeom/cylinder.h>
24#include <pxr/usd/usdGeom/cylinder_1.h>
25#include <pxr/usd/usdGeom/plane.h>
26#include <pxr/usd/usdGeom/sphere.h>
27#include <pxr/usdImaging/usdImaging/capsuleAdapter.h>
28#include <pxr/usdImaging/usdImaging/coneAdapter.h>
29#include <pxr/usdImaging/usdImaging/cubeAdapter.h>
30#include <pxr/usdImaging/usdImaging/cylinderAdapter.h>
31#include <pxr/usdImaging/usdImaging/planeAdapter.h>
32#include <pxr/usdImaging/usdImaging/sphereAdapter.h>
33
34namespace blender::io::usd {
35
37 const USDImportParams &import_params,
38 const ImportSettings &settings)
39 : USDGeomReader(prim, import_params, settings)
40{
41}
42
44{
45 Mesh *mesh = BKE_mesh_add(bmain, name_.c_str());
47 object_->data = id_cast<ID *>(mesh);
48}
49
50void USDShapeReader::read_object_data(Main *bmain, pxr::UsdTimeCode time)
51{
52 const USDMeshReadParams params = create_mesh_read_params(time.GetValue(),
54 Mesh *mesh = id_cast<Mesh *>(object_->data);
55 Mesh *read_mesh = this->read_mesh(mesh, params, nullptr);
56
57 if (read_mesh != mesh) {
59 if (is_time_varying()) {
61 }
62 }
63
65}
66
67template<typename Adapter>
68void USDShapeReader::read_values(const pxr::UsdTimeCode time,
69 pxr::VtVec3fArray &positions,
70 pxr::VtIntArray &face_indices,
71 pxr::VtIntArray &face_counts) const
72{
73 Adapter adapter;
74 pxr::VtValue points_val = adapter.GetPoints(prim_, time);
75
76 if (points_val.IsHolding<pxr::VtVec3fArray>()) {
77 positions = points_val.UncheckedGet<pxr::VtVec3fArray>();
78 }
79
80 pxr::VtValue topology_val = adapter.GetTopology(prim_, pxr::SdfPath(), time);
81
82 if (topology_val.IsHolding<pxr::HdMeshTopology>()) {
83 const pxr::HdMeshTopology &topology = topology_val.UncheckedGet<pxr::HdMeshTopology>();
84 face_counts = topology.GetFaceVertexCounts();
85 face_indices = topology.GetFaceVertexIndices();
86 }
87}
88
89bool USDShapeReader::read_mesh_values(pxr::UsdTimeCode time,
90 pxr::VtVec3fArray &positions,
91 pxr::VtIntArray &face_indices,
92 pxr::VtIntArray &face_counts) const
93{
94 if (prim_.IsA<pxr::UsdGeomCapsule>() || prim_.IsA<pxr::UsdGeomCapsule_1>()) {
95 read_values<pxr::UsdImagingCapsuleAdapter>(time, positions, face_indices, face_counts);
96 return true;
97 }
98
99 if (prim_.IsA<pxr::UsdGeomCylinder>() || prim_.IsA<pxr::UsdGeomCylinder_1>()) {
100 read_values<pxr::UsdImagingCylinderAdapter>(time, positions, face_indices, face_counts);
101 return true;
102 }
103
104 if (prim_.IsA<pxr::UsdGeomCone>()) {
105 read_values<pxr::UsdImagingConeAdapter>(time, positions, face_indices, face_counts);
106 return true;
107 }
108
109 if (prim_.IsA<pxr::UsdGeomCube>()) {
110 read_values<pxr::UsdImagingCubeAdapter>(time, positions, face_indices, face_counts);
111 return true;
112 }
113
114 if (prim_.IsA<pxr::UsdGeomSphere>()) {
115 read_values<pxr::UsdImagingSphereAdapter>(time, positions, face_indices, face_counts);
116 return true;
117 }
118
119 if (prim_.IsA<pxr::UsdGeomPlane>()) {
120 read_values<pxr::UsdImagingPlaneAdapter>(time, positions, face_indices, face_counts);
121 return true;
122 }
123
125 RPT_ERROR,
126 "Unhandled Gprim type: %s (%s)",
127 prim_.GetTypeName().GetText(),
128 prim_.GetPath().GetText());
129 return false;
130}
131
132Mesh *USDShapeReader::read_mesh(Mesh *existing_mesh,
133 const USDMeshReadParams params,
134 const char ** /*r_err_str*/)
135{
136 if (!prim_) {
137 return existing_mesh;
138 }
139
140 pxr::VtVec3fArray usd_positions;
141 pxr::VtIntArray usd_face_indices;
142 pxr::VtIntArray usd_face_counts;
143 if (!read_mesh_values(
144 params.motion_sample_time, usd_positions, usd_face_indices, usd_face_counts))
145 {
146 return existing_mesh;
147 }
148
149 /* Build or update the existing mesh. */
150 Mesh *active_mesh = mesh_from_prim(
151 existing_mesh, params, usd_positions, usd_face_indices, usd_face_counts);
152
153 return active_mesh;
154}
155
157 USDMeshReadParams params,
158 const char **r_err_str)
159{
160 Mesh *existing_mesh = geometry_set.get_mesh_for_write();
161 Mesh *new_mesh = read_mesh(existing_mesh, params, r_err_str);
162
163 if (new_mesh != existing_mesh) {
164 geometry_set.replace_mesh(new_mesh);
165 }
166}
167
168void USDShapeReader::apply_primvars_to_mesh(Mesh *mesh, const pxr::UsdTimeCode time) const
169{
170 /* TODO: also handle the displayOpacity primvar. */
171 if (!mesh || !prim_) {
172 return;
173 }
174
175 pxr::UsdGeomPrimvarsAPI pv_api = pxr::UsdGeomPrimvarsAPI(prim_);
176 std::vector<pxr::UsdGeomPrimvar> primvars = pv_api.GetPrimvarsWithValues();
177
178 pxr::TfToken active_color_name;
179
180 for (const pxr::UsdGeomPrimvar &pv : primvars) {
181 const pxr::SdfValueTypeName pv_type = pv.GetTypeName();
182 if (!pv_type.IsArray()) {
183 continue; /* Skip non-array primvar attributes. */
184 }
185
186 const pxr::TfToken name = pxr::UsdGeomPrimvar::StripPrimvarsName(pv.GetPrimvarName());
187
188 /* Skip reading primvars that have been read before and are not time varying. */
189 if (primvar_time_varying_map_.contains(name) && !primvar_time_varying_map_.lookup(name)) {
190 continue;
191 }
192
193 const std::optional<bke::AttrType> type = convert_usd_type_to_blender(pv_type);
194 if (type == bke::AttrType::ColorFloat) {
195 /* Set the active color name to 'displayColor', if a color primvar
196 * with this name exists. Otherwise, use the name of the first
197 * color primvar we find for the active color. */
198 if (active_color_name.IsEmpty() || name == usdtokens::displayColor) {
199 active_color_name = name;
200 }
201 }
202
203 read_generic_mesh_primvar(mesh, pv, time, false);
204
205 /* Record whether the primvar attribute might be time varying. */
206 if (!primvar_time_varying_map_.contains(name)) {
207 primvar_time_varying_map_.add(name, pv.ValueMightBeTimeVarying());
208 }
209 }
210
211 if (!active_color_name.IsEmpty()) {
212 BKE_id_attributes_default_color_set(&mesh->id, active_color_name.GetText());
213 BKE_id_attributes_active_color_set(&mesh->id, active_color_name.GetText());
214 }
215}
216
217Mesh *USDShapeReader::mesh_from_prim(Mesh *existing_mesh,
218 const USDMeshReadParams params,
219 pxr::VtVec3fArray &usd_positions,
220 pxr::VtIntArray &usd_face_indices,
221 pxr::VtIntArray &usd_face_counts) const
222{
223 Span<int> face_indices = Span(usd_face_indices.cdata(), usd_face_indices.size());
224 Span<int> face_counts = Span(usd_face_counts.cdata(), usd_face_counts.size());
225 Span<float3> positions = Span(usd_positions.cdata(), usd_positions.size()).cast<float3>();
226
227 const bool poly_counts_match = existing_mesh ? face_counts.size() == existing_mesh->faces_num :
228 false;
229 const bool position_counts_match = existing_mesh ? positions.size() == existing_mesh->verts_num :
230 false;
231
232 Mesh *active_mesh = nullptr;
233 if (!position_counts_match || !poly_counts_match) {
235 existing_mesh, positions.size(), 0, face_counts.size(), face_indices.size());
236 }
237 else {
238 active_mesh = existing_mesh;
239 }
240
241 MutableSpan<float3> vert_positions = active_mesh->vert_positions_for_write();
242 vert_positions.copy_from(positions);
243
244 MutableSpan<int> face_offsets = active_mesh->face_offsets_for_write();
245 for (const int i : IndexRange(active_mesh->faces_num)) {
246 face_offsets[i] = face_counts[i];
247 }
249
250 MutableSpan<int> corner_verts = active_mesh->corner_verts_for_write();
251 for (const int i : corner_verts.index_range()) {
252 corner_verts[i] = face_indices[i];
253 }
254
255 bke::mesh_calc_edges(*active_mesh, false, false);
256
257 /* Don't smooth-shade cubes; we're not worrying about sharpness for Gprims. */
258 bke::mesh_smooth_set(*active_mesh, !prim_.IsA<pxr::UsdGeomCube>());
259
260 if (params.read_flags & MOD_MESHSEQ_READ_COLOR) {
261 apply_primvars_to_mesh(active_mesh, params.motion_sample_time);
262 }
263
264 return active_mesh;
265}
266
268{
269 for (const bool animating_flag : primvar_time_varying_map_.values()) {
270 if (animating_flag) {
271 return true;
272 }
273 }
274
275 if (prim_.IsA<pxr::UsdGeomCapsule>()) {
276 pxr::UsdGeomCapsule geom(prim_);
277 return (geom.GetAxisAttr().ValueMightBeTimeVarying() ||
278 geom.GetHeightAttr().ValueMightBeTimeVarying() ||
279 geom.GetRadiusAttr().ValueMightBeTimeVarying());
280 }
281
282 if (prim_.IsA<pxr::UsdGeomCapsule_1>()) {
283 pxr::UsdGeomCapsule_1 geom(prim_);
284 return (geom.GetAxisAttr().ValueMightBeTimeVarying() ||
285 geom.GetHeightAttr().ValueMightBeTimeVarying() ||
286 geom.GetRadiusTopAttr().ValueMightBeTimeVarying() ||
287 geom.GetRadiusBottomAttr().ValueMightBeTimeVarying());
288 }
289
290 if (prim_.IsA<pxr::UsdGeomCylinder>()) {
291 pxr::UsdGeomCylinder geom(prim_);
292 return (geom.GetAxisAttr().ValueMightBeTimeVarying() ||
293 geom.GetHeightAttr().ValueMightBeTimeVarying() ||
294 geom.GetRadiusAttr().ValueMightBeTimeVarying());
295 }
296
297 if (prim_.IsA<pxr::UsdGeomCylinder_1>()) {
298 pxr::UsdGeomCylinder_1 geom(prim_);
299 return (geom.GetAxisAttr().ValueMightBeTimeVarying() ||
300 geom.GetHeightAttr().ValueMightBeTimeVarying() ||
301 geom.GetRadiusTopAttr().ValueMightBeTimeVarying() ||
302 geom.GetRadiusBottomAttr().ValueMightBeTimeVarying());
303 }
304
305 if (prim_.IsA<pxr::UsdGeomCone>()) {
306 pxr::UsdGeomCone geom(prim_);
307 return (geom.GetAxisAttr().ValueMightBeTimeVarying() ||
308 geom.GetHeightAttr().ValueMightBeTimeVarying() ||
309 geom.GetRadiusAttr().ValueMightBeTimeVarying());
310 }
311
312 if (prim_.IsA<pxr::UsdGeomCube>()) {
313 pxr::UsdGeomCube geom(prim_);
314 return geom.GetSizeAttr().ValueMightBeTimeVarying();
315 }
316
317 if (prim_.IsA<pxr::UsdGeomSphere>()) {
318 pxr::UsdGeomSphere geom(prim_);
319 return geom.GetRadiusAttr().ValueMightBeTimeVarying();
320 }
321
322 if (prim_.IsA<pxr::UsdGeomPlane>()) {
323 pxr::UsdGeomPlane geom(prim_);
324 return (geom.GetWidthAttr().ValueMightBeTimeVarying() ||
325 geom.GetLengthAttr().ValueMightBeTimeVarying() ||
326 geom.GetAxisAttr().ValueMightBeTimeVarying());
327 }
328
330 RPT_ERROR,
331 "Unhandled Gprim type: %s (%s)",
332 prim_.GetTypeName().GetText(),
333 prim_.GetPath().GetText());
334 return false;
335}
336
337} // namespace blender::io::usd
Generic geometry attributes built on CustomData.
General operations, lookup, etc. for blender objects.
Object is a sort of wrapper for general info.
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange()=default
const Value & lookup(const Key &key) const
Definition BLI_map.hh:545
bool contains(const Key &key) const
Definition BLI_map.hh:353
constexpr Span()=default
constexpr const char * c_str() const
USDGeomReader(const pxr::UsdPrim &prim, const USDImportParams &import_params, const ImportSettings &settings)
const pxr::UsdPrim & prim() const
const USDImportParams & import_params_
void create_object(Main *bmain) override
USDShapeReader(const pxr::UsdPrim &prim, const USDImportParams &import_params, const ImportSettings &settings)
void read_geometry(bke::GeometrySet &, USDMeshReadParams, const char **) override
void read_object_data(Main *bmain, pxr::UsdTimeCode time) override
void read_object_data(Main *bmain, pxr::UsdTimeCode time) override
void mesh_smooth_set(Mesh &mesh, bool use_smooth, bool keep_sharp_edges=false)
void mesh_calc_edges(Mesh &mesh, bool keep_existing_edges, bool select_new_edges)
USDMeshReadParams create_mesh_read_params(const double motion_sample_time, const int read_flags)
std::optional< bke::AttrType > convert_usd_type_to_blender(const pxr::SdfValueTypeName usd_type)
void read_generic_mesh_primvar(Mesh *mesh, const pxr::UsdGeomPrimvar &primvar, const pxr::UsdTimeCode time, const bool is_left_handed)
OffsetIndices< int > accumulate_counts_to_offsets(MutableSpan< int > counts_to_offsets, int start_offset=0)
const pxr::TfToken displayColor("displayColor", pxr::TfToken::Immortal)
void BKE_id_attributes_default_color_set(struct ID *id, std::optional< StringRef > name)
void BKE_mesh_nomain_to_mesh(Mesh *mesh_src, Mesh *mesh_dst, Object *ob, bool process_shape_keys=true)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
void BKE_id_attributes_active_color_set(struct ID *id, std::optional< StringRef > name)
Mesh * BKE_mesh_new_nomain_from_template(const Mesh *me_src, int verts_num, int edges_num, int faces_num, int corners_num)
VecBase< float, 3 > float3
Object * BKE_object_add_only_object(Main *bmain, ObjectType type, const char *name) ATTR_RETURNS_NONNULL
Definition object.cc:2265
Mesh * BKE_mesh_add(Main *bmain, const char *name)
void replace_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
MeshSeqCacheModifierReadFlag mesh_read_flag
Definition usd.hh:192