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

usd_prim_iterator.cc « import « usd « io « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 18d4bc11d46d52f3d9af7424fbf5e080c0126d07 (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
/*
 * 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) 2020 Blender Foundation.
 * All rights reserved.
 */

#include "usd_prim_iterator.h"

#include "usd.h"
#include "usd_data_cache.h"
#include "usd_importer_context.h"
#include "usd_reader_camera.h"
#include "usd_reader_light.h"
#include "usd_reader_mesh.h"
#include "usd_reader_xform.h"
#include "usd_reader_xformable.h"

#include <iostream>
#include <pxr/pxr.h>
#include <pxr/usd/usd/prim.h>
#include <pxr/usd/usd/primRange.h>
#include <pxr/usd/usd/stage.h>
#include <pxr/usd/usdGeom/camera.h>
#include <pxr/usd/usdGeom/mesh.h>
#include <pxr/usd/usdGeom/scope.h>
#include <pxr/usd/usdGeom/tokens.h>
#include <pxr/usd/usdGeom/xformable.h>
#include <pxr/usd/usdLux/light.h>

namespace blender::io::usd {

USDPrimIterator::USDPrimIterator(pxr::UsdStageRefPtr stage,
                                 const USDImporterContext &context,
                                 Main *bmain)
    : stage_(stage), context_(context), bmain_(bmain)
{
}

void USDPrimIterator::create_object_readers(std::vector<USDXformableReader *> &r_readers) const
{
  if (!stage_) {
    return;
  }
  std::vector<USDXformableReader *> child_readers;

  create_object_readers(stage_->GetPseudoRoot(), context_, r_readers, child_readers);
}

void USDPrimIterator::create_prototype_object_readers(
    std::map<pxr::SdfPath, USDXformableReader *> &r_proto_readers) const
{
  if (!stage_) {
    return;
  }

  std::vector<pxr::UsdPrim> protos = stage_->GetMasters();

  for (const pxr::UsdPrim &proto_prim : protos) {
    std::vector<USDXformableReader *> proto_readers;
    std::vector<USDXformableReader *> child_readers;

    create_object_readers(proto_prim, context_, proto_readers, child_readers);

    for (USDXformableReader *reader : proto_readers) {
      if (reader) {
        pxr::UsdPrim reader_prim = reader->prim();
        if (reader_prim) {
          r_proto_readers.insert(std::make_pair(reader_prim.GetPath(), reader));
        }
      }
    }
  }
}

void USDPrimIterator::debug_traverse_stage() const
{
  debug_traverse_stage(stage_);
}

USDXformableReader *USDPrimIterator::get_object_reader(const pxr::UsdPrim &prim,
                                                       const USDImporterContext &context)
{
  USDXformableReader *result = nullptr;

  if (prim.IsA<pxr::UsdGeomCamera>()) {
    result = new USDCameraReader(prim, context);
  }
  else if (prim.IsA<pxr::UsdGeomMesh>()) {
    result = new USDMeshReader(prim, context);
  }
  else if (prim.IsA<pxr::UsdGeomXform>()) {
    result = new USDXformReader(prim, context);
  }
  else if (prim.IsA<pxr::UsdLuxLight>()) {
    result = new USDLightReader(prim, context);
  }

  return result;
}

void USDPrimIterator::create_object_readers(const pxr::UsdPrim &prim,
                                            const USDImporterContext &context,
                                            std::vector<USDXformableReader *> &r_readers,
                                            std::vector<USDXformableReader *> &r_child_readers)
{
  if (!prim) {
    return;
  }

  std::vector<USDXformableReader *> child_readers;

  /* Recursively create readers for the child prims. */
  pxr::UsdPrimSiblingRange child_prims = prim.GetFilteredChildren(
      pxr::UsdTraverseInstanceProxies(pxr::UsdPrimDefaultPredicate));

  for (const pxr::UsdPrim &child_prim : child_prims) {
    create_object_readers(child_prim, context, r_readers, child_readers);
  }

  if (prim.IsPseudoRoot()) {
    /* We're at the pseudo root, so we're done. */
    return;
  }

  /* We prune away empty transform or scope hierarchies (we can add an import flag to make this
   * behavior optional).  Therefore, we skip this prim if it's an Xform or Scope and if
   * it has no corresponding child readers. */
  if ((prim.IsA<pxr::UsdGeomXform>() || prim.IsA<pxr::UsdGeomScope>()) && child_readers.empty()) {
    return;
  }

  /* If this is an Xform prim, see if we can merge with the child reader. */

  if (prim.IsA<pxr::UsdGeomXform>() && child_readers.size() == 1 &&
      !child_readers.front()->merged_with_parent() &&
      child_readers.front()->can_merge_with_parent()) {
    child_readers.front()->set_merged_with_parent(true);
    /* Don't create a reader for the Xform but, instead, return the child
     * that we merged. */
    r_child_readers.push_back(child_readers.front());
    return;
  }

  USDXformableReader *reader = get_object_reader(prim, context);

  if (reader) {
    for (USDXformableReader *child_reader : child_readers) {
      child_reader->set_parent(reader);
    }
    r_child_readers.push_back(reader);
    r_readers.push_back(reader);
  }
  else {
    /* No reader was allocated for this prim, so we pass our child readers back to the caller,
     * for possible handling by a parent reader. */
    r_child_readers.insert(r_child_readers.end(), child_readers.begin(), child_readers.end());
  }
}

void USDPrimIterator::cache_prototype_data(USDDataCache &r_cache) const
{
  if (!stage_) {
    return;
  }

  std::vector<pxr::UsdPrim> protos = stage_->GetMasters();

  for (const pxr::UsdPrim &proto_prim : protos) {
    std::vector<USDXformableReader *> proto_readers;
    std::vector<USDXformableReader *> child_readers;

    create_object_readers(proto_prim, context_, proto_readers, child_readers);

    for (USDXformableReader *reader : proto_readers) {
      if (reader) {
        pxr::UsdPrim reader_prim = reader->prim();
        if (reader_prim) {

          if (USDMeshReaderBase *mesh_reader = dynamic_cast<USDMeshReaderBase *>(reader)) {
            Mesh *proto_mesh = mesh_reader->create_mesh(bmain_, 0.0);
            if (proto_mesh) {
              /* TODO(makowalski): Do we want to decrement the mesh's use count to 0?
               * Might have a small memory leak otherwise. Also, check if mesh is
               * already in cache before adding? */
              r_cache.add_prototype_mesh(reader_prim.GetPath(), proto_mesh);
            }
          }
        }
      }
    }

    /* Clean up the readers. */
    for (USDXformableReader *reader : proto_readers) {
      delete reader;
    }
  }
}

void USDPrimIterator::debug_traverse_stage(const pxr::UsdStageRefPtr &usd_stage)
{
  if (!usd_stage) {
    return;
  }

  pxr::UsdPrimRange prims = usd_stage->Traverse(
      pxr::UsdTraverseInstanceProxies(pxr::UsdPrimAllPrimsPredicate));

  for (const pxr::UsdPrim &prim : prims) {
    std::cout << prim.GetPath() << std::endl;
    std::cout << "  Type: " << prim.GetTypeName() << std::endl;
    if (prim.IsInstanceProxy()) {
      pxr::UsdPrim proto_prim = prim.GetPrimInMaster();
      if (proto_prim) {
        std::cout << "  Prototype prim: " << proto_prim.GetPath() << std::endl;
      }
    }
  }
}

} /* namespace blender::io::usd */