Blender  V3.3
usd_reader_volume.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2021 Tangent Animation. All rights reserved. */
3 
4 #include "usd_reader_volume.h"
5 
6 #include "BKE_object.h"
7 #include "BKE_volume.h"
8 
9 #include "DNA_object_types.h"
10 #include "DNA_volume_types.h"
11 
12 #include <pxr/usd/usdVol/openVDBAsset.h>
13 #include <pxr/usd/usdVol/volume.h>
14 
15 #include <iostream>
16 
17 namespace usdtokens {
18 
19 static const pxr::TfToken density("density", pxr::TfToken::Immortal);
20 
21 }
22 
23 namespace blender::io::usd {
24 
25 void USDVolumeReader::create_object(Main *bmain, const double /* motionSampleTime */)
26 {
27  Volume *volume = (Volume *)BKE_volume_add(bmain, name_.c_str());
28 
30  object_->data = volume;
31 }
32 
33 void USDVolumeReader::read_object_data(Main *bmain, const double motionSampleTime)
34 {
35  if (!volume_) {
36  return;
37  }
38 
39  Volume *volume = static_cast<Volume *>(object_->data);
40 
41  if (!volume) {
42  return;
43  }
44 
45  pxr::UsdVolVolume::FieldMap fields = volume_.GetFieldPaths();
46 
47  for (pxr::UsdVolVolume::FieldMap::const_iterator it = fields.begin(); it != fields.end(); ++it) {
48 
49  pxr::UsdPrim fieldPrim = prim_.GetStage()->GetPrimAtPath(it->second);
50 
51  if (!fieldPrim.IsA<pxr::UsdVolOpenVDBAsset>()) {
52  continue;
53  }
54 
55  pxr::UsdVolOpenVDBAsset fieldBase(fieldPrim);
56 
57  pxr::UsdAttribute filepathAttr = fieldBase.GetFilePathAttr();
58 
59  if (filepathAttr.IsAuthored()) {
60  pxr::SdfAssetPath fp;
61  filepathAttr.Get(&fp, motionSampleTime);
62 
63  if (filepathAttr.ValueMightBeTimeVarying()) {
64  std::vector<double> filePathTimes;
65  filepathAttr.GetTimeSamples(&filePathTimes);
66 
67  if (!filePathTimes.empty()) {
68  int start = static_cast<int>(filePathTimes.front());
69  int end = static_cast<int>(filePathTimes.back());
70 
71  volume->is_sequence = static_cast<char>(true);
72  volume->frame_start = start;
73  volume->frame_duration = (end - start) + 1;
74  }
75  }
76 
77  std::string filepath = fp.GetResolvedPath();
78 
79  strcpy(volume->filepath, filepath.c_str());
80  }
81  }
82 
83  USDXformReader::read_object_data(bmain, motionSampleTime);
84 }
85 
86 } // namespace blender::io::usd
General operations, lookup, etc. for blender objects.
struct Object * BKE_object_add_only_object(struct Main *bmain, int type, const char *name) ATTR_RETURNS_NONNULL
Definition: object.cc:2241
Volume data-block.
void * BKE_volume_add(struct Main *bmain, const char *name)
Definition: volume.cc:690
Object is a sort of wrapper for general info.
@ OB_VOLUME
void create_object(Main *bmain, double motionSampleTime) override
void read_object_data(Main *bmain, double motionSampleTime) override
void read_object_data(Main *bmain, double motionSampleTime) override
static const pxr::TfToken density("density", pxr::TfToken::Immortal)
Definition: BKE_main.h:121
void * data
int frame_duration
char filepath[1024]
int frame_start
char is_sequence