Welcome to mirror list, hosted at ThFree Co, Russian Federation.

usd_reader_stage.cc « intern « usd « io « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e9bd4a8064137b9aecd5b63d1a7a1797010c31da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software  Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2016 Kévin Dietrich.
 * All rights reserved.
 */

#include "usd_reader_stage.h"
#include "usd_reader_camera.h"
#include "usd_reader_curve.h"
#include "usd_reader_mesh.h"
#include "usd_reader_prim.h"
#include "usd_reader_xform.h"

#include "usd_util.h"

extern "C" {
#include "DEG_depsgraph.h"
#include "DEG_depsgraph_build.h"
#include "DEG_depsgraph_query.h"

#include "DNA_node_types.h"
#include "DNA_scene_types.h"
#include "DNA_world_types.h"

#include "BKE_blender_version.h"
#include "BKE_context.h"
#include "BKE_global.h"
#include "BKE_main.h"
#include "BKE_node.h"
#include "BKE_scene.h"
#include "BKE_world.h"

#include "BLI_fileops.h"
#include "BLI_path_util.h"
#include "BLI_string.h"

#include "WM_api.h"
#include "WM_types.h"
}
#include <iostream>

namespace blender::io::usd {

USDStageReader::USDStageReader(struct Main *bmain, const char *filename)
{
  stage_ = pxr::UsdStage::Open(filename);
}

USDStageReader::~USDStageReader()
{
  clear_readers();

  stage_->Unload();
}

bool USDStageReader::valid() const
{
  // TODO: Implement
  return true;
}

/* Returns true if the given prim should be excluded from the
 * traversal because it's invisible. */
bool _prune_by_visibility(const pxr::UsdGeomImageable &imageable, const USDImportParams &params)
{
  if (imageable && params.import_visible_only) {
    if (pxr::UsdAttribute visibility_attr = imageable.GetVisibilityAttr()) {

      // Prune if the prim has a non-animating visibility attribute and is
      // invisible.
      if (!visibility_attr.ValueMightBeTimeVarying()) {
        pxr::TfToken visibility;
        visibility_attr.Get(&visibility);
        return visibility == pxr::UsdGeomTokens->invisible;
      }
    }
  }

  return false;
}

/* Returns true if the given prim should be excluded from the
 * traversal because it has a purpose which was not requested
 * by the user; e.g., the prim represents guide geometry and
 * the import_guide parameter is toggled off. */
bool _prune_by_purpose(const pxr::UsdGeomImageable &imageable, const USDImportParams &params)
{
  if (imageable && !(params.import_guide && params.import_proxy && params.import_render)) {
    if (pxr::UsdAttribute purpose_attr = imageable.GetPurposeAttr()) {
      pxr::TfToken purpose;
      purpose_attr.Get(&purpose);
      if ((!params.import_guide && purpose == pxr::UsdGeomTokens->guide) ||
          (!params.import_proxy && purpose == pxr::UsdGeomTokens->proxy) ||
          (!params.import_render && purpose == pxr::UsdGeomTokens->render)) {
        return true;
      }
    }
  }

  return false;
}

static USDPrimReader *_handlePrim(Main *bmain,
                                  pxr::UsdStageRefPtr stage,
                                  const USDImportParams &params,
                                  pxr::UsdPrim prim,
                                  USDPrimReader *parent_reader,
                                  std::vector<USDPrimReader *> &readers,
                                  ImportSettings &settings)
{
  if (prim.IsA<pxr::UsdGeomImageable>()) {
    pxr::UsdGeomImageable imageable(prim);

    if (_prune_by_purpose(imageable, params)) {
      return nullptr;
    }

    if (_prune_by_visibility(imageable, params)) {
      return nullptr;
    }
  }

  USDPrimReader *reader = NULL;

  // This check prevents the stage pseudo 'root' prim
  // or the root prims of scenegraph 'master' prototypes
  // from being added.
  if (!(prim.IsPseudoRoot() || prim.IsMaster())) {
    reader = blender::io::usd::create_reader(prim, params, settings);
    if (reader == NULL)
      return NULL;

    reader->parent(parent_reader);
    reader->create_object(bmain, 0.0);

    readers.push_back(reader);
    reader->incref();
  }

  pxr::Usd_PrimFlagsPredicate filter_predicate = pxr::UsdPrimDefaultPredicate;

  if (!params.use_instancing && params.import_instance_proxies) {
    filter_predicate = pxr::UsdTraverseInstanceProxies(filter_predicate);
  }

  pxr::UsdPrimSiblingRange children = prim.GetFilteredChildren(filter_predicate);

  for (const auto &childPrim : children) {
    _handlePrim(bmain, stage, params, childPrim, reader, readers, settings);
  }

  return reader;
}

std::vector<USDPrimReader *> USDStageReader::collect_readers(Main *bmain,
                                                             const USDImportParams &params,
                                                             ImportSettings &settings)
{
  params_ = params;
  settings_ = settings;

  clear_readers();

  // Iterate through stage
  pxr::UsdPrim root = stage_->GetPseudoRoot();

  std::string prim_path_mask(params.prim_path_mask);

  if (prim_path_mask.size() > 0) {
    std::cout << prim_path_mask << '\n';
    pxr::SdfPath path = pxr::SdfPath(prim_path_mask);
    pxr::UsdPrim prim = stage_->GetPrimAtPath(path.StripAllVariantSelections());
    if (prim.IsValid()) {
      root = prim;
      if (path.ContainsPrimVariantSelection()) {
        // TODO: This will not work properly with setting variants on child prims
        while (path.ContainsPrimVariantSelection()) {
          std::pair<std::string, std::string> variantSelection = path.GetVariantSelection();
          root.GetVariantSet(variantSelection.first).SetVariantSelection(variantSelection.second);
          path = path.GetParentPath();
        }
      }
    }
  }

  stage_->SetInterpolationType(pxr::UsdInterpolationType::UsdInterpolationTypeHeld);
  _handlePrim(bmain, stage_, params, root, NULL, readers_, settings);

  if (params.use_instancing) {
    // Collect the scenegraph instance prototypes.
    std::vector<pxr::UsdPrim> protos = stage_->GetMasters();

    for (const pxr::UsdPrim &proto_prim : protos) {
      std::vector<USDPrimReader *> proto_readers;
      _handlePrim(bmain, stage_, params, proto_prim, NULL, proto_readers, settings);
      proto_readers_.insert(std::make_pair(proto_prim.GetPath(), proto_readers));
    }
  }

  return readers_;
}

void USDStageReader::clear_readers()
{
  std::vector<USDPrimReader *>::iterator iter;
  for (iter = readers_.begin(); iter != readers_.end(); ++iter) {
    if (((USDPrimReader *)*iter)->refcount() == 0) {
      delete *iter;
    }
  }

  readers_.clear();
}

void USDStageReader::clear_proto_readers(bool decref)
{
  for (auto &pair : proto_readers_) {

    for (USDPrimReader *reader : pair.second) {

      if (!reader) {
        continue;
      }

      if (decref) {
        reader->decref();
      }

      if (reader->refcount() == 0) {
        delete reader;
      }
    }

    pair.second.clear();
  }

  proto_readers_.clear();
}

}  // Namespace blender::io::usd