Blender V4.3
usd_writer_hair.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4#include "usd_writer_hair.hh"
6
7#include <pxr/usd/usdGeom/basisCurves.h>
8#include <pxr/usd/usdGeom/tokens.h>
9
10#include "BKE_particle.h"
11
12#include "DNA_particle_types.h"
13
14namespace blender::io::usd {
15
17
19{
20 ParticleSystem *psys = context.particle_system;
21 ParticleCacheKey **cache = psys->pathcache;
22 if (cache == nullptr) {
23 return;
24 }
25
26 pxr::UsdTimeCode timecode = get_export_time_code();
27 pxr::UsdGeomBasisCurves curves = pxr::UsdGeomBasisCurves::Define(usd_export_context_.stage,
29
30 /* TODO(Sybren): deal with (psys->part->flag & PART_HAIR_BSPLINE) */
31 curves.CreateBasisAttr(pxr::VtValue(pxr::UsdGeomTokens->bspline));
32 curves.CreateTypeAttr(pxr::VtValue(pxr::UsdGeomTokens->cubic));
33
34 pxr::VtArray<pxr::GfVec3f> points;
35 pxr::VtIntArray curve_point_counts;
36 curve_point_counts.reserve(psys->totpart);
37
38 ParticleCacheKey *strand;
39 for (int strand_index = 0; strand_index < psys->totpart; ++strand_index) {
40 strand = cache[strand_index];
41
42 int point_count = strand->segments + 1;
43 curve_point_counts.push_back(point_count);
44
45 for (int point_index = 0; point_index < point_count; ++point_index, ++strand) {
46 points.push_back(pxr::GfVec3f(strand->co));
47 }
48 }
49
50 pxr::UsdAttribute attr_points = curves.CreatePointsAttr(pxr::VtValue(), true);
51 pxr::UsdAttribute attr_vertex_counts = curves.CreateCurveVertexCountsAttr(pxr::VtValue(), true);
52 if (!attr_points.HasValue()) {
53 attr_points.Set(points, pxr::UsdTimeCode::Default());
54 attr_vertex_counts.Set(curve_point_counts, pxr::UsdTimeCode::Default());
55 }
56 usd_value_writer_.SetAttribute(attr_points, pxr::VtValue(points), timecode);
57 usd_value_writer_.SetAttribute(attr_vertex_counts, pxr::VtValue(curve_point_counts), timecode);
58
59 if (psys->totpart > 0) {
60 pxr::VtArray<pxr::GfVec3f> colors;
61 colors.push_back(pxr::GfVec3f(cache[0]->col));
62 curves.CreateDisplayColorAttr(pxr::VtValue(colors));
63 }
64
65 if (psys->part) {
66 auto prim = curves.GetPrim();
67 write_id_properties(prim, psys->part->id, timecode);
68 }
69
70 this->author_extent(timecode, curves);
71}
72
74{
75 return true;
76}
77
78} // namespace blender::io::usd
virtual void author_extent(const pxr::UsdTimeCode timecode, pxr::UsdGeomBoundable &prim)
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_
virtual bool check_is_animated(const HierarchyContext &context) const override
virtual void do_write(HierarchyContext &context) override
USDHairWriter(const USDExporterContext &ctx)
uint col
static int point_count(int usdCount, CurveType curve_type, bool is_cyclic)
ParticleSettings * part
struct ParticleCacheKey ** pathcache