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 /// \file VariantSpec.cpp
25
26 #include "pxr/pxr.h"
27 #include "pxr/usd/sdf/variantSpec.h"
28 #include "pxr/usd/sdf/changeBlock.h"
29 #include "pxr/usd/sdf/childrenUtils.h"
30 #include "pxr/usd/sdf/layer.h"
31 #include "pxr/usd/sdf/primSpec.h"
32 #include "pxr/usd/sdf/schema.h"
33 #include "pxr/usd/sdf/variantSetSpec.h"
34 #include "pxr/base/tf/diagnostic.h"
35 #include "pxr/base/tf/type.h"
36 #include "pxr/base/trace/trace.h"
37
38 using std::string;
39
40 PXR_NAMESPACE_OPEN_SCOPE
41
42 SDF_DEFINE_SPEC(SdfSchema, SdfSpecTypeVariant, SdfVariantSpec, SdfSpec);
43
44 SdfVariantSpecHandle
New(const SdfVariantSetSpecHandle & owner,const std::string & name)45 SdfVariantSpec::New(
46 const SdfVariantSetSpecHandle& owner, const std::string& name)
47 {
48 TRACE_FUNCTION();
49
50 if (!owner) {
51 TF_CODING_ERROR("NULL owner variant set");
52 return TfNullPtr;
53 }
54
55 if (!SdfSchema::IsValidVariantIdentifier(name)) {
56 TF_CODING_ERROR("Invalid variant name: %s", name.c_str());
57 return TfNullPtr;
58 }
59
60 SdfPath childPath =
61 Sdf_VariantChildPolicy::GetChildPath(owner->GetPath(), TfToken(name));
62
63 SdfLayerHandle layer = owner->GetLayer();
64 if (!Sdf_ChildrenUtils<Sdf_VariantChildPolicy>::CreateSpec(
65 layer, childPath, SdfSpecTypeVariant)) {
66 return TfNullPtr;
67 }
68
69 layer->SetField(childPath, SdfFieldKeys->Specifier, SdfSpecifierOver);
70
71 return TfStatic_cast<SdfVariantSpecHandle>(
72 layer->GetObjectAtPath(childPath));
73 }
74
75
76 //
77 // Name
78 //
79
80 std::string
GetName() const81 SdfVariantSpec::GetName() const
82 {
83 return GetPath().GetVariantSelection().second;
84 }
85
86 TfToken
GetNameToken() const87 SdfVariantSpec::GetNameToken() const
88 {
89 return TfToken(GetPath().GetVariantSelection().second);
90 }
91
92 //
93 // Namespace hierarchy
94 //
95
96 SdfVariantSetSpecHandle
GetOwner() const97 SdfVariantSpec::GetOwner() const
98 {
99 SdfPath path = Sdf_VariantChildPolicy::GetParentPath(GetPath());
100 return TfStatic_cast<SdfVariantSetSpecHandle>(
101 GetLayer()->GetObjectAtPath(path));
102 }
103
104 SdfPrimSpecHandle
GetPrimSpec() const105 SdfVariantSpec::GetPrimSpec() const
106 {
107 return GetLayer()->GetPrimAtPath(GetPath());
108 }
109
110 SdfVariantSetsProxy
GetVariantSets() const111 SdfVariantSpec::GetVariantSets() const
112 {
113 return SdfVariantSetsProxy(SdfVariantSetView(GetLayer(),
114 GetPath(), SdfChildrenKeys->VariantSetChildren),
115 "variant sets", SdfVariantSetsProxy::CanErase);
116 }
117
118 std::vector<std::string>
GetVariantNames(const std::string & name) const119 SdfVariantSpec::GetVariantNames(const std::string& name) const
120 {
121 std::vector<std::string> variantNames;
122
123 SdfPath variantSetPath = GetPath().AppendVariantSelection(name, "");
124 std::vector<TfToken> variantNameTokens =
125 GetLayer()->GetFieldAs<std::vector<TfToken> >(variantSetPath,
126 SdfChildrenKeys->VariantChildren);
127
128 variantNames.reserve(variantNameTokens.size());
129 TF_FOR_ALL(i, variantNameTokens) {
130 variantNames.push_back(i->GetString());
131 }
132
133 return variantNames;
134 }
135
136 SdfVariantSpecHandle
SdfCreateVariantInLayer(const SdfLayerHandle & layer,const SdfPath & primPath,const string & variantSetName,const string & variantName)137 SdfCreateVariantInLayer(
138 const SdfLayerHandle &layer,
139 const SdfPath &primPath,
140 const string &variantSetName,
141 const string &variantName )
142 {
143 SdfPath variantPath =
144 primPath.AppendVariantSelection(variantSetName, variantName);
145
146 // Create the ancestor prim and variant specs.
147 SdfCreatePrimInLayer(layer, variantPath);
148
149 // Look up the resulting variant spec.
150 return TfStatic_cast<SdfVariantSpecHandle>(
151 layer->GetObjectAtPath(variantPath));
152 }
153
154 PXR_NAMESPACE_CLOSE_SCOPE
155