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/usdLux/lightAPI.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<UsdLuxLightAPI,
38         TfType::Bases< UsdAPISchemaBase > >();
39 
40 }
41 
42 TF_DEFINE_PRIVATE_TOKENS(
43     _schemaTokens,
44     (LightAPI)
45 );
46 
47 /* virtual */
~UsdLuxLightAPI()48 UsdLuxLightAPI::~UsdLuxLightAPI()
49 {
50 }
51 
52 /* static */
53 UsdLuxLightAPI
Get(const UsdStagePtr & stage,const SdfPath & path)54 UsdLuxLightAPI::Get(const UsdStagePtr &stage, const SdfPath &path)
55 {
56     if (!stage) {
57         TF_CODING_ERROR("Invalid stage");
58         return UsdLuxLightAPI();
59     }
60     return UsdLuxLightAPI(stage->GetPrimAtPath(path));
61 }
62 
63 
64 /* virtual */
_GetSchemaKind() const65 UsdSchemaKind UsdLuxLightAPI::_GetSchemaKind() const
66 {
67     return UsdLuxLightAPI::schemaKind;
68 }
69 
70 /* static */
71 bool
CanApply(const UsdPrim & prim,std::string * whyNot)72 UsdLuxLightAPI::CanApply(
73     const UsdPrim &prim, std::string *whyNot)
74 {
75     return prim.CanApplyAPI<UsdLuxLightAPI>(whyNot);
76 }
77 
78 /* static */
79 UsdLuxLightAPI
Apply(const UsdPrim & prim)80 UsdLuxLightAPI::Apply(const UsdPrim &prim)
81 {
82     if (prim.ApplyAPI<UsdLuxLightAPI>()) {
83         return UsdLuxLightAPI(prim);
84     }
85     return UsdLuxLightAPI();
86 }
87 
88 /* static */
89 const TfType &
_GetStaticTfType()90 UsdLuxLightAPI::_GetStaticTfType()
91 {
92     static TfType tfType = TfType::Find<UsdLuxLightAPI>();
93     return tfType;
94 }
95 
96 /* static */
97 bool
_IsTypedSchema()98 UsdLuxLightAPI::_IsTypedSchema()
99 {
100     static bool isTyped = _GetStaticTfType().IsA<UsdTyped>();
101     return isTyped;
102 }
103 
104 /* virtual */
105 const TfType &
_GetTfType() const106 UsdLuxLightAPI::_GetTfType() const
107 {
108     return _GetStaticTfType();
109 }
110 
111 UsdAttribute
GetShaderIdAttr() const112 UsdLuxLightAPI::GetShaderIdAttr() const
113 {
114     return GetPrim().GetAttribute(UsdLuxTokens->lightShaderId);
115 }
116 
117 UsdAttribute
CreateShaderIdAttr(VtValue const & defaultValue,bool writeSparsely) const118 UsdLuxLightAPI::CreateShaderIdAttr(VtValue const &defaultValue, bool writeSparsely) const
119 {
120     return UsdSchemaBase::_CreateAttr(UsdLuxTokens->lightShaderId,
121                        SdfValueTypeNames->Token,
122                        /* custom = */ false,
123                        SdfVariabilityUniform,
124                        defaultValue,
125                        writeSparsely);
126 }
127 
128 UsdAttribute
GetMaterialSyncModeAttr() const129 UsdLuxLightAPI::GetMaterialSyncModeAttr() const
130 {
131     return GetPrim().GetAttribute(UsdLuxTokens->lightMaterialSyncMode);
132 }
133 
134 UsdAttribute
CreateMaterialSyncModeAttr(VtValue const & defaultValue,bool writeSparsely) const135 UsdLuxLightAPI::CreateMaterialSyncModeAttr(VtValue const &defaultValue, bool writeSparsely) const
136 {
137     return UsdSchemaBase::_CreateAttr(UsdLuxTokens->lightMaterialSyncMode,
138                        SdfValueTypeNames->Token,
139                        /* custom = */ false,
140                        SdfVariabilityUniform,
141                        defaultValue,
142                        writeSparsely);
143 }
144 
145 UsdAttribute
GetIntensityAttr() const146 UsdLuxLightAPI::GetIntensityAttr() const
147 {
148     return GetPrim().GetAttribute(UsdLuxTokens->inputsIntensity);
149 }
150 
151 UsdAttribute
CreateIntensityAttr(VtValue const & defaultValue,bool writeSparsely) const152 UsdLuxLightAPI::CreateIntensityAttr(VtValue const &defaultValue, bool writeSparsely) const
153 {
154     return UsdSchemaBase::_CreateAttr(UsdLuxTokens->inputsIntensity,
155                        SdfValueTypeNames->Float,
156                        /* custom = */ false,
157                        SdfVariabilityVarying,
158                        defaultValue,
159                        writeSparsely);
160 }
161 
162 UsdAttribute
GetExposureAttr() const163 UsdLuxLightAPI::GetExposureAttr() const
164 {
165     return GetPrim().GetAttribute(UsdLuxTokens->inputsExposure);
166 }
167 
168 UsdAttribute
CreateExposureAttr(VtValue const & defaultValue,bool writeSparsely) const169 UsdLuxLightAPI::CreateExposureAttr(VtValue const &defaultValue, bool writeSparsely) const
170 {
171     return UsdSchemaBase::_CreateAttr(UsdLuxTokens->inputsExposure,
172                        SdfValueTypeNames->Float,
173                        /* custom = */ false,
174                        SdfVariabilityVarying,
175                        defaultValue,
176                        writeSparsely);
177 }
178 
179 UsdAttribute
GetDiffuseAttr() const180 UsdLuxLightAPI::GetDiffuseAttr() const
181 {
182     return GetPrim().GetAttribute(UsdLuxTokens->inputsDiffuse);
183 }
184 
185 UsdAttribute
CreateDiffuseAttr(VtValue const & defaultValue,bool writeSparsely) const186 UsdLuxLightAPI::CreateDiffuseAttr(VtValue const &defaultValue, bool writeSparsely) const
187 {
188     return UsdSchemaBase::_CreateAttr(UsdLuxTokens->inputsDiffuse,
189                        SdfValueTypeNames->Float,
190                        /* custom = */ false,
191                        SdfVariabilityVarying,
192                        defaultValue,
193                        writeSparsely);
194 }
195 
196 UsdAttribute
GetSpecularAttr() const197 UsdLuxLightAPI::GetSpecularAttr() const
198 {
199     return GetPrim().GetAttribute(UsdLuxTokens->inputsSpecular);
200 }
201 
202 UsdAttribute
CreateSpecularAttr(VtValue const & defaultValue,bool writeSparsely) const203 UsdLuxLightAPI::CreateSpecularAttr(VtValue const &defaultValue, bool writeSparsely) const
204 {
205     return UsdSchemaBase::_CreateAttr(UsdLuxTokens->inputsSpecular,
206                        SdfValueTypeNames->Float,
207                        /* custom = */ false,
208                        SdfVariabilityVarying,
209                        defaultValue,
210                        writeSparsely);
211 }
212 
213 UsdAttribute
GetNormalizeAttr() const214 UsdLuxLightAPI::GetNormalizeAttr() const
215 {
216     return GetPrim().GetAttribute(UsdLuxTokens->inputsNormalize);
217 }
218 
219 UsdAttribute
CreateNormalizeAttr(VtValue const & defaultValue,bool writeSparsely) const220 UsdLuxLightAPI::CreateNormalizeAttr(VtValue const &defaultValue, bool writeSparsely) const
221 {
222     return UsdSchemaBase::_CreateAttr(UsdLuxTokens->inputsNormalize,
223                        SdfValueTypeNames->Bool,
224                        /* custom = */ false,
225                        SdfVariabilityVarying,
226                        defaultValue,
227                        writeSparsely);
228 }
229 
230 UsdAttribute
GetColorAttr() const231 UsdLuxLightAPI::GetColorAttr() const
232 {
233     return GetPrim().GetAttribute(UsdLuxTokens->inputsColor);
234 }
235 
236 UsdAttribute
CreateColorAttr(VtValue const & defaultValue,bool writeSparsely) const237 UsdLuxLightAPI::CreateColorAttr(VtValue const &defaultValue, bool writeSparsely) const
238 {
239     return UsdSchemaBase::_CreateAttr(UsdLuxTokens->inputsColor,
240                        SdfValueTypeNames->Color3f,
241                        /* custom = */ false,
242                        SdfVariabilityVarying,
243                        defaultValue,
244                        writeSparsely);
245 }
246 
247 UsdAttribute
GetEnableColorTemperatureAttr() const248 UsdLuxLightAPI::GetEnableColorTemperatureAttr() const
249 {
250     return GetPrim().GetAttribute(UsdLuxTokens->inputsEnableColorTemperature);
251 }
252 
253 UsdAttribute
CreateEnableColorTemperatureAttr(VtValue const & defaultValue,bool writeSparsely) const254 UsdLuxLightAPI::CreateEnableColorTemperatureAttr(VtValue const &defaultValue, bool writeSparsely) const
255 {
256     return UsdSchemaBase::_CreateAttr(UsdLuxTokens->inputsEnableColorTemperature,
257                        SdfValueTypeNames->Bool,
258                        /* custom = */ false,
259                        SdfVariabilityVarying,
260                        defaultValue,
261                        writeSparsely);
262 }
263 
264 UsdAttribute
GetColorTemperatureAttr() const265 UsdLuxLightAPI::GetColorTemperatureAttr() const
266 {
267     return GetPrim().GetAttribute(UsdLuxTokens->inputsColorTemperature);
268 }
269 
270 UsdAttribute
CreateColorTemperatureAttr(VtValue const & defaultValue,bool writeSparsely) const271 UsdLuxLightAPI::CreateColorTemperatureAttr(VtValue const &defaultValue, bool writeSparsely) const
272 {
273     return UsdSchemaBase::_CreateAttr(UsdLuxTokens->inputsColorTemperature,
274                        SdfValueTypeNames->Float,
275                        /* custom = */ false,
276                        SdfVariabilityVarying,
277                        defaultValue,
278                        writeSparsely);
279 }
280 
281 UsdRelationship
GetFiltersRel() const282 UsdLuxLightAPI::GetFiltersRel() const
283 {
284     return GetPrim().GetRelationship(UsdLuxTokens->lightFilters);
285 }
286 
287 UsdRelationship
CreateFiltersRel() const288 UsdLuxLightAPI::CreateFiltersRel() const
289 {
290     return GetPrim().CreateRelationship(UsdLuxTokens->lightFilters,
291                        /* custom = */ false);
292 }
293 
294 namespace {
295 static inline TfTokenVector
_ConcatenateAttributeNames(const TfTokenVector & left,const TfTokenVector & right)296 _ConcatenateAttributeNames(const TfTokenVector& left,const TfTokenVector& right)
297 {
298     TfTokenVector result;
299     result.reserve(left.size() + right.size());
300     result.insert(result.end(), left.begin(), left.end());
301     result.insert(result.end(), right.begin(), right.end());
302     return result;
303 }
304 }
305 
306 /*static*/
307 const TfTokenVector&
GetSchemaAttributeNames(bool includeInherited)308 UsdLuxLightAPI::GetSchemaAttributeNames(bool includeInherited)
309 {
310     static TfTokenVector localNames = {
311         UsdLuxTokens->collectionLightLinkIncludeRoot,
312         UsdLuxTokens->collectionShadowLinkIncludeRoot,
313         UsdLuxTokens->lightShaderId,
314         UsdLuxTokens->lightMaterialSyncMode,
315         UsdLuxTokens->inputsIntensity,
316         UsdLuxTokens->inputsExposure,
317         UsdLuxTokens->inputsDiffuse,
318         UsdLuxTokens->inputsSpecular,
319         UsdLuxTokens->inputsNormalize,
320         UsdLuxTokens->inputsColor,
321         UsdLuxTokens->inputsEnableColorTemperature,
322         UsdLuxTokens->inputsColorTemperature,
323     };
324     static TfTokenVector allNames =
325         _ConcatenateAttributeNames(
326             UsdAPISchemaBase::GetSchemaAttributeNames(true),
327             localNames);
328 
329     if (includeInherited)
330         return allNames;
331     else
332         return localNames;
333 }
334 
335 PXR_NAMESPACE_CLOSE_SCOPE
336 
337 // ===================================================================== //
338 // Feel free to add custom code below this line. It will be preserved by
339 // the code generator.
340 //
341 // Just remember to wrap code in the appropriate delimiters:
342 // 'PXR_NAMESPACE_OPEN_SCOPE', 'PXR_NAMESPACE_CLOSE_SCOPE'.
343 // ===================================================================== //
344 // --(BEGIN CUSTOM CODE)--
345 
346 #include "pxr/usd/usdShade/connectableAPI.h"
347 #include "pxr/usd/usdShade/connectableAPIBehavior.h"
348 
349 PXR_NAMESPACE_OPEN_SCOPE
350 
351 class UsdLuxLightAPI_ConnectableAPIBehavior : public UsdShadeConnectableAPIBehavior
352 {
353 public:
354     // By default all UsdLuxLightAPI Connectable Behavior should be
355     // container and not exhibit encapsulation behavior, as we expect lights to
356     // be connected across multiple scopes, hence ignoring encapsulation rules.
UsdLuxLightAPI_ConnectableAPIBehavior()357     UsdLuxLightAPI_ConnectableAPIBehavior() :
358         UsdShadeConnectableAPIBehavior(
359                 true /*isContainer*/, false /*requiresEncapsulation*/) {}
360 
361     bool
CanConnectInputToSource(const UsdShadeInput & input,const UsdAttribute & source,std::string * reason) const362     CanConnectInputToSource(const UsdShadeInput &input,
363                             const UsdAttribute &source,
364                             std::string *reason) const override
365     {
366         return _CanConnectInputToSource(input, source, reason,
367                 ConnectableNodeTypes::DerivedContainerNodes);
368     }
369 
370     bool
CanConnectOutputToSource(const UsdShadeOutput & output,const UsdAttribute & source,std::string * reason) const371     CanConnectOutputToSource(const UsdShadeOutput &output,
372                              const UsdAttribute &source,
373                              std::string *reason) const override
374     {
375         return _CanConnectOutputToSource(output, source, reason,
376                 ConnectableNodeTypes::DerivedContainerNodes);
377     }
378 
379 };
380 
TF_REGISTRY_FUNCTION(UsdShadeConnectableAPI)381 TF_REGISTRY_FUNCTION(UsdShadeConnectableAPI)
382 {
383     UsdShadeRegisterConnectableAPIBehavior<
384         UsdLuxLightAPI, UsdLuxLightAPI_ConnectableAPIBehavior>();
385 }
386 
UsdLuxLightAPI(const UsdShadeConnectableAPI & connectable)387 UsdLuxLightAPI::UsdLuxLightAPI(const UsdShadeConnectableAPI &connectable)
388     : UsdLuxLightAPI(connectable.GetPrim())
389 {
390 }
391 
392 UsdShadeConnectableAPI
ConnectableAPI() const393 UsdLuxLightAPI::ConnectableAPI() const
394 {
395     return UsdShadeConnectableAPI(GetPrim());
396 }
397 
398 UsdShadeOutput
CreateOutput(const TfToken & name,const SdfValueTypeName & typeName)399 UsdLuxLightAPI::CreateOutput(const TfToken& name,
400                           const SdfValueTypeName& typeName)
401 {
402     return UsdShadeConnectableAPI(GetPrim()).CreateOutput(name, typeName);
403 }
404 
405 UsdShadeOutput
GetOutput(const TfToken & name) const406 UsdLuxLightAPI::GetOutput(const TfToken &name) const
407 {
408     return UsdShadeConnectableAPI(GetPrim()).GetOutput(name);
409 }
410 
411 std::vector<UsdShadeOutput>
GetOutputs(bool onlyAuthored) const412 UsdLuxLightAPI::GetOutputs(bool onlyAuthored) const
413 {
414     return UsdShadeConnectableAPI(GetPrim()).GetOutputs(onlyAuthored);
415 }
416 
417 UsdShadeInput
CreateInput(const TfToken & name,const SdfValueTypeName & typeName)418 UsdLuxLightAPI::CreateInput(const TfToken& name,
419                          const SdfValueTypeName& typeName)
420 {
421     return UsdShadeConnectableAPI(GetPrim()).CreateInput(name, typeName);
422 }
423 
424 UsdShadeInput
GetInput(const TfToken & name) const425 UsdLuxLightAPI::GetInput(const TfToken &name) const
426 {
427     return UsdShadeConnectableAPI(GetPrim()).GetInput(name);
428 }
429 
430 std::vector<UsdShadeInput>
GetInputs(bool onlyAuthored) const431 UsdLuxLightAPI::GetInputs(bool onlyAuthored) const
432 {
433     return UsdShadeConnectableAPI(GetPrim()).GetInputs(onlyAuthored);
434 }
435 
436 UsdCollectionAPI
GetLightLinkCollectionAPI() const437 UsdLuxLightAPI::GetLightLinkCollectionAPI() const
438 {
439     return UsdCollectionAPI(GetPrim(), UsdLuxTokens->lightLink);
440 }
441 
442 UsdCollectionAPI
GetShadowLinkCollectionAPI() const443 UsdLuxLightAPI::GetShadowLinkCollectionAPI() const
444 {
445     return UsdCollectionAPI(GetPrim(), UsdLuxTokens->shadowLink);
446 }
447 
448 static TfToken
_GetShaderIdAttrName(const TfToken & renderContext)449 _GetShaderIdAttrName(const TfToken &renderContext)
450 {
451     if (renderContext.IsEmpty()) {
452         return UsdLuxTokens->lightShaderId;
453     }
454     return TfToken(
455         SdfPath::JoinIdentifier(renderContext, UsdLuxTokens->lightShaderId));
456 }
457 
458 UsdAttribute
GetShaderIdAttrForRenderContext(const TfToken & renderContext) const459 UsdLuxLightAPI::GetShaderIdAttrForRenderContext(
460     const TfToken &renderContext) const
461 {
462     return GetPrim().GetAttribute(_GetShaderIdAttrName(renderContext));
463 }
464 
465 UsdAttribute
CreateShaderIdAttrForRenderContext(const TfToken & renderContext,VtValue const & defaultValue,bool writeSparsely) const466 UsdLuxLightAPI::CreateShaderIdAttrForRenderContext(
467     const TfToken &renderContext,
468     VtValue const &defaultValue,
469     bool writeSparsely) const
470 {
471     return UsdSchemaBase::_CreateAttr(_GetShaderIdAttrName(renderContext),
472                        SdfValueTypeNames->Token,
473                        /* custom = */ false,
474                        SdfVariabilityUniform,
475                        defaultValue,
476                        writeSparsely);
477 }
478 
479 TfToken
GetShaderId(const TfTokenVector & renderContexts) const480 UsdLuxLightAPI::GetShaderId(const TfTokenVector &renderContexts) const
481 {
482     TfToken shaderId;
483     // The passed in render contexts are in priority order so return the shader
484     // ID from the first render context specific shaderId attribute that has a
485     // a non-empty value.
486     for (const TfToken &renderContext : renderContexts) {
487         if (UsdAttribute shaderIdAttr =
488                 GetShaderIdAttrForRenderContext(renderContext)) {
489             shaderIdAttr.Get(&shaderId);
490             if (!shaderId.IsEmpty()) {
491                 return shaderId;
492             }
493         }
494     }
495     // Return the default shaderId attributes values if we couldn't get a value
496     // for any of the render contexts.
497     GetShaderIdAttr().Get(&shaderId);
498     return shaderId;
499 }
500 
501 PXR_NAMESPACE_CLOSE_SCOPE
502