Blender V5.0
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_geometry_set.hh"
6#include "BKE_lib_id.hh"
7#include "BKE_mesh.hh"
8#include "BKE_object.hh"
9#include "BKE_report.hh"
10
11#include "DNA_modifier_types.h"
12#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 = mesh;
48}
49
50void USDShapeReader::read_object_data(Main *bmain, pxr::UsdTimeCode time)
51{
52 const USDMeshReadParams params = create_mesh_read_params(time.GetValue(),
53 import_params_.mesh_read_flag);
54 Mesh *mesh = (Mesh *)object_->data;
55 Mesh *read_mesh = this->read_mesh(mesh, params, nullptr);
56
57 if (read_mesh != mesh) {
58 BKE_mesh_nomain_to_mesh(read_mesh, mesh, object_);
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,
134 const char ** /*r_err_str*/)
135{
136 if (!prim_) {
137 return existing_mesh;
138 }
139
140 pxr::VtIntArray usd_face_indices;
141 pxr::VtIntArray usd_face_counts;
142
143 /* Should have a good set of data by this point-- copy over. */
144 Mesh *active_mesh = mesh_from_prim(existing_mesh, params, usd_face_indices, usd_face_counts);
145
146 if (active_mesh == existing_mesh) {
147 return existing_mesh;
148 }
149
150 Span<int> face_indices = Span(usd_face_indices.cdata(), usd_face_indices.size());
151 Span<int> face_counts = Span(usd_face_counts.cdata(), usd_face_counts.size());
152
153 MutableSpan<int> face_offsets = active_mesh->face_offsets_for_write();
154 for (const int i : IndexRange(active_mesh->faces_num)) {
155 face_offsets[i] = face_counts[i];
156 }
158
159 /* Don't smooth-shade cubes; we're not worrying about sharpness for Gprims. */
160 bke::mesh_smooth_set(*active_mesh, !prim_.IsA<pxr::UsdGeomCube>());
161
162 MutableSpan<int> corner_verts = active_mesh->corner_verts_for_write();
163 for (const int i : corner_verts.index_range()) {
164 corner_verts[i] = face_indices[i];
165 }
166
167 bke::mesh_calc_edges(*active_mesh, false, false);
168 return active_mesh;
169}
170
173 const char **r_err_str)
174{
175 Mesh *existing_mesh = geometry_set.get_mesh_for_write();
176 Mesh *new_mesh = read_mesh(existing_mesh, params, r_err_str);
177
178 if (new_mesh != existing_mesh) {
179 geometry_set.replace_mesh(new_mesh);
180 }
181}
182
183void USDShapeReader::apply_primvars_to_mesh(Mesh *mesh, const pxr::UsdTimeCode time) const
184{
185 /* TODO: also handle the displayOpacity primvar. */
186 if (!mesh || !prim_) {
187 return;
188 }
189
190 pxr::UsdGeomPrimvarsAPI pv_api = pxr::UsdGeomPrimvarsAPI(prim_);
191 std::vector<pxr::UsdGeomPrimvar> primvars = pv_api.GetPrimvarsWithValues();
192
193 pxr::TfToken active_color_name;
194
195 for (const pxr::UsdGeomPrimvar &pv : primvars) {
196 const pxr::SdfValueTypeName pv_type = pv.GetTypeName();
197 if (!pv_type.IsArray()) {
198 continue; /* Skip non-array primvar attributes. */
199 }
200
201 const pxr::TfToken name = pxr::UsdGeomPrimvar::StripPrimvarsName(pv.GetPrimvarName());
202
203 /* Skip reading primvars that have been read before and are not time varying. */
204 if (primvar_time_varying_map_.contains(name) && !primvar_time_varying_map_.lookup(name)) {
205 continue;
206 }
207
208 const std::optional<bke::AttrType> type = convert_usd_type_to_blender(pv_type);
209 if (type == bke::AttrType::ColorFloat) {
210 /* Set the active color name to 'displayColor', if a color primvar
211 * with this name exists. Otherwise, use the name of the first
212 * color primvar we find for the active color. */
213 if (active_color_name.IsEmpty() || name == usdtokens::displayColor) {
214 active_color_name = name;
215 }
216 }
217
218 read_generic_mesh_primvar(mesh, pv, time, false);
219
220 /* Record whether the primvar attribute might be time varying. */
221 if (!primvar_time_varying_map_.contains(name)) {
222 primvar_time_varying_map_.add(name, pv.ValueMightBeTimeVarying());
223 }
224 }
225
226 if (!active_color_name.IsEmpty()) {
227 BKE_id_attributes_default_color_set(&mesh->id, active_color_name.GetText());
228 BKE_id_attributes_active_color_set(&mesh->id, active_color_name.GetText());
229 }
230}
231
232Mesh *USDShapeReader::mesh_from_prim(Mesh *existing_mesh,
234 pxr::VtIntArray &face_indices,
235 pxr::VtIntArray &face_counts) const
236{
237 pxr::VtVec3fArray positions;
238
239 if (!read_mesh_values(params.motion_sample_time, positions, face_indices, face_counts)) {
240 return existing_mesh;
241 }
242
243 const bool poly_counts_match = existing_mesh ? face_counts.size() == existing_mesh->faces_num :
244 false;
245 const bool position_counts_match = existing_mesh ? positions.size() == existing_mesh->verts_num :
246 false;
247
248 Mesh *active_mesh = nullptr;
249 if (!position_counts_match || !poly_counts_match) {
251 existing_mesh, positions.size(), 0, face_counts.size(), face_indices.size());
252 }
253 else {
254 active_mesh = existing_mesh;
255 }
256
257 MutableSpan<float3> vert_positions = active_mesh->vert_positions_for_write();
258 vert_positions.copy_from(Span(positions.cdata(), positions.size()).cast<float3>());
259
260 if (params.read_flags & MOD_MESHSEQ_READ_COLOR) {
261 if (active_mesh != existing_mesh) {
262 /* Clear the primvar map to force attributes to be reloaded. */
263 this->primvar_time_varying_map_.clear();
264 }
265 apply_primvars_to_mesh(active_mesh, params.motion_sample_time);
266 }
267
268 return active_mesh;
269}
270
272{
273 for (const bool animating_flag : primvar_time_varying_map_.values()) {
274 if (animating_flag) {
275 return true;
276 }
277 }
278
279 if (prim_.IsA<pxr::UsdGeomCapsule>()) {
280 pxr::UsdGeomCapsule geom(prim_);
281 return (geom.GetAxisAttr().ValueMightBeTimeVarying() ||
282 geom.GetHeightAttr().ValueMightBeTimeVarying() ||
283 geom.GetRadiusAttr().ValueMightBeTimeVarying());
284 }
285
286 if (prim_.IsA<pxr::UsdGeomCapsule_1>()) {
287 pxr::UsdGeomCapsule_1 geom(prim_);
288 return (geom.GetAxisAttr().ValueMightBeTimeVarying() ||
289 geom.GetHeightAttr().ValueMightBeTimeVarying() ||
290 geom.GetRadiusTopAttr().ValueMightBeTimeVarying() ||
291 geom.GetRadiusBottomAttr().ValueMightBeTimeVarying());
292 }
293
294 if (prim_.IsA<pxr::UsdGeomCylinder>()) {
295 pxr::UsdGeomCylinder geom(prim_);
296 return (geom.GetAxisAttr().ValueMightBeTimeVarying() ||
297 geom.GetHeightAttr().ValueMightBeTimeVarying() ||
298 geom.GetRadiusAttr().ValueMightBeTimeVarying());
299 }
300
301 if (prim_.IsA<pxr::UsdGeomCylinder_1>()) {
302 pxr::UsdGeomCylinder_1 geom(prim_);
303 return (geom.GetAxisAttr().ValueMightBeTimeVarying() ||
304 geom.GetHeightAttr().ValueMightBeTimeVarying() ||
305 geom.GetRadiusTopAttr().ValueMightBeTimeVarying() ||
306 geom.GetRadiusBottomAttr().ValueMightBeTimeVarying());
307 }
308
309 if (prim_.IsA<pxr::UsdGeomCone>()) {
310 pxr::UsdGeomCone geom(prim_);
311 return (geom.GetAxisAttr().ValueMightBeTimeVarying() ||
312 geom.GetHeightAttr().ValueMightBeTimeVarying() ||
313 geom.GetRadiusAttr().ValueMightBeTimeVarying());
314 }
315
316 if (prim_.IsA<pxr::UsdGeomCube>()) {
317 pxr::UsdGeomCube geom(prim_);
318 return geom.GetSizeAttr().ValueMightBeTimeVarying();
319 }
320
321 if (prim_.IsA<pxr::UsdGeomSphere>()) {
322 pxr::UsdGeomSphere geom(prim_);
323 return geom.GetRadiusAttr().ValueMightBeTimeVarying();
324 }
325
326 if (prim_.IsA<pxr::UsdGeomPlane>()) {
327 pxr::UsdGeomPlane geom(prim_);
328 return (geom.GetWidthAttr().ValueMightBeTimeVarying() ||
329 geom.GetLengthAttr().ValueMightBeTimeVarying() ||
330 geom.GetAxisAttr().ValueMightBeTimeVarying());
331 }
332
334 RPT_ERROR,
335 "Unhandled Gprim type: %s (%s)",
336 prim_.GetTypeName().GetText(),
337 prim_.GetPath().GetText());
338 return false;
339}
340
341} // namespace blender::io::usd
void BKE_id_attributes_default_color_set(struct ID *id, std::optional< blender::StringRef > name)
void BKE_id_attributes_active_color_set(struct ID *id, std::optional< blender::StringRef > name)
Definition attribute.cc:985
Mesh * BKE_mesh_new_nomain_from_template(const Mesh *me_src, int verts_num, int edges_num, int faces_num, int corners_num)
Mesh * BKE_mesh_add(Main *bmain, const char *name)
void BKE_mesh_nomain_to_mesh(Mesh *mesh_src, Mesh *mesh_dst, Object *ob, bool process_shape_keys=true)
General operations, lookup, etc. for blender objects.
Object * BKE_object_add_only_object(Main *bmain, int type, const char *name) ATTR_RETURNS_NONNULL
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
@ RPT_ERROR
Definition BKE_report.hh:39
struct Mesh Mesh
@ MOD_MESHSEQ_READ_COLOR
Object is a sort of wrapper for general info.
@ OB_MESH
const Value & lookup(const Key &key) const
Definition BLI_map.hh:545
bool contains(const Key &key) const
Definition BLI_map.hh:353
constexpr IndexRange index_range() const
Definition BLI_span.hh:670
constexpr void copy_from(Span< T > values) const
Definition BLI_span.hh:739
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
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
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)
VecBase< float, 3 > float3
const pxr::TfToken displayColor("displayColor", pxr::TfToken::Immortal)
const char * name
int faces_num
int verts_num
void replace_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
i
Definition text_draw.cc:230