1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 //    names, trademarks, service marks, or product names of the Licensor
11 //    and its affiliates, except as required to comply with Section 4(c) of
12 //    the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 //     http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #include "pxr/pxr.h"
25 #include "pxr/usd/usd/schemaBase.h"
26 
27 #include "pxr/usd/usd/stage.h"
28 #include "pxr/base/tf/type.h"
29 
30 PXR_NAMESPACE_OPEN_SCOPE
31 
32 
33 // Register the schema with the TfType system.
TF_REGISTRY_FUNCTION(TfType)34 TF_REGISTRY_FUNCTION(TfType)
35 {
36     TfType::Define< UsdSchemaBase >();
37 }
38 
UsdSchemaBase(const UsdPrim & prim)39 UsdSchemaBase::UsdSchemaBase(const UsdPrim& prim)
40     : _primData(prim._Prim())
41     , _proxyPrimPath(prim._ProxyPrimPath())
42 {
43     /* NOTHING */
44 }
45 
UsdSchemaBase(const UsdSchemaBase & schema)46 UsdSchemaBase::UsdSchemaBase(const UsdSchemaBase& schema)
47     : _primData(schema._primData)
48     , _proxyPrimPath(schema._proxyPrimPath)
49 {
50     /* NOTHING YET */
51 }
52 
53 /*virtual*/
~UsdSchemaBase()54 UsdSchemaBase::~UsdSchemaBase()
55 {
56     // This only exists to avoid memory leaks in derived classes which may
57     // define new members.
58 }
59 
60 const UsdPrimDefinition *
GetSchemaClassPrimDefinition() const61 UsdSchemaBase::GetSchemaClassPrimDefinition() const
62 {
63     const UsdSchemaRegistry &reg = UsdSchemaRegistry::GetInstance();
64     const TfToken usdTypeName = reg.GetSchemaTypeName(_GetType());
65     return IsAppliedAPISchema() ?
66         reg.FindAppliedAPIPrimDefinition(usdTypeName) :
67         reg.FindConcretePrimDefinition(usdTypeName);
68 }
69 
70 bool
_IsCompatible() const71 UsdSchemaBase::_IsCompatible() const
72 {
73     // By default, schema objects are compatible with any valid prim.
74     return true;
75 }
76 
TF_MAKE_STATIC_DATA(TfType,_tfType)77 TF_MAKE_STATIC_DATA(TfType, _tfType) {
78     *_tfType = TfType::Find<UsdSchemaBase>();
79 }
80 const TfType &
_GetTfType() const81 UsdSchemaBase::_GetTfType() const
82 {
83     return *_tfType;
84 }
85 
86 UsdAttribute
_CreateAttr(TfToken const & attrName,SdfValueTypeName const & typeName,bool custom,SdfVariability variability,VtValue const & defaultValue,bool writeSparsely) const87 UsdSchemaBase::_CreateAttr(TfToken const &attrName,
88                            SdfValueTypeName const & typeName,
89                            bool custom, SdfVariability variability,
90                            VtValue const &defaultValue,
91                            bool writeSparsely) const
92 {
93     UsdPrim prim(GetPrim());
94 
95     if (writeSparsely && !custom){
96         // We are a builtin, and we're trying to be parsimonious.
97         // We only need to even CREATE a propertySpec if we are
98         // authoring a non-fallback default value
99         UsdAttribute attr = prim.GetAttribute(attrName);
100         VtValue  fallback;
101         if (defaultValue.IsEmpty() ||
102             (!attr.HasAuthoredValue()
103              && attr.Get(&fallback)
104              && fallback == defaultValue)){
105             return attr;
106         }
107     }
108 
109     UsdAttribute attr(prim.CreateAttribute(attrName, typeName,
110                                            custom, variability));
111     if (attr && !defaultValue.IsEmpty()) {
112         attr.Set(defaultValue);
113     }
114 
115     return attr;
116 }
117 
118 PXR_NAMESPACE_CLOSE_SCOPE
119 
120