AL_USDMaya  0.29.4
USD to Maya Bridge
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
LayerManager.h
1 //
2 // Copyright 2017 Animal Logic
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.//
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 #pragma once
17 
18 #include "../Api.h"
19 
20 #include "AL/maya/utils/NodeHelper.h"
21 #include "pxr/pxr.h"
22 #include "pxr/usd/usd/stage.h"
23 
24 #include "maya/MPxLocatorNode.h"
25 #include "maya/MNodeMessage.h"
26 #include "AL/maya/utils/MayaHelperMacros.h"
27 
28 #include <iterator>
29 #include <map>
30 #include <set>
31 #include <boost/thread.hpp>
32 
33 PXR_NAMESPACE_USING_DIRECTIVE
34 
35 namespace AL {
36 namespace usdmaya {
37 namespace nodes {
38 
39 class ProxyShape;
40 
41 //----------------------------------------------------------------------------------------------------------------------
45 //----------------------------------------------------------------------------------------------------------------------
46 template <typename WrappedIterator>
48 // : public std::iterator<std::forward_iterator_tag,
49 // typename WrappedIterator::value_type>
50 {
51 public:
52  typedef typename WrappedIterator::value_type value_type;
53 
57  DirtyOnlyIterator(WrappedIterator it, WrappedIterator end):
58  m_iter(it),
59  m_end(end)
60  {
61  SetToNextDirty();
62  }
63 
67  m_iter(other.m_iter),
68  m_end(other.m_end)
69  {
70  SetToNextDirty();
71  }
72 
76  {
77  ++m_iter;
78  SetToNextDirty();
79  return *this;
80  }
81 
85  {
86  WrappedIterator tmp(*this);
87  operator++();
88  return tmp;
89  }
90 
94  bool operator == (const DirtyOnlyIterator& rhs) const
95  {
96  // You could argue we should check m_end too,
97  // but all we really care about is whether we're pointed
98  // at the same place, and it's faster...
99  return m_iter == rhs.m_iter;
100  }
101 
105  bool operator != (const DirtyOnlyIterator& rhs) const
106  {
107  return m_iter != rhs.m_iter;
108  }
109 
111  value_type& operator * ()
112  { return *m_iter; }
113 
114 private:
115  void SetToNextDirty()
116  {
117  while (m_iter != m_end && !m_iter->first->IsDirty())
118  {
119  ++m_iter;
120  }
121  }
122 
123  WrappedIterator m_iter;
124  WrappedIterator m_end;
125 };
126 
127 //----------------------------------------------------------------------------------------------------------------------
136 //----------------------------------------------------------------------------------------------------------------------
138 {
139 public:
140  typedef std::map<SdfLayerRefPtr, std::vector<std::string>> LayerToIdsMap;
141  typedef std::map<std::string, SdfLayerRefPtr> IdToLayerMap;
142 
156  bool addLayer(SdfLayerRefPtr layer, const std::string& identifier = std::string());
157 
161  bool removeLayer(SdfLayerRefPtr layer);
162 
166  SdfLayerHandle findLayer(std::string identifier) const;
167 
176 private:
177  typedef const LayerToIdsMap LayerDatabase::*_UnspecifiedBoolType;
178 public:
179  operator _UnspecifiedBoolType() const
180  { return begin() == end() ? &LayerDatabase::m_layerToIds : nullptr; }
181 
188  size_t max_size() const
189  { return m_layerToIds.size(); }
190 
191  // Iterator interface - skips past non-dirty items
194 
197  { return iterator(m_layerToIds.begin(), m_layerToIds.end()); }
198 
201  { return const_iterator(m_layerToIds.cbegin(), m_layerToIds.cend()); }
202 
205  { return const_iterator(m_layerToIds.cbegin(), m_layerToIds.cend()); }
206 
209  { return iterator(m_layerToIds.end(), m_layerToIds.end()); }
210 
213  { return const_iterator(m_layerToIds.cend(), m_layerToIds.cend()); }
214 
217  { return const_iterator(m_layerToIds.cend(), m_layerToIds.cend()); }
218 
219 private:
220  void _addLayer(SdfLayerRefPtr layer, const std::string& identifier,
221  std::vector<std::string>& idsForLayer);
222 
223  LayerToIdsMap m_layerToIds;
224  IdToLayerMap m_idToLayer;
225 };
226 
227 //----------------------------------------------------------------------------------------------------------------------
231 //----------------------------------------------------------------------------------------------------------------------
233  : public MPxNode,
234  public AL::maya::utils::NodeHelper
235 {
236 public:
237 
239  inline LayerManager()
240  : MPxNode(), NodeHelper() {}
241 
242  ~LayerManager();
243 
246  AL_USDMAYA_PUBLIC
247  static MObject findNode();
248 
255  AL_USDMAYA_PUBLIC
256  static MObject findOrCreateNode(MDGModifier* dgmod = nullptr, bool* wasCreated = nullptr);
257 
260  AL_USDMAYA_PUBLIC
261  static LayerManager* findManager();
262 
269  AL_USDMAYA_PUBLIC
270  static LayerManager* findOrCreateManager(MDGModifier* dgmod = nullptr, bool* wasCreated = nullptr);
271 
272  //--------------------------------------------------------------------------------------------------------------------
274  //--------------------------------------------------------------------------------------------------------------------
275 
288  AL_USDMAYA_PUBLIC
289  bool addLayer(SdfLayerHandle layer, const std::string& identifier = std::string());
290 
294  AL_USDMAYA_PUBLIC
295  bool removeLayer(SdfLayerHandle layer);
296 
299  AL_USDMAYA_PUBLIC
300  SdfLayerHandle findLayer(std::string identifier);
301 
305  AL_USDMAYA_PUBLIC
306  void getLayerIdentifiers(MStringArray& outputNames);
307 
309  AL_USDMAYA_PUBLIC
311 
313  AL_USDMAYA_PUBLIC
315 
317  AL_USDMAYA_PUBLIC
318  void loadAllLayers();
319 
320  //--------------------------------------------------------------------------------------------------------------------
322  //--------------------------------------------------------------------------------------------------------------------
324 
326  AL_USDMAYA_PUBLIC
327  static void* conditionalCreator();
328 
329  //--------------------------------------------------------------------------------------------------------------------
331  //--------------------------------------------------------------------------------------------------------------------
332 
333  // attributes to store the serialised layers (used for file IO only)
334 
335  // Note that the layers attribute should ONLY used during serialization, as this is the ONLY
336  // times at which these attributes are guaranteed to "line up" to the internal layer register
337  // (m_layerList). Ie, immediately before save (due to the pre-save callback), the attributes will
338  // be written from m_layerList; and immediate after open (due to the post-open callback), m_layerList
339  // will be initialized from the attributes. At all other times, the attributes will be OUT OF SYNC
340  // (and, in fact, are intentionally set to be "empty", so there's no confusion / someone doesn't
341  // try to use "out of date" information)
342  AL_DECL_ATTRIBUTE(layers);
343  // Not using AL_DECL_ATTRIBUTE for these, because we never want a generic, ie, identifierPlug() -
344  // they only make sense for a particular index of the parent array-attribute... and it taking up
345  // the "identifierPlug" name is confusing
346  AL_DECL_MULTI_CHILD_ATTRIBUTE(identifier);
347  AL_DECL_MULTI_CHILD_ATTRIBUTE(serialized);
348  AL_DECL_MULTI_CHILD_ATTRIBUTE(anonymous);
349 
350 private:
351  static MObject _findNode();
352 
353  LayerDatabase m_layerDatabase;
354 
355  // Note on layerManager / multithreading:
356  // I don't know that layerManager will be used in a multihreaded manenr... but I also don't know it COULDN'T be.
357  // (I haven't really looked into the way maya's new multi-threaded node evaluation works, for instance.) This is
358  // essentially a globally shared resource, so I figured better be safe...
359  boost::shared_mutex m_layersMutex;
360 
361  //--------------------------------------------------------------------------------------------------------------------
363  //--------------------------------------------------------------------------------------------------------------------
364 
368 
372 
376 
380 
384 
388 
392 
396 
397 };
398 
399 } // nodes
400 } // usdmaya
401 } // AL
402 //----------------------------------------------------------------------------------------------------------------------
403 
static AL_USDMAYA_PUBLIC void * conditionalCreator()
Creates the node, but only if there is not a non-referenced one in the scene already.
SdfLayerHandle findLayer(std::string identifier) const
Find the layer in the set of layers managed by this node, by identifier.
static AL_USDMAYA_PUBLIC MObject findOrCreateNode(MDGModifier *dgmod=nullptr, bool *wasCreated=nullptr)
Either find the already-existing non-referenced LayerManager node in the scene, or make one...
static AL_USDMAYA_PUBLIC MObject findNode()
Find the already-existing non-referenced LayerManager node in the scene, or return a null MObject...
AL_USDMAYA_PUBLIC MStatus populateSerialisationAttributes()
Ensures that the layers attribute will be filled out with serialized versions of all tracked layers...
Iterator wrapper for LayerToIdsMap that hides non-dirty items Implemented as a template to define con...
Definition: LayerManager.h:47
const_iterator cbegin() const
returns start of layer range
Definition: LayerManager.h:204
DirtyOnlyIterator operator++(int)
post-increment operator
Definition: LayerManager.h:84
iterator begin()
returns start of layer range
Definition: LayerManager.h:196
bool operator==(const DirtyOnlyIterator &rhs) const
equivalence operator
Definition: LayerManager.h:94
bool operator!=(const DirtyOnlyIterator &rhs) const
non equivalence operator
Definition: LayerManager.h:105
size_t max_size() const
Upper bound for the number of non-dirty layers in this object This is the count of all tracked layers...
Definition: LayerManager.h:188
const_iterator cend() const
returns end of layer range
Definition: LayerManager.h:216
DirtyOnlyIterator(const DirtyOnlyIterator &other)
copy ctor
Definition: LayerManager.h:66
bool addLayer(SdfLayerRefPtr layer, const std::string &identifier=std::string())
Add the given layer to the set of layers in this LayerDatabase, if not already present, and optionally add an extra identifier as a key to it.
AL_DECL_ATTRIBUTE(layers)
Type Info & Registration.
DirtyOnlyIterator & operator++()
pre-increment operator
Definition: LayerManager.h:75
static AL_USDMAYA_PUBLIC LayerManager * findManager()
Find the already-existing non-referenced LayerManager node in the scene, or return a nullptr...
AL_USDMAYA_PUBLIC void getLayerIdentifiers(MStringArray &outputNames)
Store a list of the managed layers' identifiers in the given MStringArray.
AL_USDMAYA_PUBLIC MStatus clearSerialisationAttributes()
Clears the layers attribute.
Stores layers, in a way that they may be looked up by the layer ref ptr, or by identifier Also...
Definition: LayerManager.h:137
const_iterator begin() const
returns start of layer range
Definition: LayerManager.h:200
bool removeLayer(SdfLayerRefPtr layer)
Remove the given layer to the list of layers managed by this node, if present.
DirtyOnlyIterator(WrappedIterator it, WrappedIterator end)
ctor
Definition: LayerManager.h:57
The layer manager node handles serialization and deserialization of all layers used by all ProxyShape...
Definition: LayerManager.h:232
const_iterator end() const
returns end of layer range
Definition: LayerManager.h:212
AL_USDMAYA_PUBLIC bool addLayer(SdfLayerHandle layer, const std::string &identifier=std::string())
Methods to handle the saving and restoring of layer data.
AL_USDMAYA_PUBLIC bool removeLayer(SdfLayerHandle layer)
Remove the given layer to the list of layers managed by this node, if present.
value_type & operator*()
dereference operator
Definition: LayerManager.h:111
AL_USDMAYA_PUBLIC SdfLayerHandle findLayer(std::string identifier)
Find the layer in the list of layers managed by this node, by identifier.
iterator end()
returns end of layer range
Definition: LayerManager.h:208
AL_USDMAYA_PUBLIC void loadAllLayers()
For every serialized layer stored in attributes, loads them as sdf layers.
static AL_USDMAYA_PUBLIC LayerManager * findOrCreateManager(MDGModifier *dgmod=nullptr, bool *wasCreated=nullptr)
Either find the already-existing non-referenced LayerManager in the scene, or make one...
AL_MAYA_DECLARE_NODE()
Type Info & Registration.
LayerManager()
ctor
Definition: LayerManager.h:239