Blender  V3.3
abc_reader_points.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2016 Kévin Dietrich. All rights reserved. */
3 
8 #include "abc_reader_points.h"
9 #include "abc_reader_mesh.h"
10 #include "abc_reader_transform.h"
11 #include "abc_util.h"
12 
13 #include "DNA_mesh_types.h"
14 #include "DNA_modifier_types.h"
15 #include "DNA_object_types.h"
16 
17 #include "BKE_customdata.h"
18 #include "BKE_mesh.h"
19 #include "BKE_object.h"
20 
21 using Alembic::AbcGeom::kWrapExisting;
22 using Alembic::AbcGeom::N3fArraySamplePtr;
23 using Alembic::AbcGeom::P3fArraySamplePtr;
24 
25 using Alembic::AbcGeom::ICompoundProperty;
26 using Alembic::AbcGeom::IN3fArrayProperty;
27 using Alembic::AbcGeom::IPoints;
28 using Alembic::AbcGeom::IPointsSchema;
29 using Alembic::AbcGeom::ISampleSelector;
30 
31 namespace blender::io::alembic {
32 
33 AbcPointsReader::AbcPointsReader(const Alembic::Abc::IObject &object, ImportSettings &settings)
34  : AbcObjectReader(object, settings)
35 {
36  IPoints ipoints(m_iobject, kWrapExisting);
37  m_schema = ipoints.getSchema();
39 }
40 
42 {
43  return m_schema.valid();
44 }
45 
47  const Alembic::AbcCoreAbstract::ObjectHeader &alembic_header,
48  const Object *const ob,
49  const char **err_str) const
50 {
51  if (!Alembic::AbcGeom::IPoints::matches(alembic_header)) {
52  *err_str =
53  "Object type mismatch, Alembic object path pointed to Points when importing, but not any "
54  "more.";
55  return false;
56  }
57 
58  if (ob->type != OB_MESH) {
59  *err_str = "Object type mismatch, Alembic object path points to Points.";
60  return false;
61  }
62 
63  return true;
64 }
65 
66 void AbcPointsReader::readObjectData(Main *bmain, const Alembic::Abc::ISampleSelector &sample_sel)
67 {
68  Mesh *mesh = BKE_mesh_add(bmain, m_data_name.c_str());
69  Mesh *read_mesh = this->read_mesh(mesh, sample_sel, 0, "", 0.0f, nullptr);
70 
71  if (read_mesh != mesh) {
73  }
74 
76  BKE_mesh_validate(mesh, false, false);
77  }
78 
80  m_object->data = mesh;
81 
84  }
85 }
86 
87 void read_points_sample(const IPointsSchema &schema,
88  const ISampleSelector &selector,
89  CDStreamConfig &config)
90 {
91  Alembic::AbcGeom::IPointsSchema::Sample sample = schema.getValue(selector);
92 
93  const P3fArraySamplePtr &positions = sample.getPositions();
94 
95  ICompoundProperty prop = schema.getArbGeomParams();
96  N3fArraySamplePtr vnormals;
97 
98  if (has_property(prop, "N")) {
99  const Alembic::Util::uint32_t itime = static_cast<Alembic::Util::uint32_t>(
100  selector.getRequestedTime());
101  const IN3fArrayProperty &normals_prop = IN3fArrayProperty(prop, "N", itime);
102 
103  if (normals_prop) {
104  vnormals = normals_prop.getValue(selector);
105  }
106  }
107 
108  read_mverts(*config.mesh, positions, vnormals);
109 }
110 
111 struct Mesh *AbcPointsReader::read_mesh(struct Mesh *existing_mesh,
112  const ISampleSelector &sample_sel,
113  int read_flag,
114  const char * /*velocity_name*/,
115  const float /*velocity_scale*/,
116  const char **err_str)
117 {
118  IPointsSchema::Sample sample;
119  try {
120  sample = m_schema.getValue(sample_sel);
121  }
122  catch (Alembic::Util::Exception &ex) {
123  *err_str = "Error reading points sample; more detail on the console";
124  printf("Alembic: error reading points sample for '%s/%s' at time %f: %s\n",
125  m_iobject.getFullName().c_str(),
126  m_schema.getName().c_str(),
127  sample_sel.getRequestedTime(),
128  ex.what());
129  return existing_mesh;
130  }
131 
132  const P3fArraySamplePtr &positions = sample.getPositions();
133 
134  Mesh *new_mesh = nullptr;
135 
136  if (existing_mesh->totvert != positions->size()) {
137  new_mesh = BKE_mesh_new_nomain(positions->size(), 0, 0, 0, 0);
138  }
139 
140  Mesh *mesh_to_export = new_mesh ? new_mesh : existing_mesh;
141  const bool use_vertex_interpolation = read_flag & MOD_MESHSEQ_INTERPOLATE_VERTICES;
142  CDStreamConfig config = get_config(mesh_to_export, use_vertex_interpolation);
143  read_points_sample(m_schema, sample_sel, config);
144 
145  return mesh_to_export;
146 }
147 
148 } // namespace blender::io::alembic
CustomData interface, see also DNA_customdata_types.h.
const CustomData_MeshMasks CD_MASK_MESH
Definition: customdata.cc:2065
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.cc:963
bool BKE_mesh_validate(struct Mesh *me, bool do_verbose, bool cddata_check_mask)
struct Mesh * BKE_mesh_new_nomain(int verts_len, int edges_len, int tessface_len, int loops_len, int polys_len)
Definition: mesh.cc:991
General operations, lookup, etc. for blender objects.
struct Object * BKE_object_add_only_object(struct Main *bmain, int type, const char *name) ATTR_RETURNS_NONNULL
Definition: object.cc:2241
@ MOD_MESHSEQ_INTERPOLATE_VERTICES
Object is a sort of wrapper for general info.
@ OB_MESH
struct Mesh * read_mesh(struct Mesh *existing_mesh, const Alembic::Abc::ISampleSelector &sample_sel, int read_flag, const char *velocity_name, float velocity_scale, const char **err_str) override
AbcPointsReader(const Alembic::Abc::IObject &object, ImportSettings &settings)
void readObjectData(Main *bmain, const Alembic::Abc::ISampleSelector &sample_sel) override
bool accepts_object_type(const Alembic::AbcCoreAbstract::ObjectHeader &alembic_header, const Object *const ob, const char **err_str) const override
void get_min_max_time(const Alembic::AbcGeom::IObject &object, const Schema &schema, chrono_t &min, chrono_t &max)
Definition: abc_util.h:66
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:106
void read_points_sample(const IPointsSchema &schema, const ISampleSelector &selector, CDStreamConfig &config)
bool has_animations(Alembic::AbcGeom::IPolyMeshSchema &schema, ImportSettings *settings)
MutableSpan< float3 > positions
unsigned int uint32_t
Definition: stdint.h:80
Definition: BKE_main.h:121
int totvert
void * data