Blender V5.3
abc_writer_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_writer_nurbs.h"
11
12#include "DNA_curve_types.h"
13#include "DNA_object_types.h"
14
15#include "BLI_listbase.hh"
16
17#include "BKE_curve.hh"
18#include "BKE_object_types.hh"
19
20#include "CLG_log.h"
21
22namespace blender {
23
24static CLG_LogRef LOG = {"io.alembic"};
25
26namespace io::alembic {
27
28using Alembic::Abc::OObject;
29using Alembic::AbcGeom::FloatArraySample;
30using Alembic::AbcGeom::OBoolProperty;
31using Alembic::AbcGeom::OCompoundProperty;
32using Alembic::AbcGeom::ONuPatch;
33using Alembic::AbcGeom::ONuPatchSchema;
34
36
38{
39 Curve *curve = id_cast<Curve *>(context->object->data);
40 size_t num_nurbs = curve->nurb.count();
41 OObject abc_parent = args_.abc_parent;
42 const char *abc_parent_path = abc_parent.getFullName().c_str();
43
44 for (size_t i = 0; i < num_nurbs; i++) {
45 std::stringstream patch_name_stream;
46 patch_name_stream << args_.abc_name << '_' << i;
47
48 while (abc_parent.getChildHeader(patch_name_stream.str())) {
49 patch_name_stream << "_";
50 }
51
52 std::string patch_name = patch_name_stream.str();
53 CLOG_DEBUG(&LOG, "exporting %s/%s", abc_parent_path, patch_name.c_str());
54
55 ONuPatch nurbs(abc_parent, patch_name, timesample_index_);
56 abc_nurbs_.push_back(nurbs);
57 abc_nurbs_schemas_.push_back(nurbs.getSchema());
58 }
59}
60
62{
63 if (abc_nurbs_.empty()) {
64 return OObject();
65 }
66 /* For parenting purposes within the Alembic file, all NURBS patches are equal, so just use the
67 * first one. */
68 return abc_nurbs_[0];
69}
70
71Alembic::Abc::OCompoundProperty ABCNurbsWriter::abc_prop_for_custom_props()
72{
73 if (abc_nurbs_.empty()) {
74 return Alembic::Abc::OCompoundProperty();
75 }
76
77 /* A single NURBS object in Blender is expanded to multiple curves in Alembic.
78 * Just store the custom properties on the first one for simplicity. */
79 return abc_schema_prop_for_custom_props(abc_nurbs_schemas_[0]);
80}
81
83{
84 /* Check if object has shape keys. */
85 Curve *cu = id_cast<Curve *>(context.object->data);
86 return (cu->key != nullptr);
87}
88
90{
91 return ELEM(context->object->type, OB_SURF, OB_CURVES_LEGACY);
92}
93
94static void get_knots(std::vector<float> &knots, const int num_knots, float *nu_knots)
95{
96 if (num_knots <= 1) {
97 return;
98 }
99
100 /* Add an extra knot at the beginning and end of the array since most apps
101 * require/expect them. */
102 knots.reserve(num_knots + 2);
103
104 knots.push_back(0.0f);
105
106 for (int i = 0; i < num_knots; i++) {
107 knots.push_back(nu_knots[i]);
108 }
109
110 knots[0] = 2.0f * knots[1] - knots[2];
111 knots.push_back(2.0f * knots[num_knots] - knots[num_knots - 1]);
112}
113
115{
116 Curve *curve = id_cast<Curve *>(context.object->data);
117 ListBaseT<Nurb> *nulb;
118
119 if (context.object->runtime->curve_cache->deformed_nurbs.first_ != nullptr) {
120 nulb = &context.object->runtime->curve_cache->deformed_nurbs;
121 }
122 else {
123 nulb = BKE_curve_nurbs_get(curve);
124 }
125
126 size_t count = 0;
127 for (Nurb *nu = nulb->first(); nu; nu = nu->next, count++) {
128 std::vector<float> knotsU;
129 get_knots(knotsU, KNOTSU(nu), nu->knotsu);
130
131 std::vector<float> knotsV;
132 get_knots(knotsV, KNOTSV(nu), nu->knotsv);
133
134 const int size = nu->pntsu * nu->pntsv;
135 std::vector<Imath::V3f> positions(size);
136 std::vector<float> weights(size);
137
138 const BPoint *bp = nu->bp;
139
140 for (int i = 0; i < size; i++, bp++) {
141 copy_yup_from_zup(positions[i].getValue(), bp->vec);
142 weights[i] = bp->vec[3];
143 }
144
145 ONuPatchSchema::Sample sample;
146 sample.setUOrder(nu->orderu + 1);
147 sample.setVOrder(nu->orderv + 1);
148 sample.setPositions(positions);
149 sample.setPositionWeights(weights);
150 sample.setUKnot(FloatArraySample(knotsU));
151 sample.setVKnot(FloatArraySample(knotsV));
152 sample.setNu(nu->pntsu);
153 sample.setNv(nu->pntsv);
154
155 /* TODO(kevin): to accommodate other software we should duplicate control
156 * points to indicate that a NURBS is cyclic. */
157 OCompoundProperty user_props = abc_nurbs_schemas_[count].getUserProperties();
158
159 if ((nu->flagu & CU_NURB_ENDPOINT) != 0) {
160 OBoolProperty prop(user_props, "endpoint_u");
161 prop.set(true);
162 }
163
164 if ((nu->flagv & CU_NURB_ENDPOINT) != 0) {
165 OBoolProperty prop(user_props, "endpoint_v");
166 prop.set(true);
167 }
168
169 if ((nu->flagu & CU_NURB_CYCLIC) != 0) {
170 OBoolProperty prop(user_props, "cyclic_u");
171 prop.set(true);
172 }
173
174 if ((nu->flagv & CU_NURB_CYCLIC) != 0) {
175 OBoolProperty prop(user_props, "cyclic_v");
176 prop.set(true);
177 }
178
179 abc_nurbs_schemas_[count].set(sample);
180 }
181}
182
183} // namespace io::alembic
184} // namespace blender
#define KNOTSU(nu)
Definition BKE_curve.hh:78
#define KNOTSV(nu)
Definition BKE_curve.hh:80
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
Object is a sort of wrapper for general info.
#define ELEM(...)
ABCAbstractWriter(const ABCWriterConstructorArgs &args)
Alembic::Abc::OCompoundProperty abc_schema_prop_for_custom_props(T abc_schema)
const ABCWriterConstructorArgs args_
bool check_is_animated(const HierarchyContext &context) const override
bool is_supported(const HierarchyContext *context) const override
void create_alembic_objects(const HierarchyContext *context) override
ABCNurbsWriter(const ABCWriterConstructorArgs &args)
void do_write(HierarchyContext &context) override
Alembic::Abc::OObject get_alembic_object() const override
Alembic::Abc::OCompoundProperty abc_prop_for_custom_props() override
static void get_knots(std::vector< float > &knots, const int num_knots, float *nu_knots)
BLI_INLINE void copy_yup_from_zup(float yup[3], const float zup[3])
BMesh Iterators.
ListBaseT< Nurb > * BKE_curve_nurbs_get(Curve *cu)
Definition curve.cc:4992
static CLG_LogRef LOG
Definition logging.cpp:12
ListBaseT< Nurb > nurb
struct Key * key
struct Nurb * next