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>
55 Mesh *read_mesh = this->read_mesh(
mesh, params,
nullptr);
57 if (read_mesh !=
mesh) {
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
74 pxr::VtValue points_val = adapter.GetPoints(
prim_, time);
76 if (points_val.IsHolding<pxr::VtVec3fArray>()) {
77 positions = points_val.UncheckedGet<pxr::VtVec3fArray>();
80 pxr::VtValue topology_val = adapter.GetTopology(
prim_, pxr::SdfPath(), time);
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();
89bool USDShapeReader::read_mesh_values(pxr::UsdTimeCode time,
90 pxr::VtVec3fArray &positions,
91 pxr::VtIntArray &face_indices,
92 pxr::VtIntArray &face_counts)
const
94 if (
prim_.IsA<pxr::UsdGeomCapsule>() ||
prim_.IsA<pxr::UsdGeomCapsule_1>()) {
95 read_values<pxr::UsdImagingCapsuleAdapter>(time, positions, face_indices, face_counts);
99 if (
prim_.IsA<pxr::UsdGeomCylinder>() ||
prim_.IsA<pxr::UsdGeomCylinder_1>()) {
100 read_values<pxr::UsdImagingCylinderAdapter>(time, positions, face_indices, face_counts);
104 if (
prim_.IsA<pxr::UsdGeomCone>()) {
105 read_values<pxr::UsdImagingConeAdapter>(time, positions, face_indices, face_counts);
109 if (
prim_.IsA<pxr::UsdGeomCube>()) {
110 read_values<pxr::UsdImagingCubeAdapter>(time, positions, face_indices, face_counts);
114 if (
prim_.IsA<pxr::UsdGeomSphere>()) {
115 read_values<pxr::UsdImagingSphereAdapter>(time, positions, face_indices, face_counts);
119 if (
prim_.IsA<pxr::UsdGeomPlane>()) {
120 read_values<pxr::UsdImagingPlaneAdapter>(time, positions, face_indices, face_counts);
126 "Unhandled Gprim type: %s (%s)",
127 prim_.GetTypeName().GetText(),
128 prim_.GetPath().GetText());
132Mesh *USDShapeReader::read_mesh(Mesh *existing_mesh,
137 return existing_mesh;
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))
146 return existing_mesh;
150 Mesh *active_mesh = mesh_from_prim(
151 existing_mesh, params, usd_positions, usd_face_indices, usd_face_counts);
158 const char **r_err_str)
161 Mesh *new_mesh = read_mesh(existing_mesh, params, r_err_str);
163 if (new_mesh != existing_mesh) {
168void USDShapeReader::apply_primvars_to_mesh(
Mesh *
mesh,
const pxr::UsdTimeCode time)
const
175 pxr::UsdGeomPrimvarsAPI pv_api = pxr::UsdGeomPrimvarsAPI(
prim_);
176 std::vector<pxr::UsdGeomPrimvar> primvars = pv_api.GetPrimvarsWithValues();
178 pxr::TfToken active_color_name;
180 for (
const pxr::UsdGeomPrimvar &pv : primvars) {
181 const pxr::SdfValueTypeName pv_type = pv.GetTypeName();
182 if (!pv_type.IsArray()) {
186 const pxr::TfToken
name = pxr::UsdGeomPrimvar::StripPrimvarsName(pv.GetPrimvarName());
199 active_color_name =
name;
206 if (!primvar_time_varying_map_.contains(
name)) {
207 primvar_time_varying_map_.add(
name, pv.ValueMightBeTimeVarying());
211 if (!active_color_name.IsEmpty()) {
217Mesh *USDShapeReader::mesh_from_prim(Mesh *existing_mesh,
219 pxr::VtVec3fArray &usd_positions,
220 pxr::VtIntArray &usd_face_indices,
221 pxr::VtIntArray &usd_face_counts)
const
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>();
227 const bool poly_counts_match = existing_mesh ? face_counts.
size() == existing_mesh->faces_num :
229 const bool position_counts_match = existing_mesh ? positions.
size() == existing_mesh->verts_num :
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());
238 active_mesh = existing_mesh;
241 MutableSpan<float3> vert_positions = active_mesh->vert_positions_for_write();
242 vert_positions.copy_from(positions);
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];
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];
261 apply_primvars_to_mesh(active_mesh, params.motion_sample_time);
269 for (
const bool animating_flag : primvar_time_varying_map_.values()) {
270 if (animating_flag) {
275 if (
prim_.IsA<pxr::UsdGeomCapsule>()) {
276 pxr::UsdGeomCapsule geom(
prim_);
277 return (geom.GetAxisAttr().ValueMightBeTimeVarying() ||
278 geom.GetHeightAttr().ValueMightBeTimeVarying() ||
279 geom.GetRadiusAttr().ValueMightBeTimeVarying());
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());
290 if (
prim_.IsA<pxr::UsdGeomCylinder>()) {
291 pxr::UsdGeomCylinder geom(
prim_);
292 return (geom.GetAxisAttr().ValueMightBeTimeVarying() ||
293 geom.GetHeightAttr().ValueMightBeTimeVarying() ||
294 geom.GetRadiusAttr().ValueMightBeTimeVarying());
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());
305 if (
prim_.IsA<pxr::UsdGeomCone>()) {
306 pxr::UsdGeomCone geom(
prim_);
307 return (geom.GetAxisAttr().ValueMightBeTimeVarying() ||
308 geom.GetHeightAttr().ValueMightBeTimeVarying() ||
309 geom.GetRadiusAttr().ValueMightBeTimeVarying());
312 if (
prim_.IsA<pxr::UsdGeomCube>()) {
313 pxr::UsdGeomCube geom(
prim_);
314 return geom.GetSizeAttr().ValueMightBeTimeVarying();
317 if (
prim_.IsA<pxr::UsdGeomSphere>()) {
318 pxr::UsdGeomSphere geom(
prim_);
319 return geom.GetRadiusAttr().ValueMightBeTimeVarying();
322 if (
prim_.IsA<pxr::UsdGeomPlane>()) {
323 pxr::UsdGeomPlane geom(
prim_);
324 return (geom.GetWidthAttr().ValueMightBeTimeVarying() ||
325 geom.GetLengthAttr().ValueMightBeTimeVarying() ||
326 geom.GetAxisAttr().ValueMightBeTimeVarying());
331 "Unhandled Gprim type: %s (%s)",
332 prim_.GetTypeName().GetText(),
333 prim_.GetPath().GetText());
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
constexpr IndexRange()=default
const Value & lookup(const Key &key) const
bool contains(const Key &key) const
constexpr const char * c_str() const
void add_cache_modifier()
USDGeomReader(const pxr::UsdPrim &prim, const USDImportParams &import_params, const ImportSettings &settings)
const pxr::UsdPrim & prim() const
StringRefNull name() const
const USDImportParams & import_params_
ReportList * reports() const
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 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
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
Mesh * BKE_mesh_add(Main *bmain, const char *name)
void replace_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
Mesh * get_mesh_for_write()
MeshSeqCacheModifierReadFlag mesh_read_flag