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/materialBindingAPI.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 std::string
_Repr(const UsdShadeMaterialBindingAPI & self)54 _Repr(const UsdShadeMaterialBindingAPI &self)
55 {
56     std::string primRepr = TfPyRepr(self.GetPrim());
57     return TfStringPrintf(
58         "UsdShade.MaterialBindingAPI(%s)",
59         primRepr.c_str());
60 }
61 
62 struct UsdShadeMaterialBindingAPI_CanApplyResult :
63     public TfPyAnnotatedBoolResult<std::string>
64 {
UsdShadeMaterialBindingAPI_CanApplyResult__anon4b7a578e0111::UsdShadeMaterialBindingAPI_CanApplyResult65     UsdShadeMaterialBindingAPI_CanApplyResult(bool val, std::string const &msg) :
66         TfPyAnnotatedBoolResult<std::string>(val, msg) {}
67 };
68 
69 static UsdShadeMaterialBindingAPI_CanApplyResult
_WrapCanApply(const UsdPrim & prim)70 _WrapCanApply(const UsdPrim& prim)
71 {
72     std::string whyNot;
73     bool result = UsdShadeMaterialBindingAPI::CanApply(prim, &whyNot);
74     return UsdShadeMaterialBindingAPI_CanApplyResult(result, whyNot);
75 }
76 
77 } // anonymous namespace
78 
wrapUsdShadeMaterialBindingAPI()79 void wrapUsdShadeMaterialBindingAPI()
80 {
81     typedef UsdShadeMaterialBindingAPI This;
82 
83     UsdShadeMaterialBindingAPI_CanApplyResult::Wrap<UsdShadeMaterialBindingAPI_CanApplyResult>(
84         "_CanApplyResult", "whyNot");
85 
86     class_<This, bases<UsdAPISchemaBase> >
87         cls("MaterialBindingAPI");
88 
89     cls
90         .def(init<UsdPrim>(arg("prim")))
91         .def(init<UsdSchemaBase const&>(arg("schemaObj")))
92         .def(TfTypePythonClass())
93 
94         .def("Get", &This::Get, (arg("stage"), arg("path")))
95         .staticmethod("Get")
96 
97         .def("CanApply", &_WrapCanApply, (arg("prim")))
98         .staticmethod("CanApply")
99 
100         .def("Apply", &This::Apply, (arg("prim")))
101         .staticmethod("Apply")
102 
103         .def("GetSchemaAttributeNames",
104              &This::GetSchemaAttributeNames,
105              arg("includeInherited")=true,
106              return_value_policy<TfPySequenceToList>())
107         .staticmethod("GetSchemaAttributeNames")
108 
109         .def("_GetStaticTfType", (TfType const &(*)()) TfType::Find<This>,
110              return_value_policy<return_by_value>())
111         .staticmethod("_GetStaticTfType")
112 
113         .def(!self)
114 
115 
116         .def("__repr__", ::_Repr)
117     ;
118 
119     _CustomWrapCode(cls);
120 }
121 
122 // ===================================================================== //
123 // Feel free to add custom code below this line, it will be preserved by
124 // the code generator.  The entry point for your custom code should look
125 // minimally like the following:
126 //
127 // WRAP_CUSTOM {
128 //     _class
129 //         .def("MyCustomMethod", ...)
130 //     ;
131 // }
132 //
133 // Of course any other ancillary or support code may be provided.
134 //
135 // Just remember to wrap code in the appropriate delimiters:
136 // 'namespace {', '}'.
137 //
138 // ===================================================================== //
139 // --(BEGIN CUSTOM CODE)--
140 
141 #include <boost/python/tuple.hpp>
142 
143 namespace {
144 
145 static object
_WrapComputeBoundMaterial(const UsdShadeMaterialBindingAPI & bindingAPI,const TfToken & materialPurpose)146 _WrapComputeBoundMaterial(const UsdShadeMaterialBindingAPI &bindingAPI,
147                           const TfToken &materialPurpose) {
148     UsdRelationship bindingRel;
149     UsdShadeMaterial mat = bindingAPI.ComputeBoundMaterial(materialPurpose,
150             &bindingRel);
151     return boost::python::make_tuple(mat, bindingRel);
152 }
153 
154 static object
_WrapComputeBoundMaterials(const std::vector<UsdPrim> & prims,const TfToken & materialPurpose)155 _WrapComputeBoundMaterials(const std::vector<UsdPrim> &prims,
156                            const TfToken &materialPurpose)
157 {
158     std::vector<UsdRelationship> bindingRels;
159     auto materials = UsdShadeMaterialBindingAPI::ComputeBoundMaterials(prims,
160         materialPurpose, &bindingRels);
161     return boost::python::make_tuple(materials, bindingRels);
162 }
163 
164 WRAP_CUSTOM {
165 
166     using This = UsdShadeMaterialBindingAPI;
167 
168     // Create a root scope so that CollectionBinding is scoped under
169     // UsdShade.MaterialBindingAPI.
170     scope scope_root = _class;
171 
172     class_<This::DirectBinding> directBinding("DirectBinding");
173     directBinding
174         .def(init<>())
175         .def(init<UsdRelationship>(arg("bindingRel")))
176         .def("GetMaterial", &This::DirectBinding::GetMaterial)
177         .def("GetBindingRel", &This::DirectBinding::GetBindingRel,
178              return_value_policy<return_by_value>())
179         .def("GetMaterialPath", &This::DirectBinding::GetMaterialPath,
180              return_value_policy<return_by_value>())
181         .def("GetMaterialPurpose", &This::DirectBinding::GetMaterialPurpose,
182              return_value_policy<return_by_value>())
183         ;
184 
185     class_<This::CollectionBinding> collBinding("CollectionBinding");
186     collBinding
187         .def(init<>())
188         .def(init<UsdRelationship>(arg("collBindingRel")))
189         .def("GetCollection", &This::CollectionBinding::GetCollection)
190         .def("GetMaterial", &This::CollectionBinding::GetMaterial)
191         .def("GetCollectionPath", &This::CollectionBinding::GetCollectionPath,
192              return_value_policy<return_by_value>())
193         .def("GetMaterialPath", &This::CollectionBinding::GetMaterialPath,
194              return_value_policy<return_by_value>())
195         .def("GetBindingRel", &This::CollectionBinding::GetBindingRel,
196              return_value_policy<return_by_value>())
197         .def("IsValid", &This::CollectionBinding::IsValid)
198         ;
199 
200     to_python_converter<This::CollectionBindingVector,
201                         TfPySequenceToPython<This::CollectionBindingVector>>();
202     TfPyRegisterStlSequencesFromPython<This::CollectionBindingVector>();
203 
204     scope scope_materialBindingAPI = _class
205         .def("GetDirectBindingRel", &This::GetDirectBindingRel,
206              (arg("materialPurpose")=UsdShadeTokens->allPurpose))
207 
208         .def("GetCollectionBindingRel", &This::GetCollectionBindingRel,
209              (arg("bindingName"),
210               arg("materialPurpose")=UsdShadeTokens->allPurpose))
211 
212         .def("GetCollectionBindingRels", &This::GetCollectionBindingRels,
213              arg("materialPurpose")=UsdShadeTokens->allPurpose,
214              return_value_policy<TfPySequenceToList>())
215 
216         .def("GetMaterialBindingStrength", &This::GetMaterialBindingStrength,
217              arg("bindingRel"))
218              .staticmethod("GetMaterialBindingStrength")
219 
220         .def("SetMaterialBindingStrength", &This::SetMaterialBindingStrength,
221              arg("bindingRel"))
222              .staticmethod("SetMaterialBindingStrength")
223 
224         .def("GetDirectBinding", &This::GetDirectBinding,
225              (arg("materialPurpose")=UsdShadeTokens->allPurpose))
226 
227         .def("GetCollectionBindings", &This::GetCollectionBindings,
228              arg("materialPurpose")=UsdShadeTokens->allPurpose,
229              return_value_policy<TfPySequenceToList>())
230 
231         .def("Bind", (bool(This::*)(const UsdShadeMaterial &,
232                               const TfToken &,
233                               const TfToken &) const) &This::Bind,
234              (arg("material"),
235               arg("bindingStrength")=UsdShadeTokens->fallbackStrength,
236               arg("materialPurpose")=UsdShadeTokens->allPurpose))
237 
238         .def("Bind", (bool(This::*)(const UsdCollectionAPI &collection,
239                                     const UsdShadeMaterial &,
240                                     const TfToken &,
241                                     const TfToken &,
242                                     const TfToken &) const) &This::Bind,
243              (arg("collection"),
244               arg("material"),
245               arg("bindingName")=TfToken(),
246               arg("bindingStrength")=UsdShadeTokens->fallbackStrength,
247               arg("materialPurpose")=UsdShadeTokens->allPurpose))
248 
249         .def("UnbindDirectBinding", &This::UnbindDirectBinding,
250              arg("materialPurpose")=UsdShadeTokens->allPurpose)
251 
252         .def("UnbindCollectionBinding", &This::UnbindCollectionBinding,
253              (arg("bindingName"),
254               arg("materialPurpose")=UsdShadeTokens->allPurpose))
255 
256         .def("UnbindAllBindings", &This::UnbindAllBindings)
257 
258         .def("RemovePrimFromBindingCollection",
259              &This::RemovePrimFromBindingCollection,
260              (arg("prim"), arg("bindingName"),
261               arg("materialPurpose")=UsdShadeTokens->allPurpose))
262 
263         .def("AddPrimToBindingCollection",
264              &This::AddPrimToBindingCollection,
265              (arg("prim"), arg("bindingName"),
266               arg("materialPurpose")=UsdShadeTokens->allPurpose))
267 
268         .def("ComputeBoundMaterial", &_WrapComputeBoundMaterial,
269              arg("materialPurpose")=UsdShadeTokens->allPurpose)
270 
271         .def("ComputeBoundMaterials", &_WrapComputeBoundMaterials,
272              (arg("prims"), arg("materialPurpose")=UsdShadeTokens->allPurpose))
273             .staticmethod("ComputeBoundMaterials")
274 
275         .def("CreateMaterialBindSubset",
276              &This::CreateMaterialBindSubset,
277              (arg("subsetName"),
278               arg("indices"), arg("elementType")=UsdGeomTokens->face))
279         .def("GetMaterialBindSubsets",
280              &This::GetMaterialBindSubsets,
281              return_value_policy<TfPySequenceToList>())
282         .def("SetMaterialBindSubsetsFamilyType",
283              &This::SetMaterialBindSubsetsFamilyType,
284              (arg("familyType")))
285         .def("GetMaterialBindSubsetsFamilyType",
286              &This::GetMaterialBindSubsetsFamilyType)
287         .def("CanContainPropertyName",
288             &UsdShadeMaterialBindingAPI::CanContainPropertyName,
289             arg("name"))
290         .staticmethod("CanContainPropertyName")
291     ;
292 }
293 
294 }
295