forked from autodesk-forks/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocument.h
618 lines (511 loc) · 17.9 KB
/
Document.h
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
//
// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
#ifndef MATERIALX_DOCUMENT
#define MATERIALX_DOCUMENT
/// @file
/// The top-level Document class
#include <MaterialXCore/Library.h>
#include <MaterialXCore/Look.h>
#include <MaterialXCore/Material.h>
#include <MaterialXCore/Node.h>
#include <MaterialXCore/Variant.h>
namespace MaterialX
{
class Document;
/// A shared pointer to a Document
using DocumentPtr = shared_ptr<Document>;
/// A shared pointer to a const Document
using ConstDocumentPtr = shared_ptr<const Document>;
/// @class Document
/// A MaterialX document, which represents the top-level element in the
/// MaterialX ownership hierarchy.
///
/// Use the factory function createDocument() to create a Document instance.
class Document : public GraphElement
{
public:
Document(ElementPtr parent, const string& name);
virtual ~Document();
/// Create a new document of the given subclass.
template <class T> static shared_ptr<T> createDocument()
{
shared_ptr<T> doc = std::make_shared<T>(ElementPtr(), EMPTY_STRING);
doc->initialize();
return doc;
}
/// Initialize the document, removing any existing content.
virtual void initialize();
/// Create a deep copy of the document.
virtual DocumentPtr copy() const
{
DocumentPtr doc = createDocument<Document>();
doc->copyContentFrom(getSelf());
return doc;
}
/// Import the given document as a library within this document.
/// The contents of the library document are copied into this one, and
/// are assigned the source URI of the library.
/// @param library The library document to be imported.
/// @param copyOptions An optional pointer to a CopyOptions object.
/// If provided, then the given options will affect the behavior of the
/// import function. Defaults to a null pointer.
void importLibrary(const ConstDocumentPtr& library, const CopyOptions* copyOptions = nullptr);
/// @}
/// @name NodeGraph Elements
/// @{
/// Add a NodeGraph to the document.
/// @param name The name of the new NodeGraph.
/// If no name is specified, then a unique name will automatically be
/// generated.
/// @return A shared pointer to the new NodeGraph.
NodeGraphPtr addNodeGraph(const string& name = EMPTY_STRING)
{
return addChild<NodeGraph>(name);
}
/// Return the NodeGraph, if any, with the given name.
NodeGraphPtr getNodeGraph(const string& name) const
{
return getChildOfType<NodeGraph>(name);
}
/// Return a vector of all NodeGraph elements in the document.
vector<NodeGraphPtr> getNodeGraphs() const
{
return getChildrenOfType<NodeGraph>();
}
/// Remove the NodeGraph, if any, with the given name.
void removeNodeGraph(const string& name)
{
removeChildOfType<NodeGraph>(name);
}
/// Return a vector of all port elements that match the given node name.
/// Port elements support spatially-varying upstream connections to
/// nodes, and include both Input and Output elements.
vector<PortElementPtr> getMatchingPorts(const string& nodeName) const;
/// @}
/// @name Material Elements
/// @{
/// Add a Material to the document.
/// @param name The name of the new Material.
/// If no name is specified, then a unique name will automatically be
/// generated.
/// @return A shared pointer to the new Material.
MaterialPtr addMaterial(const string& name = EMPTY_STRING)
{
return addChild<Material>(name);
}
/// Return the Material, if any, with the given name.
MaterialPtr getMaterial(const string& name) const
{
return getChildOfType<Material>(name);
}
/// Return a vector of all Material elements in the document.
vector<MaterialPtr> getMaterials() const
{
return getChildrenOfType<Material>();
}
/// Remove the Material, if any, with the given name.
void removeMaterial(const string& name)
{
removeChildOfType<Material>(name);
}
/// @}
/// @name GeomInfo Elements
/// @{
/// Add a GeomInfo to the document.
/// @param name The name of the new GeomInfo.
/// If no name is specified, then a unique name will automatically be
/// generated.
/// @param geom An optional geometry string for the GeomInfo.
/// @return A shared pointer to the new GeomInfo.
GeomInfoPtr addGeomInfo(const string& name = EMPTY_STRING, const string& geom = UNIVERSAL_GEOM_NAME)
{
GeomInfoPtr geomInfo = addChild<GeomInfo>(name);
geomInfo->setGeom(geom);
return geomInfo;
}
/// Return the GeomInfo, if any, with the given name.
GeomInfoPtr getGeomInfo(const string& name) const
{
return getChildOfType<GeomInfo>(name);
}
/// Return a vector of all GeomInfo elements in the document.
vector<GeomInfoPtr> getGeomInfos() const
{
return getChildrenOfType<GeomInfo>();
}
/// Remove the GeomInfo, if any, with the given name.
void removeGeomInfo(const string& name)
{
removeChildOfType<GeomInfo>(name);
}
/// Return the value of a geometric attribute for the given geometry string.
ValuePtr getGeomAttrValue(const string& geomAttrName, const string& geom = UNIVERSAL_GEOM_NAME) const;
/// @}
/// @name GeomPropDef Elements
/// @{
/// Add a GeomPropDef to the document.
/// @param name The name of the new GeomPropDef.
/// @param geomprop The geometric property to use for the GeomPropDef.
/// @return A shared pointer to the new GeomPropDef.
GeomPropDefPtr addGeomPropDef(const string& name, const string& geomprop)
{
GeomPropDefPtr geomPropDef = addChild<GeomPropDef>(name);
geomPropDef->setGeomProp(geomprop);
return geomPropDef;
}
/// Return the GeomPropDef, if any, with the given name.
GeomPropDefPtr getGeomPropDef(const string& name) const
{
return getChildOfType<GeomPropDef>(name);
}
/// Return a vector of all GeomPropDef elements in the document.
vector<GeomPropDefPtr> getGeomPropDefs() const
{
return getChildrenOfType<GeomPropDef>();
}
/// Remove the GeomPropDef, if any, with the given name.
void removeGeomPropDef(const string& name)
{
removeChildOfType<GeomPropDef>(name);
}
/// @}
/// @name Look Elements
/// @{
/// Add a Look to the document.
/// @param name The name of the new Look.
/// If no name is specified, then a unique name will automatically be
/// generated.
/// @return A shared pointer to the new Look.
LookPtr addLook(const string& name = EMPTY_STRING)
{
return addChild<Look>(name);
}
/// Return the Look, if any, with the given name.
LookPtr getLook(const string& name) const
{
return getChildOfType<Look>(name);
}
/// Return a vector of all Look elements in the document.
vector<LookPtr> getLooks() const
{
return getChildrenOfType<Look>();
}
/// Remove the Look, if any, with the given name.
void removeLook(const string& name)
{
removeChildOfType<Look>(name);
}
/// @}
/// @name Collection Elements
/// @{
/// Add a Collection to the document.
/// @param name The name of the new Collection.
/// If no name is specified, then a unique name will automatically be
/// generated.
/// @return A shared pointer to the new Collection.
CollectionPtr addCollection(const string& name = EMPTY_STRING)
{
return addChild<Collection>(name);
}
/// Return the Collection, if any, with the given name.
CollectionPtr getCollection(const string& name) const
{
return getChildOfType<Collection>(name);
}
/// Return a vector of all Collection elements in the document.
vector<CollectionPtr> getCollections() const
{
return getChildrenOfType<Collection>();
}
/// Remove the Collection, if any, with the given name.
void removeCollection(const string& name)
{
removeChildOfType<Collection>(name);
}
/// @}
/// @name TypeDef Elements
/// @{
/// Add a TypeDef to the document.
/// @param name The name of the new TypeDef.
/// If no name is specified, then a unique name will automatically be
/// generated.
/// @return A shared pointer to the new TypeDef.
TypeDefPtr addTypeDef(const string& name)
{
return addChild<TypeDef>(name);
}
/// Return the TypeDef, if any, with the given name.
TypeDefPtr getTypeDef(const string& name) const
{
return getChildOfType<TypeDef>(name);
}
/// Return a vector of all TypeDef elements in the document.
vector<TypeDefPtr> getTypeDefs() const
{
return getChildrenOfType<TypeDef>();
}
/// Remove the TypeDef, if any, with the given name.
void removeTypeDef(const string& name)
{
removeChildOfType<TypeDef>(name);
}
/// @}
/// @name NodeDef Elements
/// @{
/// Add a NodeDef to the document.
/// @param name The name of the new NodeDef.
/// If no name is specified, then a unique name will automatically be
/// generated.
/// @param type An optional type string.
/// @param node An optional node string.
/// @return A shared pointer to the new NodeDef.
NodeDefPtr addNodeDef(const string& name = EMPTY_STRING,
const string& type = DEFAULT_TYPE_STRING,
const string& node = EMPTY_STRING)
{
NodeDefPtr child = addChild<NodeDef>(name);
child->setType(type);
if (!node.empty())
{
child->setNodeString(node);
}
return child;
}
/// Return the NodeDef, if any, with the given name.
NodeDefPtr getNodeDef(const string& name) const
{
return getChildOfType<NodeDef>(name);
}
/// Return a vector of all NodeDef elements in the document.
vector<NodeDefPtr> getNodeDefs() const
{
return getChildrenOfType<NodeDef>();
}
/// Remove the NodeDef, if any, with the given name.
void removeNodeDef(const string& name)
{
removeChildOfType<NodeDef>(name);
}
/// Return a vector of all NodeDef elements that match the given node name.
vector<NodeDefPtr> getMatchingNodeDefs(const string& nodeName) const;
/// @}
/// @name PropertySet Elements
/// @{
/// Add a PropertySet to the document.
/// @param name The name of the new PropertySet.
/// If no name is specified, then a unique name will automatically be
/// generated.
/// @return A shared pointer to the new PropertySet.
PropertySetPtr addPropertySet(const string& name = EMPTY_STRING)
{
return addChild<PropertySet>(name);
}
/// Return the PropertySet, if any, with the given name.
PropertySetPtr getPropertySet(const string& name) const
{
return getChildOfType<PropertySet>(name);
}
/// Return a vector of all PropertySet elements in the document.
vector<PropertySetPtr> getPropertySets() const
{
return getChildrenOfType<PropertySet>();
}
/// Remove the PropertySet, if any, with the given name.
void removePropertySet(const string& name)
{
removeChildOfType<PropertySet>(name);
}
/// @}
/// @name VariantSet Elements
/// @{
/// Add a VariantSet to the document.
/// @param name The name of the new VariantSet.
/// If no name is specified, then a unique name will automatically be
/// generated.
/// @return A shared pointer to the new VariantSet.
VariantSetPtr addVariantSet(const string& name = EMPTY_STRING)
{
return addChild<VariantSet>(name);
}
/// Return the VariantSet, if any, with the given name.
VariantSetPtr getVariantSet(const string& name) const
{
return getChildOfType<VariantSet>(name);
}
/// Return a vector of all VariantSet elements in the document.
vector<VariantSetPtr> getVariantSets() const
{
return getChildrenOfType<VariantSet>();
}
/// Remove the VariantSet, if any, with the given name.
void removeVariantSet(const string& name)
{
removeChildOfType<VariantSet>(name);
}
/// @}
/// @name Implementation Elements
/// @{
/// Add an Implementation to the document.
/// @param name The name of the new Implementation.
/// If no name is specified, then a unique name will automatically be
/// generated.
/// @return A shared pointer to the new Implementation.
ImplementationPtr addImplementation(const string& name = EMPTY_STRING)
{
return addChild<Implementation>(name);
}
/// Return the Implementation, if any, with the given name.
ImplementationPtr getImplementation(const string& name) const
{
return getChildOfType<Implementation>(name);
}
/// Return a vector of all Implementation elements in the document.
vector<ImplementationPtr> getImplementations() const
{
return getChildrenOfType<Implementation>();
}
/// Remove the Implementation, if any, with the given name.
void removeImplementation(const string& name)
{
removeChildOfType<Implementation>(name);
}
/// Return a vector of all node implementations that match the given
/// NodeDef string. Note that a node implementation may be either an
/// Implementation element or NodeGraph element.
vector<InterfaceElementPtr> getMatchingImplementations(const string& nodeDef) const;
/// @}
/// @name Version
/// @{
/// Return the major and minor versions as an integer pair.
std::pair<int, int> getVersionIntegers() const override;
/// Upgrade the content of this document from earlier supported versions to
/// the library version. Documents from future versions are left unmodified.
void upgradeVersion();
/// @}
/// @name Color Management System
/// @{
/// Set the color management system string.
void setColorManagementSystem(const string& cms)
{
setAttribute(CMS_ATTRIBUTE, cms);
}
/// Return true if a color management system string has been set.
bool hasColorManagementSystem() const
{
return hasAttribute(CMS_ATTRIBUTE);
}
/// Return the color management system string.
const string& getColorManagementSystem() const
{
return getAttribute(CMS_ATTRIBUTE);
}
/// @}
/// @name Color Management Config
/// @{
/// Set the color management config string.
void setColorManagementConfig(const string& cmsConfig)
{
setAttribute(CMS_CONFIG_ATTRIBUTE, cmsConfig);
}
/// Return true if a color management config string has been set.
bool hasColorManagementConfig() const
{
return hasAttribute(CMS_CONFIG_ATTRIBUTE);
}
/// Return the color management config string.
const string& getColorManagementConfig() const
{
return getAttribute(CMS_CONFIG_ATTRIBUTE);
}
/// @}
/// @name Validation
/// @{
/// Validate that the given document is consistent with the MaterialX
/// specification.
/// @param message An optional output string, to which a description of
/// each error will be appended.
/// @return True if the document passes all tests, false otherwise.
bool validate(string* message = nullptr) const override;
/// @}
/// @name Callbacks
/// @{
/// Called when an element is added to the element tree.
virtual void onAddElement(ElementPtr parent, ElementPtr elem);
/// Called when an element is removed from the element tree.
virtual void onRemoveElement(ElementPtr parent, ElementPtr elem);
/// Called when an attribute of an element is set to a new value.
virtual void onSetAttribute(ElementPtr elem, const string& attrib, const string& value);
/// Called when an attribute of an element is removed.
virtual void onRemoveAttribute(ElementPtr elem, const string& attrib);
/// Called when content is copied into an element.
virtual void onCopyContent(ElementPtr elem);
/// Called when content is cleared from an element.
virtual void onClearContent(ElementPtr elem);
/// Called when data is read into the current document.
virtual void onRead() { }
/// Called when data is written from the current document.
virtual void onWrite() { }
/// Called before a set of document updates is performed.
virtual void onBeginUpdate() { }
/// Called after a set of document updates is performed.
virtual void onEndUpdate() { }
/// Enable observer callbacks
virtual void enableCallbacks() { }
/// Disable observer callbacks
virtual void disableCallbacks() { }
/// @}
public:
static const string CATEGORY;
static const string CMS_ATTRIBUTE;
static const string CMS_CONFIG_ATTRIBUTE;
private:
class Cache;
std::unique_ptr<Cache> _cache;
};
/// @class ScopedUpdate
/// An RAII class for Document updates.
///
/// A ScopedUpdate instance calls Document::onBeginUpdate when created, and
/// Document::onEndUpdate when destroyed.
class ScopedUpdate
{
public:
explicit ScopedUpdate(DocumentPtr doc) :
_doc(doc)
{
_doc->onBeginUpdate();
}
~ScopedUpdate()
{
_doc->onEndUpdate();
}
private:
DocumentPtr _doc;
};
/// @class ScopedDisableCallbacks
/// An RAII class for disabling Document callbacks.
///
/// A ScopedDisableCallbacks instance calls Document::disableCallbacks() when
/// created, and Document::enableCallbacks when destroyed.
class ScopedDisableCallbacks
{
public:
explicit ScopedDisableCallbacks(DocumentPtr doc) :
_doc(doc)
{
_doc->disableCallbacks();
}
~ScopedDisableCallbacks()
{
_doc->enableCallbacks();
}
private:
DocumentPtr _doc;
};
/// Create a new Document.
/// @relates Document
DocumentPtr createDocument();
} // namespace MaterialX
#endif