Blender V5.3
usd_reader_mesh.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 * Adapted from the Blender Alembic importer implementation.
5 * Modifications Copyright 2021 Tangent Animation and. NVIDIA Corporation. All rights reserved. */
6#pragma once
7
8#include "BLI_map.hh"
10#include "BLI_span.hh"
11
12#include "usd.hh"
13#include "usd_api_modifier.hh"
14#include "usd_reader_geom.hh"
15
16#include <pxr/usd/usdGeom/mesh.h>
17
18namespace blender::io::usd {
19
21 private:
22 pxr::VtVec3fArray positions_;
23 pxr::VtVec3fArray normals_;
24 pxr::VtIntArray face_indices_;
25 pxr::VtIntArray face_counts_;
26
27 public:
28 USDMeshReadData(const pxr::UsdGeomMesh &mesh_prim, pxr::UsdTimeCode time);
29
31 {
32 return Span(face_indices_.cdata(), face_indices_.size());
33 }
35 {
36 return Span(face_counts_.cdata(), face_counts_.size());
37 }
39 {
40 return Span(positions_.cdata(), positions_.size()).cast<float3>();
41 }
43 {
44 return Span(normals_.cdata(), normals_.size()).cast<float3>();
45 }
47 {
48 return MutableSpan(normals_.data(), normals_.size()).cast<float3>();
49 }
50
52 pxr::TfToken orientation;
53};
54
56 private:
57 pxr::UsdGeomMesh mesh_prim_;
58
59 bool is_left_handed_ = false;
60 bool is_time_varying_ = false;
61
62 /* This is to ensure we load all data once, because we reuse the read_mesh function
63 * in the mesh seq modifier, and in initial load. Ideally, a better fix would be
64 * implemented. Note this will break if faces or positions vary. */
65 bool is_initial_load_ = false;
66
67 Map<const pxr::TfToken, bool> primvar_varying_map_;
68
69 public:
70 USDMeshReader(const pxr::UsdPrim &prim,
71 const USDImportParams &import_params,
72 const ImportSettings &settings)
73 : USDGeomReader(prim, import_params, settings), mesh_prim_(prim)
74 {
75 }
76
77 bool valid() const override
78 {
79 return bool(mesh_prim_);
80 }
81
82 void create_object(Main *bmain) override;
83 void read_object_data(Main *bmain, pxr::UsdTimeCode time) override;
84
85 void read_geometry(bke::GeometrySet &geometry_set,
86 USDMeshReadParams params,
87 const char **r_err_str) override;
88
89 bool topology_changed(const Mesh *existing_mesh, pxr::UsdTimeCode time) override;
90
99 pxr::SdfPath get_skeleton_path() const;
100
101 private:
102 void process_normals_vertex_varying(Mesh *mesh, MutableSpan<float3> usd_normals) const;
103 void process_normals_face_varying(Mesh *mesh, Span<float3> usd_normals) const;
105 void process_normals_uniform(Mesh *mesh, Span<float3> usd_normals) const;
106 void readFaceSetsSample(Main *bmain, Mesh *mesh, pxr::UsdTimeCode time);
107 void assign_facesets_to_material_indices(pxr::UsdTimeCode time,
108 MutableSpan<int> material_indices,
109 Map<pxr::SdfPath, int> *r_mat_map);
110
111 bool read_faces(Mesh *mesh, const USDMeshReadData &usd_data) const;
112 void read_subdiv();
113 void read_vertex_creases(Mesh *mesh, pxr::UsdTimeCode time);
114 void read_edge_creases(Mesh *mesh, pxr::UsdTimeCode time);
115 void read_velocities(Mesh *mesh, pxr::UsdTimeCode time);
116
117 void read_mesh_sample(ImportSettings *settings,
118 Mesh *mesh,
119 USDMeshReadData &usd_data,
120 pxr::UsdTimeCode time,
121 bool new_mesh);
122
123 Mesh *read_mesh(struct Mesh *existing_mesh,
124 const USDMeshReadParams params,
125 const char **r_err_str);
126
127 void read_custom_data(const ImportSettings *settings,
128 Mesh *mesh,
129 pxr::UsdTimeCode time,
130 bool new_mesh);
131
132 void read_uv_data_primvar(Mesh *mesh,
133 const pxr::UsdGeomPrimvar &primvar,
134 const pxr::UsdTimeCode time);
135
136 bool topology_changed(const Mesh *existing_mesh, const USDMeshReadData &usd_data) const;
137
142 std::optional<XformResult> get_local_usd_xform(pxr::UsdTimeCode time) const override;
143
151 bool is_root_xform_prim() const override;
152};
153
154} // namespace blender::io::usd
constexpr MutableSpan()=default
constexpr Span()=default
USDGeomReader(const pxr::UsdPrim &prim, const USDImportParams &import_params, const ImportSettings &settings)
bool topology_changed(const Mesh *existing_mesh, pxr::UsdTimeCode time) override
USDMeshReader(const pxr::UsdPrim &prim, const USDImportParams &import_params, const ImportSettings &settings)
pxr::SdfPath get_skeleton_path() const
void read_geometry(bke::GeometrySet &geometry_set, USDMeshReadParams params, const char **r_err_str) override
void create_object(Main *bmain) override
void read_object_data(Main *bmain, pxr::UsdTimeCode time) override
const pxr::UsdPrim & prim() const
virtual bool is_root_xform_prim() const
void read_custom_data(const std::string &iobject_full_name, const ICompoundProperty &prop, const CDStreamConfig &config, const Alembic::Abc::ISampleSelector &iss)
VecBase< float, 3 > float3
USDMeshReadData(const pxr::UsdGeomMesh &mesh_prim, pxr::UsdTimeCode time)
MutableSpan< float3 > normals_for_write()