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 double motionSampleTime,
69 pxr::VtVec3fArray &positions,
70 pxr::VtIntArray &face_indices,
71 pxr::VtIntArray &face_counts)
const
74 pxr::VtValue points_val = adapter.GetPoints(
prim_, motionSampleTime);
76 if (points_val.IsHolding<pxr::VtVec3fArray>()) {
77 positions = points_val.Get<pxr::VtVec3fArray>();
80 pxr::VtValue topology_val = adapter.GetTopology(
prim_, pxr::SdfPath(), motionSampleTime);
82 if (topology_val.IsHolding<pxr::HdMeshTopology>()) {
83 const pxr::HdMeshTopology &topology = topology_val.Get<pxr::HdMeshTopology>();
84 face_counts = topology.GetFaceVertexCounts();
85 face_indices = topology.GetFaceVertexIndices();
89bool USDShapeReader::read_mesh_values(
double motionSampleTime,
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>(
96 motionSampleTime, positions, face_indices, face_counts);
100 if (
prim_.IsA<pxr::UsdGeomCylinder>() ||
prim_.IsA<pxr::UsdGeomCylinder_1>()) {
101 read_values<pxr::UsdImagingCylinderAdapter>(
102 motionSampleTime, positions, face_indices, face_counts);
106 if (
prim_.IsA<pxr::UsdGeomCone>()) {
107 read_values<pxr::UsdImagingConeAdapter>(
108 motionSampleTime, positions, face_indices, face_counts);
112 if (
prim_.IsA<pxr::UsdGeomCube>()) {
113 read_values<pxr::UsdImagingCubeAdapter>(
114 motionSampleTime, positions, face_indices, face_counts);
118 if (
prim_.IsA<pxr::UsdGeomSphere>()) {
119 read_values<pxr::UsdImagingSphereAdapter>(
120 motionSampleTime, positions, face_indices, face_counts);
124 if (
prim_.IsA<pxr::UsdGeomPlane>()) {
125 read_values<pxr::UsdImagingPlaneAdapter>(
126 motionSampleTime, positions, face_indices, face_counts);
132 "Unhandled Gprim type: %s (%s)",
133 prim_.GetTypeName().GetText(),
134 prim_.GetPath().GetText());
138Mesh *USDShapeReader::read_mesh(
Mesh *existing_mesh,
143 return existing_mesh;
146 pxr::VtIntArray usd_face_indices;
147 pxr::VtIntArray usd_face_counts;
150 Mesh *active_mesh = mesh_from_prim(existing_mesh,
params, usd_face_indices, usd_face_counts);
152 if (active_mesh == existing_mesh) {
153 return existing_mesh;
156 Span<int> face_indices =
Span(usd_face_indices.cdata(), usd_face_indices.size());
157 Span<int> face_counts =
Span(usd_face_counts.cdata(), usd_face_counts.size());
159 MutableSpan<int> face_offsets = active_mesh->face_offsets_for_write();
160 for (
const int i : IndexRange(active_mesh->
faces_num)) {
161 face_offsets[
i] = face_counts[
i];
168 MutableSpan<int> corner_verts = active_mesh->corner_verts_for_write();
170 corner_verts[
i] = face_indices[
i];
179 const char **r_err_str)
182 Mesh *new_mesh = read_mesh(existing_mesh,
params, r_err_str);
184 if (new_mesh != existing_mesh) {
189void USDShapeReader::apply_primvars_to_mesh(
Mesh *mesh,
const double motionSampleTime)
const
192 if (!mesh || !
prim_) {
196 pxr::UsdGeomPrimvarsAPI pv_api = pxr::UsdGeomPrimvarsAPI(
prim_);
197 std::vector<pxr::UsdGeomPrimvar> primvars = pv_api.GetPrimvarsWithValues();
199 pxr::TfToken active_color_name;
201 for (
const pxr::UsdGeomPrimvar &pv : primvars) {
202 const pxr::SdfValueTypeName pv_type = pv.GetTypeName();
203 if (!pv_type.IsArray()) {
207 const pxr::TfToken
name = pxr::UsdGeomPrimvar::StripPrimvarsName(pv.GetPrimvarName());
220 active_color_name =
name;
227 if (!primvar_time_varying_map_.contains(
name)) {
228 primvar_time_varying_map_.add(
name, pv.ValueMightBeTimeVarying());
232 if (!active_color_name.IsEmpty()) {
238Mesh *USDShapeReader::mesh_from_prim(
Mesh *existing_mesh,
240 pxr::VtIntArray &face_indices,
241 pxr::VtIntArray &face_counts)
const
243 pxr::VtVec3fArray positions;
245 if (!read_mesh_values(
params.motion_sample_time, positions, face_indices, face_counts)) {
246 return existing_mesh;
249 const bool poly_counts_match = existing_mesh ? face_counts.size() == existing_mesh->
faces_num :
251 const bool position_counts_match = existing_mesh ? positions.size() == existing_mesh->
verts_num :
254 Mesh *active_mesh =
nullptr;
255 if (!position_counts_match || !poly_counts_match) {
257 existing_mesh, positions.size(), 0, face_counts.size(), face_indices.size());
260 active_mesh = existing_mesh;
263 MutableSpan<float3> vert_positions = active_mesh->vert_positions_for_write();
267 if (active_mesh != existing_mesh) {
269 this->primvar_time_varying_map_.clear();
271 apply_primvars_to_mesh(active_mesh,
params.motion_sample_time);
279 for (
const bool animating_flag : primvar_time_varying_map_.values()) {
280 if (animating_flag) {
285 if (
prim_.IsA<pxr::UsdGeomCapsule>()) {
286 pxr::UsdGeomCapsule geom(
prim_);
287 return (geom.GetAxisAttr().ValueMightBeTimeVarying() ||
288 geom.GetHeightAttr().ValueMightBeTimeVarying() ||
289 geom.GetRadiusAttr().ValueMightBeTimeVarying());
292 if (
prim_.IsA<pxr::UsdGeomCapsule_1>()) {
293 pxr::UsdGeomCapsule_1 geom(
prim_);
294 return (geom.GetAxisAttr().ValueMightBeTimeVarying() ||
295 geom.GetHeightAttr().ValueMightBeTimeVarying() ||
296 geom.GetRadiusTopAttr().ValueMightBeTimeVarying() ||
297 geom.GetRadiusBottomAttr().ValueMightBeTimeVarying());
300 if (
prim_.IsA<pxr::UsdGeomCylinder>()) {
301 pxr::UsdGeomCylinder geom(
prim_);
302 return (geom.GetAxisAttr().ValueMightBeTimeVarying() ||
303 geom.GetHeightAttr().ValueMightBeTimeVarying() ||
304 geom.GetRadiusAttr().ValueMightBeTimeVarying());
307 if (
prim_.IsA<pxr::UsdGeomCylinder_1>()) {
308 pxr::UsdGeomCylinder_1 geom(
prim_);
309 return (geom.GetAxisAttr().ValueMightBeTimeVarying() ||
310 geom.GetHeightAttr().ValueMightBeTimeVarying() ||
311 geom.GetRadiusTopAttr().ValueMightBeTimeVarying() ||
312 geom.GetRadiusBottomAttr().ValueMightBeTimeVarying());
315 if (
prim_.IsA<pxr::UsdGeomCone>()) {
316 pxr::UsdGeomCone geom(
prim_);
317 return (geom.GetAxisAttr().ValueMightBeTimeVarying() ||
318 geom.GetHeightAttr().ValueMightBeTimeVarying() ||
319 geom.GetRadiusAttr().ValueMightBeTimeVarying());
322 if (
prim_.IsA<pxr::UsdGeomCube>()) {
323 pxr::UsdGeomCube geom(
prim_);
324 return geom.GetSizeAttr().ValueMightBeTimeVarying();
327 if (
prim_.IsA<pxr::UsdGeomSphere>()) {
328 pxr::UsdGeomSphere geom(
prim_);
329 return geom.GetRadiusAttr().ValueMightBeTimeVarying();
332 if (
prim_.IsA<pxr::UsdGeomPlane>()) {
333 pxr::UsdGeomPlane geom(
prim_);
334 return (geom.GetWidthAttr().ValueMightBeTimeVarying() ||
335 geom.GetLengthAttr().ValueMightBeTimeVarying() ||
336 geom.GetAxisAttr().ValueMightBeTimeVarying());
341 "Unhandled Gprim type: %s (%s)",
342 prim_.GetTypeName().GetText(),
343 prim_.GetPath().GetText());
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)
Mesh * BKE_mesh_new_nomain_from_template(const Mesh *me_src, int verts_num, int edges_num, int faces_num, int corners_num)
void BKE_mesh_nomain_to_mesh(Mesh *mesh_src, Mesh *mesh_dst, Object *ob)
Mesh * BKE_mesh_add(Main *bmain, const char *name)
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
Object is a sort of wrapper for general info.
const Value & lookup(const Key &key) const
bool contains(const Key &key) const
constexpr IndexRange index_range() const
constexpr void copy_from(Span< T > values) 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, double motionSampleTime) 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< eCustomDataType > convert_usd_type_to_blender(const pxr::SdfValueTypeName usd_type)
void read_generic_mesh_primvar(Mesh *mesh, const pxr::UsdGeomPrimvar &primvar, const double motionSampleTime, 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)
void replace_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
Mesh * get_mesh_for_write()