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/usdShade/nodeDefAPI.h"
25 #include "pxr/usd/usd/schemaBase.h"
26 
27 #include "pxr/usd/sdf/primSpec.h"
28 
29 #include "pxr/usd/usd/pyConversions.h"
30 #include "pxr/base/tf/pyAnnotatedBoolResult.h"
31 #include "pxr/base/tf/pyContainerConversions.h"
32 #include "pxr/base/tf/pyResultConversions.h"
33 #include "pxr/base/tf/pyUtils.h"
34 #include "pxr/base/tf/wrapTypeHelpers.h"
35 
36 #include <boost/python.hpp>
37 
38 #include <string>
39 
40 using namespace boost::python;
41 
42 PXR_NAMESPACE_USING_DIRECTIVE
43 
44 namespace {
45 
46 #define WRAP_CUSTOM                                                     \
47     template <class Cls> static void _CustomWrapCode(Cls &_class)
48 
49 // fwd decl.
50 WRAP_CUSTOM;
51 
52 
53 static UsdAttribute
_CreateImplementationSourceAttr(UsdShadeNodeDefAPI & self,object defaultVal,bool writeSparsely)54 _CreateImplementationSourceAttr(UsdShadeNodeDefAPI &self,
55                                       object defaultVal, bool writeSparsely) {
56     return self.CreateImplementationSourceAttr(
57         UsdPythonToSdfType(defaultVal, SdfValueTypeNames->Token), writeSparsely);
58 }
59 
60 static UsdAttribute
_CreateIdAttr(UsdShadeNodeDefAPI & self,object defaultVal,bool writeSparsely)61 _CreateIdAttr(UsdShadeNodeDefAPI &self,
62                                       object defaultVal, bool writeSparsely) {
63     return self.CreateIdAttr(
64         UsdPythonToSdfType(defaultVal, SdfValueTypeNames->Token), writeSparsely);
65 }
66 
67 static std::string
_Repr(const UsdShadeNodeDefAPI & self)68 _Repr(const UsdShadeNodeDefAPI &self)
69 {
70     std::string primRepr = TfPyRepr(self.GetPrim());
71     return TfStringPrintf(
72         "UsdShade.NodeDefAPI(%s)",
73         primRepr.c_str());
74 }
75 
76 struct UsdShadeNodeDefAPI_CanApplyResult :
77     public TfPyAnnotatedBoolResult<std::string>
78 {
UsdShadeNodeDefAPI_CanApplyResult__anon0d79fd390111::UsdShadeNodeDefAPI_CanApplyResult79     UsdShadeNodeDefAPI_CanApplyResult(bool val, std::string const &msg) :
80         TfPyAnnotatedBoolResult<std::string>(val, msg) {}
81 };
82 
83 static UsdShadeNodeDefAPI_CanApplyResult
_WrapCanApply(const UsdPrim & prim)84 _WrapCanApply(const UsdPrim& prim)
85 {
86     std::string whyNot;
87     bool result = UsdShadeNodeDefAPI::CanApply(prim, &whyNot);
88     return UsdShadeNodeDefAPI_CanApplyResult(result, whyNot);
89 }
90 
91 } // anonymous namespace
92 
wrapUsdShadeNodeDefAPI()93 void wrapUsdShadeNodeDefAPI()
94 {
95     typedef UsdShadeNodeDefAPI This;
96 
97     UsdShadeNodeDefAPI_CanApplyResult::Wrap<UsdShadeNodeDefAPI_CanApplyResult>(
98         "_CanApplyResult", "whyNot");
99 
100     class_<This, bases<UsdAPISchemaBase> >
101         cls("NodeDefAPI");
102 
103     cls
104         .def(init<UsdPrim>(arg("prim")))
105         .def(init<UsdSchemaBase const&>(arg("schemaObj")))
106         .def(TfTypePythonClass())
107 
108         .def("Get", &This::Get, (arg("stage"), arg("path")))
109         .staticmethod("Get")
110 
111         .def("CanApply", &_WrapCanApply, (arg("prim")))
112         .staticmethod("CanApply")
113 
114         .def("Apply", &This::Apply, (arg("prim")))
115         .staticmethod("Apply")
116 
117         .def("GetSchemaAttributeNames",
118              &This::GetSchemaAttributeNames,
119              arg("includeInherited")=true,
120              return_value_policy<TfPySequenceToList>())
121         .staticmethod("GetSchemaAttributeNames")
122 
123         .def("_GetStaticTfType", (TfType const &(*)()) TfType::Find<This>,
124              return_value_policy<return_by_value>())
125         .staticmethod("_GetStaticTfType")
126 
127         .def(!self)
128 
129 
130         .def("GetImplementationSourceAttr",
131              &This::GetImplementationSourceAttr)
132         .def("CreateImplementationSourceAttr",
133              &_CreateImplementationSourceAttr,
134              (arg("defaultValue")=object(),
135               arg("writeSparsely")=false))
136 
137         .def("GetIdAttr",
138              &This::GetIdAttr)
139         .def("CreateIdAttr",
140              &_CreateIdAttr,
141              (arg("defaultValue")=object(),
142               arg("writeSparsely")=false))
143 
144         .def("__repr__", ::_Repr)
145     ;
146 
147     _CustomWrapCode(cls);
148 }
149 
150 // ===================================================================== //
151 // Feel free to add custom code below this line, it will be preserved by
152 // the code generator.  The entry point for your custom code should look
153 // minimally like the following:
154 //
155 // WRAP_CUSTOM {
156 //     _class
157 //         .def("MyCustomMethod", ...)
158 //     ;
159 // }
160 //
161 // Of course any other ancillary or support code may be provided.
162 //
163 // Just remember to wrap code in the appropriate delimiters:
164 // 'namespace {', '}'.
165 //
166 // ===================================================================== //
167 // --(BEGIN CUSTOM CODE)--
168 
169 namespace {
170 
171 static object
_WrapGetShaderId(const UsdShadeNodeDefAPI & shader)172 _WrapGetShaderId(const UsdShadeNodeDefAPI &shader)
173 {
174     TfToken id;
175     if (shader.GetShaderId(&id)) {
176         return object(id);
177     }
178     return object();
179 }
180 
181 static object
_WrapGetSourceAsset(const UsdShadeNodeDefAPI & shader,const TfToken & sourceType)182 _WrapGetSourceAsset(const UsdShadeNodeDefAPI &shader,
183                     const TfToken &sourceType)
184 {
185     SdfAssetPath asset;
186     if (shader.GetSourceAsset(&asset, sourceType)) {
187         return object(asset);
188     }
189     return object();
190 }
191 
192 static object
_WrapGetSourceAssetSubIdentifier(const UsdShadeNodeDefAPI & shader,const TfToken & sourceType)193 _WrapGetSourceAssetSubIdentifier(const UsdShadeNodeDefAPI &shader,
194                     const TfToken &sourceType)
195 {
196     TfToken subIdentifier;
197     if (shader.GetSourceAssetSubIdentifier(&subIdentifier, sourceType)) {
198         return object(subIdentifier);
199     }
200     return object();
201 }
202 
203 static object
_WrapGetSourceCode(const UsdShadeNodeDefAPI & shader,const TfToken & sourceType)204 _WrapGetSourceCode(const UsdShadeNodeDefAPI &shader,
205                    const TfToken &sourceType)
206 {
207     std::string code;
208     if (shader.GetSourceCode(&code, sourceType)) {
209         return object(code);
210     }
211     return object();
212 }
213 
214 WRAP_CUSTOM {
215     _class
216         .def("GetImplementationSource", &UsdShadeNodeDefAPI::GetImplementationSource)
217         .def("SetShaderId", &UsdShadeNodeDefAPI::SetShaderId)
218         .def("SetSourceAsset", &UsdShadeNodeDefAPI::SetSourceAsset,
219             (arg("sourceAsset"),
220              arg("sourceType")=UsdShadeTokens->universalSourceType))
221         .def("SetSourceAssetSubIdentifier",
222             &UsdShadeNodeDefAPI::SetSourceAssetSubIdentifier,
223             (arg("subIdentifier"),
224              arg("sourceType")=UsdShadeTokens->universalSourceType))
225         .def("SetSourceCode", &UsdShadeNodeDefAPI::SetSourceCode,
226             (arg("sourceCode"),
227              arg("sourceType")=UsdShadeTokens->universalSourceType))
228         .def("GetShaderId", _WrapGetShaderId)
229         .def("GetSourceAsset", _WrapGetSourceAsset,
230              arg("sourceType")=UsdShadeTokens->universalSourceType)
231         .def("GetSourceAssetSubIdentifier", _WrapGetSourceAssetSubIdentifier,
232              arg("sourceType")=UsdShadeTokens->universalSourceType)
233         .def("GetSourceCode", _WrapGetSourceCode,
234              arg("sourceType")=UsdShadeTokens->universalSourceType)
235         .def("GetShaderNodeForSourceType",
236              &UsdShadeNodeDefAPI::GetShaderNodeForSourceType,
237              (arg("sourceType")),
238              return_internal_reference<>())
239         ;
240 }
241 
242 }
243