Blender  V2.93
abc_reader_points.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2016 Kévin Dietrich.
17  * All rights reserved.
18  */
19 
24 #include "abc_reader_points.h"
25 #include "abc_reader_mesh.h"
26 #include "abc_reader_transform.h"
27 #include "abc_util.h"
28 
29 #include "DNA_mesh_types.h"
30 #include "DNA_modifier_types.h"
31 #include "DNA_object_types.h"
32 
33 #include "BKE_customdata.h"
34 #include "BKE_mesh.h"
35 #include "BKE_object.h"
36 
37 using Alembic::AbcGeom::kWrapExisting;
38 using Alembic::AbcGeom::N3fArraySamplePtr;
39 using Alembic::AbcGeom::P3fArraySamplePtr;
40 
41 using Alembic::AbcGeom::ICompoundProperty;
42 using Alembic::AbcGeom::IN3fArrayProperty;
43 using Alembic::AbcGeom::IPoints;
44 using Alembic::AbcGeom::IPointsSchema;
45 using Alembic::AbcGeom::ISampleSelector;
46 
47 namespace blender::io::alembic {
48 
49 AbcPointsReader::AbcPointsReader(const Alembic::Abc::IObject &object, ImportSettings &settings)
50  : AbcObjectReader(object, settings)
51 {
52  IPoints ipoints(m_iobject, kWrapExisting);
53  m_schema = ipoints.getSchema();
55 }
56 
58 {
59  return m_schema.valid();
60 }
61 
63  const Alembic::AbcCoreAbstract::ObjectHeader &alembic_header,
64  const Object *const ob,
65  const char **err_str) const
66 {
67  if (!Alembic::AbcGeom::IPoints::matches(alembic_header)) {
68  *err_str =
69  "Object type mismatch, Alembic object path pointed to Points when importing, but not any "
70  "more.";
71  return false;
72  }
73 
74  if (ob->type != OB_MESH) {
75  *err_str = "Object type mismatch, Alembic object path points to Points.";
76  return false;
77  }
78 
79  return true;
80 }
81 
82 void AbcPointsReader::readObjectData(Main *bmain, const Alembic::Abc::ISampleSelector &sample_sel)
83 {
84  Mesh *mesh = BKE_mesh_add(bmain, m_data_name.c_str());
85  Mesh *read_mesh = this->read_mesh(mesh, sample_sel, 0, nullptr);
86 
87  if (read_mesh != mesh) {
89  }
90 
92  BKE_mesh_validate(mesh, false, false);
93  }
94 
96  m_object->data = mesh;
97 
98  if (has_animations(m_schema, m_settings)) {
100  }
101 }
102 
103 void read_points_sample(const IPointsSchema &schema,
104  const ISampleSelector &selector,
105  CDStreamConfig &config)
106 {
107  Alembic::AbcGeom::IPointsSchema::Sample sample = schema.getValue(selector);
108 
109  const P3fArraySamplePtr &positions = sample.getPositions();
110 
111  ICompoundProperty prop = schema.getArbGeomParams();
112  N3fArraySamplePtr vnormals;
113 
114  if (has_property(prop, "N")) {
115  const Alembic::Util::uint32_t itime = static_cast<Alembic::Util::uint32_t>(
116  selector.getRequestedTime());
117  const IN3fArrayProperty &normals_prop = IN3fArrayProperty(prop, "N", itime);
118 
119  if (normals_prop) {
120  vnormals = normals_prop.getValue(selector);
121  }
122  }
123 
124  read_mverts(config.mvert, positions, vnormals);
125 }
126 
127 struct Mesh *AbcPointsReader::read_mesh(struct Mesh *existing_mesh,
128  const ISampleSelector &sample_sel,
129  int read_flag,
130  const char **err_str)
131 {
132  IPointsSchema::Sample sample;
133  try {
134  sample = m_schema.getValue(sample_sel);
135  }
136  catch (Alembic::Util::Exception &ex) {
137  *err_str = "Error reading points sample; more detail on the console";
138  printf("Alembic: error reading points sample for '%s/%s' at time %f: %s\n",
139  m_iobject.getFullName().c_str(),
140  m_schema.getName().c_str(),
141  sample_sel.getRequestedTime(),
142  ex.what());
143  return existing_mesh;
144  }
145 
146  const P3fArraySamplePtr &positions = sample.getPositions();
147 
148  Mesh *new_mesh = nullptr;
149 
150  if (existing_mesh->totvert != positions->size()) {
151  new_mesh = BKE_mesh_new_nomain(positions->size(), 0, 0, 0, 0);
152  }
153 
154  Mesh *mesh_to_export = new_mesh ? new_mesh : existing_mesh;
155  const bool use_vertex_interpolation = read_flag & MOD_MESHSEQ_INTERPOLATE_VERTICES;
156  CDStreamConfig config = get_config(mesh_to_export, use_vertex_interpolation);
157  read_points_sample(m_schema, sample_sel, config);
158 
159  return mesh_to_export;
160 }
161 
162 } // namespace blender::io::alembic
CustomData interface, see also DNA_customdata_types.h.
const CustomData_MeshMasks CD_MASK_MESH
Definition: customdata.c:1933
void BKE_mesh_nomain_to_mesh(struct Mesh *mesh_src, struct Mesh *mesh_dst, struct Object *ob, const struct CustomData_MeshMasks *mask, bool take_ownership)
struct Mesh * BKE_mesh_add(struct Main *bmain, const char *name)
Definition: mesh.c:849
struct Mesh * BKE_mesh_new_nomain(int verts_len, int edges_len, int tessface_len, int loops_len, int polys_len)
Definition: mesh.c:877
bool BKE_mesh_validate(struct Mesh *me, const bool do_verbose, const bool cddata_check_mask)
General operations, lookup, etc. for blender objects.
struct Object * BKE_object_add_only_object(struct Main *bmain, int type, const char *name) ATTR_NONNULL(1) ATTR_RETURNS_NONNULL
Definition: object.c:2193
@ MOD_MESHSEQ_INTERPOLATE_VERTICES
Object is a sort of wrapper for general info.
@ OB_MESH
void readObjectData(Main *bmain, const Alembic::Abc::ISampleSelector &sample_sel)
struct Mesh * read_mesh(struct Mesh *existing_mesh, const Alembic::Abc::ISampleSelector &sample_sel, int read_flag, const char **err_str)
AbcPointsReader(const Alembic::Abc::IObject &object, ImportSettings &settings)
bool accepts_object_type(const Alembic::AbcCoreAbstract::ObjectHeader &alembic_header, const Object *const ob, const char **err_str) const
static void sample(SocketReader *reader, int x, int y, float color[4])
void get_min_max_time(const Alembic::AbcGeom::IObject &object, const Schema &schema, chrono_t &min, chrono_t &max)
Definition: abc_util.h:80
CDStreamConfig get_config(Mesh *mesh, const bool use_vertex_interpolation)
static void read_mverts(CDStreamConfig &config, const AbcMeshData &mesh_data)
bool has_property(const Alembic::Abc::ICompoundProperty &prop, const std::string &name)
Definition: abc_util.cc:129
void read_points_sample(const IPointsSchema &schema, const ISampleSelector &selector, CDStreamConfig &config)
bool has_animations(Alembic::AbcGeom::IPolyMeshSchema &schema, ImportSettings *settings)
unsigned int uint32_t
Definition: stdint.h:83
Definition: BKE_main.h:116
int totvert
void * data