Blender  V2.93
usd_writer_hair.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2019 Blender Foundation.
17  * All rights reserved.
18  */
19 #include "usd_writer_hair.h"
20 #include "usd_hierarchy_iterator.h"
21 
22 #include <pxr/usd/usdGeom/basisCurves.h>
23 #include <pxr/usd/usdGeom/tokens.h>
24 
25 #include "BKE_particle.h"
26 
27 #include "DNA_particle_types.h"
28 
29 namespace blender::io::usd {
30 
32 {
33 }
34 
36 {
37  ParticleSystem *psys = context.particle_system;
38  ParticleCacheKey **cache = psys->pathcache;
39  if (cache == nullptr) {
40  return;
41  }
42 
43  pxr::UsdTimeCode timecode = get_export_time_code();
44  pxr::UsdGeomBasisCurves curves = pxr::UsdGeomBasisCurves::Define(usd_export_context_.stage,
46 
47  /* TODO(Sybren): deal with (psys->part->flag & PART_HAIR_BSPLINE) */
48  curves.CreateBasisAttr(pxr::VtValue(pxr::UsdGeomTokens->bspline));
49  curves.CreateTypeAttr(pxr::VtValue(pxr::UsdGeomTokens->cubic));
50 
51  pxr::VtArray<pxr::GfVec3f> points;
52  pxr::VtIntArray curve_point_counts;
53  curve_point_counts.reserve(psys->totpart);
54 
55  ParticleCacheKey *strand;
56  for (int strand_index = 0; strand_index < psys->totpart; ++strand_index) {
57  strand = cache[strand_index];
58 
59  int point_count = strand->segments + 1;
60  curve_point_counts.push_back(point_count);
61 
62  for (int point_index = 0; point_index < point_count; ++point_index, ++strand) {
63  points.push_back(pxr::GfVec3f(strand->co));
64  }
65  }
66 
67  pxr::UsdAttribute attr_points = curves.CreatePointsAttr(pxr::VtValue(), true);
68  pxr::UsdAttribute attr_vertex_counts = curves.CreateCurveVertexCountsAttr(pxr::VtValue(), true);
69  if (!attr_points.HasValue()) {
70  attr_points.Set(points, pxr::UsdTimeCode::Default());
71  attr_vertex_counts.Set(curve_point_counts, pxr::UsdTimeCode::Default());
72  }
73  usd_value_writer_.SetAttribute(attr_points, pxr::VtValue(points), timecode);
74  usd_value_writer_.SetAttribute(attr_vertex_counts, pxr::VtValue(curve_point_counts), timecode);
75 
76  if (psys->totpart > 0) {
77  pxr::VtArray<pxr::GfVec3f> colors;
78  colors.push_back(pxr::GfVec3f(cache[0]->col));
79  curves.CreateDisplayColorAttr(pxr::VtValue(colors));
80  }
81 }
82 
84 {
85  return true;
86 }
87 
88 } // namespace blender::io::usd
#define UNUSED(x)
pxr::UsdTimeCode get_export_time_code() const
pxr::UsdUtilsSparseValueWriter usd_value_writer_
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
struct SELECTID_Context context
Definition: select_engine.c:47
struct ParticleCacheKey ** pathcache