Blender V5.3
usd_writer_curves.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <cstdint>
6#include <numeric>
7
8#include <pxr/usd/usdGeom/basisCurves.h>
9#include <pxr/usd/usdGeom/curves.h>
10#include <pxr/usd/usdGeom/nurbsCurves.h>
11#include <pxr/usd/usdGeom/primvar.h>
12#include <pxr/usd/usdGeom/primvarsAPI.h>
13#include <pxr/usd/usdGeom/tokens.h>
14#include <pxr/usd/usdShade/material.h>
15#include <pxr/usd/usdShade/materialBindingAPI.h>
16
19#include "usd_utils.hh"
20#include "usd_writer_curves.hh"
21
22#include "BLI_array_utils.hh"
24#include "BLI_set.hh"
25#include "BLI_span.hh"
26#include "BLI_virtual_array.hh"
27
29#include "BKE_attribute.hh"
31#include "BKE_curves.hh"
32#include "BKE_lib_id.hh"
33#include "BKE_material.hh"
34#include "BKE_report.hh"
35
36#include "BLT_translation.hh"
37
38#include "DNA_curve_types.h"
39#include "DNA_material_types.h"
40#include "DNA_object_types.h"
41
42#include "RNA_access.hh"
43#include "RNA_enum_types.hh"
44
45namespace blender::io::usd {
46
47pxr::UsdGeomBasisCurves USDCurvesWriter::DefineUsdGeomBasisCurves(pxr::VtValue curve_basis,
48 const bool is_cyclic,
49 const bool is_cubic) const
50{
51 pxr::UsdGeomBasisCurves basis_curves = pxr::UsdGeomBasisCurves::Define(
53 /* Not required to set the basis attribute for linear curves
54 * https://graphics.pixar.com/usd/dev/api/class_usd_geom_basis_curves.html#details */
55 if (is_cubic) {
56 basis_curves.CreateTypeAttr(pxr::VtValue(pxr::UsdGeomTokens->cubic));
57 basis_curves.CreateBasisAttr(curve_basis);
58 }
59 else {
60 basis_curves.CreateTypeAttr(pxr::VtValue(pxr::UsdGeomTokens->linear));
61 }
62
63 if (is_cyclic) {
64 basis_curves.CreateWrapAttr(pxr::VtValue(pxr::UsdGeomTokens->periodic));
65 }
66 else if (curve_basis == pxr::VtValue(pxr::UsdGeomTokens->catmullRom)) {
67 /* In Blender the first and last points are treated as endpoints. The pinned attribute tells
68 * the client that to evaluate or render the curve, it must effectively add 'phantom
69 * points' at the beginning and end of every curve in a batch. These phantom points are
70 * injected to ensure that the interpolated curve begins at P[0] and ends at P[n-1]. */
71 basis_curves.CreateWrapAttr(pxr::VtValue(pxr::UsdGeomTokens->pinned));
72 }
73 else {
74 basis_curves.CreateWrapAttr(pxr::VtValue(pxr::UsdGeomTokens->nonperiodic));
75 }
76
77 return basis_curves;
78}
79
80static void populate_curve_widths(const bke::CurvesGeometry &curves, pxr::VtArray<float> &widths)
81{
82 const VArray<float> radii = curves.radius();
83
84 widths.resize(radii.size());
85 for (const int i : radii.index_range()) {
86 widths[i] = radii[i] * 2.0f;
87 }
88}
89
90static pxr::TfToken get_curve_width_interpolation(const pxr::VtArray<float> &widths,
91 const pxr::VtArray<int> &segments,
92 const pxr::VtIntArray &control_point_counts,
93 const bool is_cyclic,
94 ReportList *reports)
95{
96 if (widths.empty()) {
97 return pxr::TfToken();
98 }
99
100 const size_t accumulated_control_point_count = std::accumulate(
101 control_point_counts.begin(), control_point_counts.end(), 0);
102
103 /* For Blender curves, radii are always stored per point. For linear curves, this should match
104 * with USD's vertex interpolation. For cubic curves, this should match with USD's varying
105 * interpolation. */
106 if (widths.size() == accumulated_control_point_count) {
107 return pxr::UsdGeomTokens->vertex;
108 }
109
110 size_t expectedVaryingSize = std::accumulate(segments.begin(), segments.end(), 0);
111 if (!is_cyclic) {
112 expectedVaryingSize += control_point_counts.size();
113 }
114
115 if (widths.size() == expectedVaryingSize) {
116 return pxr::UsdGeomTokens->varying;
117 }
118
119 BKE_report(reports, RPT_WARNING, "Curve width size not supported for USD interpolation");
120 return pxr::TfToken();
121}
122
124 const Span<float3> positions,
125 pxr::VtArray<pxr::GfVec3f> &verts,
126 pxr::VtIntArray &control_point_counts,
127 pxr::VtArray<int> &segments,
128 const bool is_cyclic,
129 const bool is_cubic)
130{
131 const OffsetIndices points_by_curve = curves.points_by_curve();
132 for (const int i_curve : curves.curves_range()) {
133
134 const IndexRange points = points_by_curve[i_curve];
135 for (const int i_point : points) {
136 verts.push_back(
137 pxr::GfVec3f(positions[i_point][0], positions[i_point][1], positions[i_point][2]));
138 }
139
140 const int tot_points = points.size();
141 control_point_counts[i_curve] = tot_points;
142
143 /* For periodic linear curve, segment count = curveVertexCount.
144 * For periodic cubic curve, segment count = curveVertexCount / vstep.
145 * For nonperiodic linear curve, segment count = curveVertexCount - 1.
146 * For nonperiodic cubic curve, segment count = ((curveVertexCount - 4) / vstep) + 1.
147 * This function handles linear and Catmull-Rom curves. For Catmull-Rom, vstep is 1.
148 * https://graphics.pixar.com/usd/dev/api/class_usd_geom_basis_curves.html */
149 if (is_cyclic) {
150 segments[i_curve] = tot_points;
151 }
152 else if (is_cubic) {
153 segments[i_curve] = (tot_points - 4) + 1;
154 }
155 else {
156 segments[i_curve] = tot_points - 1;
157 }
158 }
159}
160
162 pxr::VtArray<pxr::GfVec3f> &verts,
163 pxr::VtIntArray &control_point_counts,
164 pxr::VtArray<float> &widths,
165 pxr::TfToken &interpolation,
166 const bool is_cyclic,
167 const bool is_cubic,
168 ReportList *reports)
169{
170 const int num_curves = curves.curve_num;
171 const Span<float3> positions = curves.positions();
172
173 pxr::VtArray<int> segments(num_curves);
174
176 curves, positions, verts, control_point_counts, segments, is_cyclic, is_cubic);
177
179 interpolation = get_curve_width_interpolation(
180 widths, segments, control_point_counts, is_cyclic, reports);
181}
182
184 const Span<float3> positions,
185 const Span<float3> handles_l,
186 const Span<float3> handles_r,
187 pxr::VtArray<pxr::GfVec3f> &verts,
188 pxr::VtIntArray &control_point_counts,
189 pxr::VtArray<int> &segments,
190 const bool is_cyclic)
191{
192 const int bezier_vstep = 3;
193 const OffsetIndices points_by_curve = curves.points_by_curve();
194
195 for (const int i_curve : curves.curves_range()) {
196
197 const IndexRange points = points_by_curve[i_curve];
198 const int start_point_index = points[0];
199 const int last_point_index = points[points.size() - 1];
200
201 const int start_verts_count = verts.size();
202
203 for (int i_point = start_point_index; i_point < last_point_index; i_point++) {
204
205 /* The order verts in the USD bezier curve representation is [control point 0, right handle
206 * 0, left handle 1, control point 1, right handle 1, left handle 2, control point 2, ...].
207 * The last vert in the array doesn't need a right handle because the curve stops at that
208 * point. */
209 verts.push_back(
210 pxr::GfVec3f(positions[i_point][0], positions[i_point][1], positions[i_point][2]));
211
212 const float3 right_handle = handles_r[i_point];
213 verts.push_back(pxr::GfVec3f(right_handle[0], right_handle[1], right_handle[2]));
214
215 const float3 left_handle = handles_l[i_point + 1];
216 verts.push_back(pxr::GfVec3f(left_handle[0], left_handle[1], left_handle[2]));
217 }
218
219 verts.push_back(pxr::GfVec3f(positions[last_point_index][0],
220 positions[last_point_index][1],
221 positions[last_point_index][2]));
222
223 /* For USD periodic bezier curves, since the curve is closed, we need to include
224 * the right handle of the last point and the left handle of the first point.
225 */
226 if (is_cyclic) {
227 const float3 right_handle = handles_r[last_point_index];
228 verts.push_back(pxr::GfVec3f(right_handle[0], right_handle[1], right_handle[2]));
229
230 const float3 left_handle = handles_l[start_point_index];
231 verts.push_back(pxr::GfVec3f(left_handle[0], left_handle[1], left_handle[2]));
232 }
233
234 const int tot_points = verts.size() - start_verts_count;
235 control_point_counts[i_curve] = tot_points;
236
237 if (is_cyclic) {
238 segments[i_curve] = tot_points / bezier_vstep;
239 }
240 else {
241 segments[i_curve] = ((tot_points - 4) / bezier_vstep) + 1;
242 }
243 }
244}
245
247 pxr::VtArray<pxr::GfVec3f> &verts,
248 pxr::VtIntArray &control_point_counts,
249 pxr::VtArray<float> &widths,
250 pxr::TfToken &interpolation,
251 const bool is_cyclic,
252 ReportList *reports)
253{
254 const int num_curves = curves.curve_num;
255
256 const Span<float3> positions = curves.positions();
257 const std::optional<Span<float3>> handles_l = curves.handle_positions_left();
258 const std::optional<Span<float3>> handles_r = curves.handle_positions_right();
259
260 pxr::VtArray<int> segments(num_curves);
261
263 positions,
264 handles_l.value_or(Span<float3>{}),
265 handles_r.value_or(Span<float3>{}),
266 verts,
267 control_point_counts,
268 segments,
269 is_cyclic);
270
272 interpolation = get_curve_width_interpolation(
273 widths, segments, control_point_counts, is_cyclic, reports);
274}
275
277 pxr::VtArray<pxr::GfVec3f> &verts,
278 pxr::VtIntArray &control_point_counts,
279 pxr::VtArray<float> &widths,
280 pxr::VtArray<double> &knots,
281 pxr::VtArray<double> &weights,
282 pxr::VtArray<int> &orders,
283 pxr::TfToken &interpolation,
284 const bool is_cyclic)
285{
286 /* Order and range, when representing a batched NurbsCurve should be authored one value per
287 * curve. */
288 const int num_curves = curves.curve_num;
289 orders.resize(num_curves);
290
291 const Span<float3> positions = curves.positions();
292 const Span<float> custom_knots = curves.nurbs_custom_knots();
293 const std::optional<Span<float>> nurbs_weights = curves.nurbs_weights();
294
295 VArray<int8_t> geom_orders = curves.nurbs_orders();
296 VArray<int8_t> knots_modes = curves.nurbs_knots_modes();
297 const VArray<float> radii = curves.radius();
298
299 const OffsetIndices points_by_curve = curves.points_by_curve();
300 const OffsetIndices custom_knots_by_curve = curves.nurbs_custom_knots_by_curve();
301 for (const int i_curve : curves.curves_range()) {
302 const IndexRange points = points_by_curve[i_curve];
303 const size_t curr_vert_num = verts.size();
304 for (const int i_point : points) {
305 verts.push_back(
306 pxr::GfVec3f(positions[i_point][0], positions[i_point][1], positions[i_point][2]));
307 widths.push_back(radii[i_point] * 2.0f);
308 }
309
310 if (nurbs_weights) {
311 for (const int i_point : points) {
312 weights.push_back((*nurbs_weights)[i_point]);
313 }
314 }
315
316 /* Repeat the first degree(order - 1) number of points and weights if curve is cyclic. */
317 if (is_cyclic) {
318 for (const int i_point : points.take_front(geom_orders[i_curve] - 1)) {
319 verts.push_back(
320 pxr::GfVec3f(positions[i_point][0], positions[i_point][1], positions[i_point][2]));
321 widths.push_back(radii[i_point] * 2.0f);
322 if (nurbs_weights) {
323 weights.push_back((*nurbs_weights)[i_point]);
324 }
325 }
326 }
327
328 const int tot_blender_points = int(points.size());
329 const int tot_usd_points = int(verts.size() - curr_vert_num);
330 control_point_counts[i_curve] = tot_usd_points;
331
332 const int8_t order = geom_orders[i_curve];
333 orders[i_curve] = int(geom_orders[i_curve]);
334
335 const KnotsMode mode = KnotsMode(knots_modes[i_curve]);
336
337 const int knots_num = bke::curves::nurbs::knots_num(tot_blender_points, order, is_cyclic);
338 Array<float> temp_knots(knots_num);
340 tot_blender_points,
341 order,
342 is_cyclic,
343 custom_knots_by_curve[i_curve],
344 custom_knots,
345 temp_knots);
346
347 /* Knots should be the concatenation of all batched curves.
348 * https://graphics.pixar.com/usd/dev/api/class_usd_geom_nurbs_curves.html#details */
349 for (int i_knot = 0; i_knot < knots_num; i_knot++) {
350 knots.push_back(double(temp_knots[i_knot]));
351 }
352
353 /* For USD it is required to set specific end knots for periodic/non-periodic curves
354 * https://graphics.pixar.com/usd/dev/api/class_usd_geom_nurbs_curves.html#details */
355 int zeroth_knot_index = knots.size() - knots_num;
356 if (is_cyclic) {
357 knots[zeroth_knot_index] = knots[zeroth_knot_index + 1] -
358 (knots[knots.size() - 2] - knots[knots.size() - 3]);
359 knots[knots.size() - 1] = knots[knots.size() - 2] +
360 (knots[zeroth_knot_index + 2] - knots[zeroth_knot_index + 1]);
361 }
362 else {
363 knots[zeroth_knot_index] = knots[zeroth_knot_index + 1];
364 knots[knots.size() - 1] = knots[knots.size() - 2];
365 }
366 }
367
368 interpolation = pxr::UsdGeomTokens->vertex;
369}
370
371void USDCurvesWriter::set_writer_attributes_for_nurbs(
372 const pxr::UsdGeomNurbsCurves &usd_nurbs_curves,
373 pxr::VtArray<double> &knots,
374 pxr::VtArray<double> &weights,
375 pxr::VtArray<int> &orders,
376 const pxr::UsdTimeCode time)
377{
378 pxr::UsdAttribute attr_knots = usd_nurbs_curves.CreateKnotsAttr(pxr::VtValue(), true);
379 set_attribute(attr_knots, knots, time, usd_value_writer_);
380 pxr::UsdAttribute attr_weights = usd_nurbs_curves.CreatePointWeightsAttr(pxr::VtValue(), true);
381 set_attribute(attr_weights, weights, time, usd_value_writer_);
382 pxr::UsdAttribute attr_order = usd_nurbs_curves.CreateOrderAttr(pxr::VtValue(), true);
383 set_attribute(attr_order, orders, time, usd_value_writer_);
384}
385
386void USDCurvesWriter::set_writer_attributes(pxr::UsdGeomCurves &usd_curves,
387 pxr::VtArray<pxr::GfVec3f> &verts,
388 pxr::VtIntArray &control_point_counts,
389 pxr::VtArray<float> &widths,
390 const pxr::UsdTimeCode time,
391 const pxr::TfToken interpolation)
392{
393 pxr::UsdAttribute attr_points = usd_curves.CreatePointsAttr(pxr::VtValue(), true);
394 set_attribute(attr_points, verts, time, usd_value_writer_);
395
396 pxr::UsdAttribute attr_counts = usd_curves.CreateCurveVertexCountsAttr(pxr::VtValue(), true);
397 set_attribute(attr_counts, control_point_counts, time, usd_value_writer_);
398
399 pxr::UsdAttribute attr_widths = usd_curves.CreateWidthsAttr(pxr::VtValue(), true);
400 set_attribute(attr_widths, widths, time, usd_value_writer_);
401
402 if (!interpolation.IsEmpty()) {
403 usd_curves.SetWidthsInterpolation(interpolation);
404 }
405}
406
407static std::optional<pxr::TfToken> convert_blender_domain_to_usd(
408 const bke::AttrDomain blender_domain, bool is_bezier)
409{
410 switch (blender_domain) {
412 return is_bezier ? pxr::UsdGeomTokens->varying : pxr::UsdGeomTokens->vertex;
414 return pxr::UsdGeomTokens->uniform;
415
416 default:
417 return std::nullopt;
418 }
419}
420
421/* Excluded attributes are those which are handled through native USD concepts
422 * and should not be exported as generic attributes. */
424{
425 static const Set<StringRefNull> excluded_attrs = []() {
427 set.add_new("position");
428 set.add_new("radius");
429 set.add_new("resolution");
430 set.add_new("id");
431 set.add_new("cyclic");
432 set.add_new("curve_type");
433 set.add_new("normal_mode");
434 set.add_new("handle_left");
435 set.add_new("handle_right");
436 set.add_new("handle_type_left");
437 set.add_new("handle_type_right");
438 set.add_new("knots_mode");
439 set.add_new("nurbs_order");
440 set.add_new("nurbs_weight");
441 set.add_new("velocity");
442 return set;
443 }();
444
445 return excluded_attrs.contains(name);
446}
447
448void USDCurvesWriter::write_generic_data(const bke::CurvesGeometry &curves,
449 const bke::AttributeIter &attr,
450 const pxr::UsdGeomCurves &usd_curves)
451{
452 const CurveType curve_type = CurveType(curves.curve_types().first());
453 const bool is_bezier = curve_type == CURVE_TYPE_BEZIER;
454
455 const std::optional<pxr::TfToken> pv_interp = convert_blender_domain_to_usd(attr.domain,
456 is_bezier);
457 const std::optional<pxr::SdfValueTypeName> pv_type = convert_blender_type_to_usd(attr.data_type);
458
459 if (!pv_interp || !pv_type) {
460 BKE_reportf(this->reports(),
462 "Attribute '%s' (Blender domain %d, type %d) cannot be converted to USD",
463 attr.name.c_str(),
464 int8_t(attr.domain),
465 int(attr.data_type));
466 return;
467 }
468
469 const GVArray attribute = *attr.get();
470 if (attribute.is_empty()) {
471 return;
472 }
473
474 const pxr::UsdTimeCode time = get_export_time_code();
475 const pxr::TfToken pv_name(
477 const pxr::UsdGeomPrimvarsAPI pv_api = pxr::UsdGeomPrimvarsAPI(usd_curves);
478
479 pxr::UsdGeomPrimvar pv_attr = pv_api.CreatePrimvar(pv_name, *pv_type, *pv_interp);
480
482}
483
484void USDCurvesWriter::write_uv_data(const bke::AttributeIter &attr,
485 const pxr::UsdGeomCurves &usd_curves)
486{
487 const VArray<float2> buffer = *attr.get<float2>(bke::AttrDomain::Curve);
488 if (buffer.is_empty()) {
489 return;
490 }
491
492 const pxr::UsdTimeCode time = get_export_time_code();
493 const pxr::TfToken pv_name(
495 const pxr::UsdGeomPrimvarsAPI pv_api = pxr::UsdGeomPrimvarsAPI(usd_curves);
496
497 pxr::UsdGeomPrimvar pv_uv = pv_api.CreatePrimvar(
498 pv_name, pxr::SdfValueTypeNames->TexCoord2fArray, pxr::UsdGeomTokens->uniform);
499
501}
502
503void USDCurvesWriter::write_velocities(const bke::CurvesGeometry &curves,
504 const pxr::UsdGeomCurves &usd_curves)
505{
506 const VArraySpan velocity = *curves.attributes().lookup<float3>("velocity",
508 if (velocity.is_empty()) {
509 return;
510 }
511
512 /* Export per-vertex velocity vectors. */
513 Span<pxr::GfVec3f> data = velocity.cast<pxr::GfVec3f>();
514 pxr::VtVec3fArray usd_velocities;
515 usd_velocities.assign(data.begin(), data.end());
516
517 pxr::UsdTimeCode time = get_export_time_code();
518 pxr::UsdAttribute attr_vel = usd_curves.CreateVelocitiesAttr(pxr::VtValue(), true);
519 set_attribute(attr_vel, usd_velocities, time, usd_value_writer_);
520}
521
522void USDCurvesWriter::write_custom_data(const bke::CurvesGeometry &curves,
523 const pxr::UsdGeomCurves &usd_curves)
524{
525 const bke::AttributeAccessor attributes = curves.attributes();
526
527 attributes.foreach_attribute([&](const bke::AttributeIter &iter) {
528 /* Skip "internal" Blender properties and attributes dealt with elsewhere. */
529 if (iter.name[0] == '.' || bke::attribute_name_is_anonymous(iter.name) ||
530 is_excluded_attr(iter.name))
531 {
532 return;
533 }
534
535 /* Spline UV data */
536 if (iter.domain == bke::AttrDomain::Curve && iter.data_type == bke::AttrType::Float2) {
538 this->write_uv_data(iter, usd_curves);
539 }
540 }
541
542 /* Everything else. */
543 else {
544 this->write_generic_data(curves, iter, usd_curves);
545 }
546 });
547}
548
550{
551 Curves *curves_id;
552 std::unique_ptr<Curves, std::function<void(Curves *)>> converted_curves;
553 int8_t curve_type_default = CURVE_TYPE_CATMULL_ROM;
554
555 switch (context.object->type) {
556 case OB_CURVES_LEGACY: {
557 const Curve *legacy_curve = id_cast<Curve *>(context.object->data);
558 converted_curves = std::unique_ptr<Curves, std::function<void(Curves *)>>(
559 bke::curve_legacy_to_curves(*legacy_curve), [](Curves *c) { BKE_id_free(nullptr, c); });
560 curves_id = converted_curves.get();
561 curve_type_default = CURVE_TYPE_BEZIER;
562 break;
563 }
564 case OB_CURVES:
565 curves_id = id_cast<Curves *>(context.object->data);
566 break;
567 default:
569 return;
570 }
571
572 const bke::CurvesGeometry empty;
573 const bke::CurvesGeometry &curves = curves_id ? curves_id->geometry.wrap() : empty;
574
575 const std::array<int, CURVE_TYPES_NUM> &curve_type_counts = curves.curve_type_counts();
576 const int number_of_curve_types = std::count_if(curve_type_counts.begin(),
577 curve_type_counts.end(),
578 [](const int count) { return count > 0; });
579 if (number_of_curve_types > 1) {
581 reports(), RPT_WARNING, "Cannot export mixed curve types in the same Curves object");
582 return;
583 }
584
588 "Cannot export mixed cyclic and non-cyclic curves in the same Curves object");
589 return;
590 }
591
592 const pxr::UsdTimeCode time = get_export_time_code();
593 const int8_t curve_type_fallback = first_frame_curve_type == -1 ? curve_type_default :
594 first_frame_curve_type;
595 const int8_t curve_type = curves.curves_num() > 0 ? curves.curve_types()[0] :
596 curve_type_fallback;
597
598 if (first_frame_curve_type == -1) {
599 first_frame_curve_type = curve_type;
600 }
601 else if (first_frame_curve_type != curve_type) {
602 const char *first_frame_curve_type_name = nullptr;
604 rna_enum_curves_type_items, int(first_frame_curve_type), &first_frame_curve_type_name);
605
606 const char *current_curve_type_name = nullptr;
608 rna_enum_curves_type_items, int(curve_type), &current_curve_type_name);
609
612 "USD does not support animating curve types. The curve type changes from %s to "
613 "%s on frame %f",
614 IFACE_(first_frame_curve_type_name),
615 IFACE_(current_curve_type_name),
616 time.GetValue());
617 return;
618 }
619
620 const bool is_cyclic = curves.curves_num() > 0 ? curves.cyclic().first() : false;
621 pxr::VtArray<pxr::GfVec3f> verts;
622 pxr::VtIntArray control_point_counts;
623 pxr::VtArray<float> widths;
624 pxr::TfToken interpolation;
625
626 pxr::UsdGeomBasisCurves usd_basis_curves;
627 pxr::UsdGeomNurbsCurves usd_nurbs_curves;
628 pxr::UsdGeomCurves *usd_curves = nullptr;
629
630 control_point_counts.resize(curves.curves_num());
631 switch (curve_type) {
632 case CURVE_TYPE_POLY:
633 usd_basis_curves = DefineUsdGeomBasisCurves(pxr::VtValue(), is_cyclic, false);
634 usd_curves = &usd_basis_curves;
635
637 curves, verts, control_point_counts, widths, interpolation, is_cyclic, false, reports());
638 break;
640 usd_basis_curves = DefineUsdGeomBasisCurves(
641 pxr::VtValue(pxr::UsdGeomTokens->catmullRom), is_cyclic, true);
642 usd_curves = &usd_basis_curves;
643
645 curves, verts, control_point_counts, widths, interpolation, is_cyclic, true, reports());
646 break;
648 usd_basis_curves = DefineUsdGeomBasisCurves(
649 pxr::VtValue(pxr::UsdGeomTokens->bezier), is_cyclic, true);
650 usd_curves = &usd_basis_curves;
651
653 curves, verts, control_point_counts, widths, interpolation, is_cyclic, reports());
654 break;
655 case CURVE_TYPE_NURBS: {
656 pxr::VtArray<double> knots;
657 pxr::VtArray<double> weights;
658 pxr::VtArray<int> orders;
659
660 usd_nurbs_curves = pxr::UsdGeomNurbsCurves::Define(usd_export_context_.stage,
662 usd_curves = &usd_nurbs_curves;
663
665 verts,
666 control_point_counts,
667 widths,
668 knots,
669 weights,
670 orders,
671 interpolation,
672 is_cyclic);
673
674 set_writer_attributes_for_nurbs(usd_nurbs_curves, knots, weights, orders, time);
675
676 break;
677 }
678 default:
680 }
681
682 this->set_writer_attributes(
683 *usd_curves, verts, control_point_counts, widths, time, interpolation);
684
685 this->assign_materials(context, *usd_curves);
686
687 /* TODO: We cannot write custom primvars for cyclic NURBS curves at the moment. */
688 if (!is_cyclic || (is_cyclic && curve_type != CURVE_TYPE_NURBS)) {
689 this->write_velocities(curves, *usd_curves);
690 this->write_custom_data(curves, *usd_curves);
691 }
692
693 if (curves_id) {
694 const pxr::UsdPrim prim = usd_curves->GetPrim();
695 add_to_prim_map(prim.GetPath(), &curves_id->id);
696 write_id_properties(prim, curves_id->id, time);
697 }
698
699 this->author_extent(*usd_curves, curves.bounds_min_max(), time);
700}
701
703 const pxr::UsdGeomCurves &usd_curves)
704{
706 return;
707 }
708
709 const int totcol = BKE_object_material_count_eval(context.object);
710 if (totcol == 0) {
711 return;
712 }
713
714 bool curve_material_bound = false;
715 for (int mat_num = 0; mat_num < totcol; mat_num++) {
716 Material *material = BKE_object_material_get_eval(context.object, mat_num + 1);
717 if (material == nullptr) {
718 continue;
719 }
720
721 pxr::UsdPrim curve_prim = usd_curves.GetPrim();
722 pxr::UsdShadeMaterialBindingAPI api = pxr::UsdShadeMaterialBindingAPI(curve_prim);
723 pxr::UsdShadeMaterial usd_material = ensure_usd_material(context, material);
724 api.Bind(usd_material);
725 pxr::UsdShadeMaterialBindingAPI::Apply(curve_prim);
726
727 /* USD seems to support neither per-material nor per-face-group double-sidedness, so we just
728 * use the flag from the first non-empty material slot. */
729 usd_curves.CreateDoubleSidedAttr(
730 pxr::VtValue((material->blend_flag & MA_BL_CULL_BACKFACE) == 0));
731
732 curve_material_bound = true;
733 break;
734 }
735
736 if (!curve_material_bound) {
737 /* Blender defaults to double-sided, but USD to single-sided. */
738 usd_curves.CreateDoubleSidedAttr(pxr::VtValue(true));
739 }
740}
741
742} // namespace blender::io::usd
Low-level operations for curves.
General operations, lookup, etc. for materials.
#define IFACE_(msgid)
Object is a sort of wrapper for general info.
#define BLI_assert_unreachable()
constexpr int64_t size() const
constexpr IndexRange take_front(int64_t n) const
bool contains(const Key &key) const
Definition BLI_set.hh:310
void add_new(const Key &key)
Definition BLI_set.hh:233
constexpr const char * c_str() const
IndexRange index_range() const
GAttributeReader get() const
constexpr bool is_empty() const
pxr::UsdShadeMaterial ensure_usd_material(const HierarchyContext &context, Material *material) const
void author_extent(const pxr::UsdGeomBoundable &boundable, const pxr::UsdTimeCode time)
void add_to_prim_map(const pxr::SdfPath &usd_path, const ID *id) const
pxr::UsdTimeCode get_export_time_code() const
pxr::UsdUtilsSparseValueWriter usd_value_writer_
void write_id_properties(const pxr::UsdPrim &prim, const ID &id, pxr::UsdTimeCode=pxr::UsdTimeCode::Default()) const
const USDExporterContext usd_export_context_
void do_write(HierarchyContext &context) override
void assign_materials(const HierarchyContext &context, const pxr::UsdGeomCurves &usd_curves)
#define uniform(slot)
#define attribute(slot)
BooleanMix booleans_mix_calc(const VArray< bool > &varray, IndexRange range_to_check)
int knots_num(int points_num, int8_t order, bool cyclic)
void load_curve_knots(KnotsMode mode, int points_num, int8_t order, bool cyclic, IndexRange curve_knots, Span< float > custom_knots, MutableSpan< float > knots)
bool attribute_name_is_anonymous(const StringRef name)
Curves * curve_legacy_to_curves(const Curve &curve_legacy)
std::string time(const std::tm &date_time, TimeFormat format=TimeFormat::H24)
static pxr::TfToken get_curve_width_interpolation(const pxr::VtArray< float > &widths, const pxr::VtArray< int > &segments, const pxr::VtIntArray &control_point_counts, const bool is_cyclic, ReportList *reports)
void copy_blender_attribute_to_primvar(const GVArray &attribute, const bke::AttrType data_type, const pxr::UsdTimeCode time, const pxr::UsdGeomPrimvar &primvar, pxr::UsdUtilsSparseValueWriter &value_writer)
static bool is_excluded_attr(StringRefNull name)
std::optional< pxr::SdfValueTypeName > convert_blender_type_to_usd(const bke::AttrType blender_type, bool use_color3f_type)
void copy_blender_buffer_to_primvar(const VArray< BlenderT > &buffer, const pxr::UsdTimeCode time, const pxr::UsdGeomPrimvar &primvar, pxr::UsdUtilsSparseValueWriter &value_writer)
static void populate_curve_props_for_bezier(const bke::CurvesGeometry &curves, pxr::VtArray< pxr::GfVec3f > &verts, pxr::VtIntArray &control_point_counts, pxr::VtArray< float > &widths, pxr::TfToken &interpolation, const bool is_cyclic, ReportList *reports)
static std::optional< pxr::TfToken > convert_blender_domain_to_usd(const bke::AttrDomain blender_domain, bool is_bezier)
static void populate_curve_verts(const bke::CurvesGeometry &curves, const Span< float3 > positions, pxr::VtArray< pxr::GfVec3f > &verts, pxr::VtIntArray &control_point_counts, pxr::VtArray< int > &segments, const bool is_cyclic, const bool is_cubic)
static void populate_curve_props(const bke::CurvesGeometry &curves, pxr::VtArray< pxr::GfVec3f > &verts, pxr::VtIntArray &control_point_counts, pxr::VtArray< float > &widths, pxr::TfToken &interpolation, const bool is_cyclic, const bool is_cubic, ReportList *reports)
static void populate_curve_verts_for_bezier(const bke::CurvesGeometry &curves, const Span< float3 > positions, const Span< float3 > handles_l, const Span< float3 > handles_r, pxr::VtArray< pxr::GfVec3f > &verts, pxr::VtIntArray &control_point_counts, pxr::VtArray< int > &segments, const bool is_cyclic)
void set_attribute(const pxr::UsdAttribute &attr, const USDT value, pxr::UsdTimeCode time, pxr::UsdUtilsSparseValueWriter &value_writer)
static void populate_curve_widths(const bke::CurvesGeometry &curves, pxr::VtArray< float > &widths)
static void populate_curve_props_for_nurbs(const bke::CurvesGeometry &curves, pxr::VtArray< pxr::GfVec3f > &verts, pxr::VtIntArray &control_point_counts, pxr::VtArray< float > &widths, pxr::VtArray< double > &knots, pxr::VtArray< double > &weights, pxr::VtArray< int > &orders, pxr::TfToken &interpolation, const bool is_cyclic)
std::string make_safe_primvar_name(const StringRef name, bool allow_unicode)
Definition usd_utils.cc:88
int BKE_object_material_count_eval(const Object *ob)
@ RPT_WARNING
Definition BKE_report.hh:40
const EnumPropertyItem rna_enum_curves_type_items[]
Definition rna_curves.cc:26
VecBase< float, 2 > float2
bool RNA_enum_name_from_value(const EnumPropertyItem *item, int value, const char **r_name)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
BMesh const char void * data
void BKE_id_free(Main *bmain, void *idv)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
@ CURVE_TYPE_CATMULL_ROM
Material * BKE_object_material_get_eval(Object *ob, short act)
VecBase< float, 3 > float3
signed char int8_t
Definition BKE_text.h:76