20#include <pxr/usd/usdGeom/capsule.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/sphere.h>
25#include <pxr/usdImaging/usdImaging/capsuleAdapter.h>
26#include <pxr/usdImaging/usdImaging/coneAdapter.h>
27#include <pxr/usdImaging/usdImaging/cubeAdapter.h>
28#include <pxr/usdImaging/usdImaging/cylinderAdapter.h>
29#include <pxr/usdImaging/usdImaging/sphereAdapter.h>
52 Mesh *read_mesh = this->read_mesh(mesh,
params,
nullptr);
54 if (read_mesh != mesh) {
64template<
typename Adapter>
65void USDShapeReader::read_values(
const double motionSampleTime,
66 pxr::VtVec3fArray &positions,
67 pxr::VtIntArray &face_indices,
68 pxr::VtIntArray &face_counts)
const
71 pxr::VtValue points_val = adapter.GetPoints(
prim_, motionSampleTime);
73 if (points_val.IsHolding<pxr::VtVec3fArray>()) {
74 positions = points_val.Get<pxr::VtVec3fArray>();
77 pxr::VtValue topology_val = adapter.GetTopology(
prim_, pxr::SdfPath(), motionSampleTime);
79 if (topology_val.IsHolding<pxr::HdMeshTopology>()) {
80 const pxr::HdMeshTopology &topology = topology_val.Get<pxr::HdMeshTopology>();
81 face_counts = topology.GetFaceVertexCounts();
82 face_indices = topology.GetFaceVertexIndices();
86bool USDShapeReader::read_mesh_values(
double motionSampleTime,
87 pxr::VtVec3fArray &positions,
88 pxr::VtIntArray &face_indices,
89 pxr::VtIntArray &face_counts)
const
91 if (
prim_.IsA<pxr::UsdGeomCapsule>()) {
92 read_values<pxr::UsdImagingCapsuleAdapter>(
93 motionSampleTime, positions, face_indices, face_counts);
97 if (
prim_.IsA<pxr::UsdGeomCylinder>()) {
98 read_values<pxr::UsdImagingCylinderAdapter>(
99 motionSampleTime, positions, face_indices, face_counts);
103 if (
prim_.IsA<pxr::UsdGeomCone>()) {
104 read_values<pxr::UsdImagingConeAdapter>(
105 motionSampleTime, positions, face_indices, face_counts);
109 if (
prim_.IsA<pxr::UsdGeomCube>()) {
110 read_values<pxr::UsdImagingCubeAdapter>(
111 motionSampleTime, positions, face_indices, face_counts);
115 if (
prim_.IsA<pxr::UsdGeomSphere>()) {
116 read_values<pxr::UsdImagingSphereAdapter>(
117 motionSampleTime, positions, face_indices, face_counts);
123 "Unhandled Gprim type: %s (%s)",
124 prim_.GetTypeName().GetText(),
125 prim_.GetPath().GetText());
129Mesh *USDShapeReader::read_mesh(
Mesh *existing_mesh,
130 const USDMeshReadParams
params,
133 pxr::VtIntArray face_indices;
134 pxr::VtIntArray face_counts;
137 return existing_mesh;
141 Mesh *active_mesh = mesh_from_prim(existing_mesh,
params, face_indices, face_counts);
143 if (active_mesh == existing_mesh) {
144 return existing_mesh;
147 MutableSpan<int> face_offsets = active_mesh->face_offsets_for_write();
148 for (
const int i : IndexRange(active_mesh->faces_num)) {
149 face_offsets[i] = face_counts[i];
156 MutableSpan<int> corner_verts = active_mesh->corner_verts_for_write();
157 for (
const int i : corner_verts.index_range()) {
158 corner_verts[i] = face_indices[i];
167 const char **r_err_str)
170 Mesh *new_mesh = read_mesh(existing_mesh,
params, r_err_str);
172 if (new_mesh != existing_mesh) {
177void USDShapeReader::apply_primvars_to_mesh(
Mesh *mesh,
const double motionSampleTime)
const
180 if (!mesh || !
prim_) {
184 pxr::UsdGeomPrimvarsAPI pv_api = pxr::UsdGeomPrimvarsAPI(
prim_);
185 std::vector<pxr::UsdGeomPrimvar> primvars = pv_api.GetPrimvarsWithValues();
187 pxr::TfToken active_color_name;
189 for (
const pxr::UsdGeomPrimvar &pv : primvars) {
190 if (!pv.HasValue()) {
193 "Skipping primvar %s, mesh %s -- no value",
194 pv.GetName().GetText(),
199 if (!pv.GetAttr().GetTypeName().IsArray()) {
204 const pxr::TfToken name = pxr::UsdGeomPrimvar::StripPrimvarsName(pv.GetPrimvarName());
207 if (primvar_time_varying_map_.
contains(name) && !primvar_time_varying_map_.
lookup(name)) {
211 const pxr::SdfValueTypeName sdf_type = pv.GetTypeName();
219 active_color_name =
name;
226 if (!primvar_time_varying_map_.
contains(name)) {
227 primvar_time_varying_map_.
add(name, pv.ValueMightBeTimeVarying());
231 if (!active_color_name.IsEmpty()) {
237Mesh *USDShapeReader::mesh_from_prim(
Mesh *existing_mesh,
238 const USDMeshReadParams
params,
239 pxr::VtIntArray &face_indices,
240 pxr::VtIntArray &face_counts)
const
242 pxr::VtVec3fArray positions;
244 if (!read_mesh_values(
params.motion_sample_time, positions, face_indices, face_counts)) {
245 return existing_mesh;
248 const bool poly_counts_match = existing_mesh ? face_counts.size() == existing_mesh->
faces_num :
250 const bool position_counts_match = existing_mesh ? positions.size() == existing_mesh->
verts_num :
253 Mesh *active_mesh =
nullptr;
254 if (!position_counts_match || !poly_counts_match) {
256 existing_mesh, positions.size(), 0, face_counts.size(), face_indices.size());
259 active_mesh = existing_mesh;
262 MutableSpan<float3> vert_positions = active_mesh->vert_positions_for_write();
263 vert_positions.copy_from(
Span(positions.data(), positions.size()).cast<
float3>());
266 if (active_mesh != existing_mesh) {
268 this->primvar_time_varying_map_.
clear();
270 apply_primvars_to_mesh(active_mesh,
params.motion_sample_time);
278 for (
const bool animating_flag : primvar_time_varying_map_.
values()) {
279 if (animating_flag) {
284 if (
prim_.IsA<pxr::UsdGeomCapsule>()) {
285 pxr::UsdGeomCapsule geom(
prim_);
286 return (geom.GetAxisAttr().ValueMightBeTimeVarying() ||
287 geom.GetHeightAttr().ValueMightBeTimeVarying() ||
288 geom.GetRadiusAttr().ValueMightBeTimeVarying());
291 if (
prim_.IsA<pxr::UsdGeomCylinder>()) {
292 pxr::UsdGeomCylinder geom(
prim_);
293 return (geom.GetAxisAttr().ValueMightBeTimeVarying() ||
294 geom.GetHeightAttr().ValueMightBeTimeVarying() ||
295 geom.GetRadiusAttr().ValueMightBeTimeVarying());
298 if (
prim_.IsA<pxr::UsdGeomCone>()) {
299 pxr::UsdGeomCone geom(
prim_);
300 return (geom.GetAxisAttr().ValueMightBeTimeVarying() ||
301 geom.GetHeightAttr().ValueMightBeTimeVarying() ||
302 geom.GetRadiusAttr().ValueMightBeTimeVarying());
305 if (
prim_.IsA<pxr::UsdGeomCube>()) {
306 pxr::UsdGeomCube geom(
prim_);
307 return geom.GetSizeAttr().ValueMightBeTimeVarying();
310 if (
prim_.IsA<pxr::UsdGeomSphere>()) {
311 pxr::UsdGeomSphere geom(
prim_);
312 return geom.GetRadiusAttr().ValueMightBeTimeVarying();
317 "Unhandled Gprim type: %s (%s)",
318 prim_.GetTypeName().GetText(),
319 prim_.GetPath().GetText());
void BKE_id_attributes_default_color_set(struct ID *id, const char *name)
void BKE_id_attributes_active_color_set(struct ID *id, const char *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.
bool add(const Key &key, const Value &value)
const Value & lookup(const Key &key) const
ValueIterator values() const
bool contains(const Key &key) const
void add_cache_modifier()
const USDImportParams & import_params_
const std::string & name() const
ReportList * reports() const
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 create_object(Main *bmain, double) 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)
const pxr::TfToken displayColor("displayColor", pxr::TfToken::Immortal)
void replace_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
Mesh * get_mesh_for_write()