Blender V5.3
abc_reader_nurbs.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "abc_reader_nurbs.h"
10#include "abc_axis_conversion.h"
11#include "abc_util.h"
12
13#include "MEM_guardedalloc.h"
14
15#include "DNA_curve_types.h"
16#include "DNA_object_types.h"
17
18#include "BLI_listbase.hh"
19
20#include "BLT_translation.hh"
21
22#include "BKE_curve.hh"
23#include "BKE_object.hh"
24
25#include "CLG_log.h"
26
27namespace blender {
28
29using Alembic::AbcGeom::FloatArraySamplePtr;
30using Alembic::AbcGeom::kWrapExisting;
31using Alembic::AbcGeom::MetaData;
32using Alembic::AbcGeom::P3fArraySamplePtr;
33
34using Alembic::AbcGeom::ICompoundProperty;
35using Alembic::AbcGeom::INuPatch;
36using Alembic::AbcGeom::INuPatchSchema;
37using Alembic::AbcGeom::IObject;
38
39namespace io::alembic {
40
41static CLG_LogRef LOG = {"io.alembic"};
42
44{
45 getNurbsPatches(m_iobject);
46}
47
49{
50 if (m_schemas.empty()) {
51 return false;
52 }
53
54 std::vector<std::pair<INuPatchSchema, IObject>>::const_iterator it;
55 for (it = m_schemas.begin(); it != m_schemas.end(); ++it) {
56 const INuPatchSchema &schema = it->first;
57
58 if (!schema.valid()) {
59 return false;
60 }
61 }
62
63 return true;
64}
65
67 const Alembic::AbcCoreAbstract::v12::ObjectHeader &alembic_header,
68 const Object *const ob,
69 const char **r_err_str) const
70{
71 if (!Alembic::AbcGeom::INuPatch::matches(alembic_header)) {
72 *r_err_str = RPT_(
73 "Object type mismatch, Alembic object path pointed to NURBS when importing, but not any "
74 "more");
75 return false;
76 }
77
78 if (ob->type != OB_CURVES_LEGACY) {
79 *r_err_str = RPT_("Object type mismatch, Alembic object path points to NURBS");
80 return false;
81 }
82
83 return true;
84}
85
86static bool set_knots(const FloatArraySamplePtr &knots, float *&nu_knots)
87{
88 if (!knots || knots->size() < 2) {
89 return false;
90 }
91
92 /* Skip first and last knots, as they are used for padding. */
93 const size_t num_knots = knots->size() - 2;
94 nu_knots = MEM_new_array_zeroed<float>(num_knots, "abc_setsplineknotsu");
95
96 for (size_t i = 0; i < num_knots; i++) {
97 nu_knots[i] = (*knots)[i + 1];
98 }
99
100 return true;
101}
102
103void AbcNurbsReader::readObjectData(Main *bmain, const Alembic::Abc::ISampleSelector &sample_sel)
104{
105 Curve *cu = BKE_curve_add(bmain, m_data_name.c_str(), OB_SURF);
106 cu->actvert = CU_ACT_NONE;
107
108 std::vector<std::pair<INuPatchSchema, IObject>>::iterator it;
109
110 for (it = m_schemas.begin(); it != m_schemas.end(); ++it) {
111 Nurb *nu = MEM_new<Nurb>("abc_getnurb");
112 nu->flag = CU_SMOOTH;
113 nu->type = CU_NURBS;
114 nu->resolu = cu->resolu;
115 nu->resolv = cu->resolv;
116
117 const INuPatchSchema &schema = it->first;
118 INuPatchSchema::Sample smp;
119 try {
120 smp = schema.getValue(sample_sel);
121 }
122 catch (Alembic::Util::Exception &ex) {
123 CLOG_WARN(&LOG,
124 "Error reading nurbs sample for '%s/%s' at time %f: %s",
125 m_iobject.getFullName().c_str(),
126 schema.getName().c_str(),
127 sample_sel.getRequestedTime(),
128 ex.what());
129 return;
130 }
131
132 nu->orderu = smp.getUOrder() - 1;
133 nu->orderv = smp.getVOrder() - 1;
134 nu->pntsu = smp.getNumU();
135 nu->pntsv = smp.getNumV();
136
137 /* Read positions and weights. */
138
139 const P3fArraySamplePtr positions = smp.getPositions();
140 const FloatArraySamplePtr weights = smp.getPositionWeights();
141
142 const size_t num_points = positions->size();
143 const bool has_weights = weights && weights->size() >= num_points;
144
145 nu->bp = MEM_new_array_zeroed<BPoint>(num_points, "abc_setsplinetype");
146
147 BPoint *bp = nu->bp;
148 float posw_in = 1.0f;
149
150 for (size_t i = 0; i < num_points; i++, bp++) {
151 const Imath::V3f &pos_in = (*positions)[i];
152
153 if (has_weights) {
154 posw_in = (*weights)[i];
155 }
156
157 copy_zup_from_yup(bp->vec, pos_in.getValue());
158 bp->vec[3] = posw_in;
159 bp->f1 = SELECT;
160 bp->radius = 1.0f;
161 bp->weight = 1.0f;
162 }
163
164 /* Read knots. */
165
166 if (!set_knots(smp.getUKnot(), nu->knotsu)) {
168 }
169
170 if (!set_knots(smp.getVKnot(), nu->knotsv)) {
172 }
173
174 /* Read flags. */
175
176 ICompoundProperty user_props = schema.getUserProperties();
177
178 if (has_property(user_props, "enpoint_u")) {
179 nu->flagu |= CU_NURB_ENDPOINT;
180 }
181
182 if (has_property(user_props, "enpoint_v")) {
183 nu->flagv |= CU_NURB_ENDPOINT;
184 }
185
186 if (has_property(user_props, "cyclic_u")) {
187 nu->flagu |= CU_NURB_CYCLIC;
188 }
189
190 if (has_property(user_props, "cyclic_v")) {
191 nu->flagv |= CU_NURB_CYCLIC;
192 }
193
195 }
196
198 m_object->data = id_cast<ID *>(cu);
199}
200
201void AbcNurbsReader::getNurbsPatches(const IObject &obj)
202{
203 if (!obj.valid()) {
204 return;
205 }
206
207 const int num_children = obj.getNumChildren();
208
209 if (num_children == 0) {
210 INuPatch abc_nurb(obj, kWrapExisting);
211 INuPatchSchema schem = abc_nurb.getSchema();
212 m_schemas.emplace_back(schem, obj);
213 return;
214 }
215
216 for (int i = 0; i < num_children; i++) {
217 bool ok = true;
218 IObject child(obj, obj.getChildHeader(i).getName());
219
220 if (!m_name.empty() && child.valid() && !begins_with(child.getFullName(), m_name)) {
221 ok = false;
222 }
223
224 if (!child.valid()) {
225 continue;
226 }
227
228 const MetaData &md = child.getMetaData();
229
230 if (INuPatch::matches(md) && ok) {
231 INuPatch abc_nurb(child, kWrapExisting);
232 INuPatchSchema schem = abc_nurb.getSchema();
233 m_schemas.emplace_back(schem, child);
234 }
235
236 getNurbsPatches(child);
237 }
238}
239
240} // namespace io::alembic
241} // namespace blender
#define SELECT
Definition BKE_mask.hh:19
General operations, lookup, etc. for blender objects.
#define RPT_(msgid)
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
#define CU_ACT_NONE
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
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 **r_err_str) const override
AbcNurbsReader(const AbcReaderConstructorArgs &args)
AbcObjectReader(const AbcReaderConstructorArgs &args)
bool begins_with(const TContainer &input, const TContainer &match)
Definition abc_util.h:62
BLI_INLINE void copy_zup_from_yup(float zup[3], const float yup[3])
bool has_property(const Alembic::Abc::ICompoundProperty &prop, const std::string &name)
Definition abc_util.cc:82
static bool set_knots(const FloatArraySamplePtr &knots, float *&nu_knots)
static CLG_LogRef LOG
BMesh Iterators.
ListBaseT< Nurb > * BKE_curve_nurbs_get(Curve *cu)
Definition curve.cc:4992
Curve * BKE_curve_add(Main *bmain, const char *name, ObjectType type)
Definition curve.cc:429
void BKE_nurb_knot_calc_v(Nurb *nu)
Definition curve.cc:1219
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
static CLG_LogRef LOG
Object * BKE_object_add_only_object(Main *bmain, ObjectType type, const char *name) ATTR_RETURNS_NONNULL
Definition object.cc:2265
void BKE_nurb_knot_calc_u(Nurb *nu)
Definition curve.cc:1214
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
eNurbKnotFlag flagu
eNurbKnotFlag flagv