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/usd/usdGeom/motionAPI.h"
25 #include "pxr/usd/usd/schemaRegistry.h"
26 #include "pxr/usd/usd/typed.h"
27 #include "pxr/usd/usd/tokens.h"
28 
29 #include "pxr/usd/sdf/types.h"
30 #include "pxr/usd/sdf/assetPath.h"
31 
32 PXR_NAMESPACE_OPEN_SCOPE
33 
34 // Register the schema with the TfType system.
TF_REGISTRY_FUNCTION(TfType)35 TF_REGISTRY_FUNCTION(TfType)
36 {
37     TfType::Define<UsdGeomMotionAPI,
38         TfType::Bases< UsdAPISchemaBase > >();
39 
40 }
41 
42 TF_DEFINE_PRIVATE_TOKENS(
43     _schemaTokens,
44     (MotionAPI)
45 );
46 
47 /* virtual */
~UsdGeomMotionAPI()48 UsdGeomMotionAPI::~UsdGeomMotionAPI()
49 {
50 }
51 
52 /* static */
53 UsdGeomMotionAPI
Get(const UsdStagePtr & stage,const SdfPath & path)54 UsdGeomMotionAPI::Get(const UsdStagePtr &stage, const SdfPath &path)
55 {
56     if (!stage) {
57         TF_CODING_ERROR("Invalid stage");
58         return UsdGeomMotionAPI();
59     }
60     return UsdGeomMotionAPI(stage->GetPrimAtPath(path));
61 }
62 
63 
64 /* virtual */
_GetSchemaKind() const65 UsdSchemaKind UsdGeomMotionAPI::_GetSchemaKind() const
66 {
67     return UsdGeomMotionAPI::schemaKind;
68 }
69 
70 /* static */
71 bool
CanApply(const UsdPrim & prim,std::string * whyNot)72 UsdGeomMotionAPI::CanApply(
73     const UsdPrim &prim, std::string *whyNot)
74 {
75     return prim.CanApplyAPI<UsdGeomMotionAPI>(whyNot);
76 }
77 
78 /* static */
79 UsdGeomMotionAPI
Apply(const UsdPrim & prim)80 UsdGeomMotionAPI::Apply(const UsdPrim &prim)
81 {
82     if (prim.ApplyAPI<UsdGeomMotionAPI>()) {
83         return UsdGeomMotionAPI(prim);
84     }
85     return UsdGeomMotionAPI();
86 }
87 
88 /* static */
89 const TfType &
_GetStaticTfType()90 UsdGeomMotionAPI::_GetStaticTfType()
91 {
92     static TfType tfType = TfType::Find<UsdGeomMotionAPI>();
93     return tfType;
94 }
95 
96 /* static */
97 bool
_IsTypedSchema()98 UsdGeomMotionAPI::_IsTypedSchema()
99 {
100     static bool isTyped = _GetStaticTfType().IsA<UsdTyped>();
101     return isTyped;
102 }
103 
104 /* virtual */
105 const TfType &
_GetTfType() const106 UsdGeomMotionAPI::_GetTfType() const
107 {
108     return _GetStaticTfType();
109 }
110 
111 UsdAttribute
GetVelocityScaleAttr() const112 UsdGeomMotionAPI::GetVelocityScaleAttr() const
113 {
114     return GetPrim().GetAttribute(UsdGeomTokens->motionVelocityScale);
115 }
116 
117 UsdAttribute
CreateVelocityScaleAttr(VtValue const & defaultValue,bool writeSparsely) const118 UsdGeomMotionAPI::CreateVelocityScaleAttr(VtValue const &defaultValue, bool writeSparsely) const
119 {
120     return UsdSchemaBase::_CreateAttr(UsdGeomTokens->motionVelocityScale,
121                        SdfValueTypeNames->Float,
122                        /* custom = */ false,
123                        SdfVariabilityVarying,
124                        defaultValue,
125                        writeSparsely);
126 }
127 
128 namespace {
129 static inline TfTokenVector
_ConcatenateAttributeNames(const TfTokenVector & left,const TfTokenVector & right)130 _ConcatenateAttributeNames(const TfTokenVector& left,const TfTokenVector& right)
131 {
132     TfTokenVector result;
133     result.reserve(left.size() + right.size());
134     result.insert(result.end(), left.begin(), left.end());
135     result.insert(result.end(), right.begin(), right.end());
136     return result;
137 }
138 }
139 
140 /*static*/
141 const TfTokenVector&
GetSchemaAttributeNames(bool includeInherited)142 UsdGeomMotionAPI::GetSchemaAttributeNames(bool includeInherited)
143 {
144     static TfTokenVector localNames = {
145         UsdGeomTokens->motionVelocityScale,
146     };
147     static TfTokenVector allNames =
148         _ConcatenateAttributeNames(
149             UsdAPISchemaBase::GetSchemaAttributeNames(true),
150             localNames);
151 
152     if (includeInherited)
153         return allNames;
154     else
155         return localNames;
156 }
157 
158 PXR_NAMESPACE_CLOSE_SCOPE
159 
160 // ===================================================================== //
161 // Feel free to add custom code below this line. It will be preserved by
162 // the code generator.
163 //
164 // Just remember to wrap code in the appropriate delimiters:
165 // 'PXR_NAMESPACE_OPEN_SCOPE', 'PXR_NAMESPACE_CLOSE_SCOPE'.
166 // ===================================================================== //
167 // --(BEGIN CUSTOM CODE)--
168 
169 PXR_NAMESPACE_OPEN_SCOPE
170 
ComputeVelocityScale(UsdTimeCode time) const171 float UsdGeomMotionAPI::ComputeVelocityScale(UsdTimeCode time) const
172 {
173     UsdPrim prim = GetPrim();
174     UsdPrim pseudoRoot = prim.GetStage()->GetPseudoRoot();
175     float  velocityScale = 1.0;
176 
177     while (prim != pseudoRoot){
178         UsdAttribute vsAttr =
179             prim.GetAttribute(UsdGeomTokens->motionVelocityScale);
180         if (vsAttr.HasAuthoredValue() &&
181             vsAttr.Get(&velocityScale, time)){
182             return velocityScale;
183         }
184         prim = prim.GetParent();
185     }
186 
187     return velocityScale;
188 }
189 
190 PXR_NAMESPACE_CLOSE_SCOPE
191