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/points.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/pyContainerConversions.h"
31 #include "pxr/base/tf/pyResultConversions.h"
32 #include "pxr/base/tf/pyUtils.h"
33 #include "pxr/base/tf/wrapTypeHelpers.h"
34 
35 #include <boost/python.hpp>
36 
37 #include <string>
38 
39 using namespace boost::python;
40 
41 PXR_NAMESPACE_USING_DIRECTIVE
42 
43 namespace {
44 
45 #define WRAP_CUSTOM                                                     \
46     template <class Cls> static void _CustomWrapCode(Cls &_class)
47 
48 // fwd decl.
49 WRAP_CUSTOM;
50 
51 
52 static UsdAttribute
_CreateWidthsAttr(UsdGeomPoints & self,object defaultVal,bool writeSparsely)53 _CreateWidthsAttr(UsdGeomPoints &self,
54                                       object defaultVal, bool writeSparsely) {
55     return self.CreateWidthsAttr(
56         UsdPythonToSdfType(defaultVal, SdfValueTypeNames->FloatArray), writeSparsely);
57 }
58 
59 static UsdAttribute
_CreateIdsAttr(UsdGeomPoints & self,object defaultVal,bool writeSparsely)60 _CreateIdsAttr(UsdGeomPoints &self,
61                                       object defaultVal, bool writeSparsely) {
62     return self.CreateIdsAttr(
63         UsdPythonToSdfType(defaultVal, SdfValueTypeNames->Int64Array), writeSparsely);
64 }
65 
66 static std::string
_Repr(const UsdGeomPoints & self)67 _Repr(const UsdGeomPoints &self)
68 {
69     std::string primRepr = TfPyRepr(self.GetPrim());
70     return TfStringPrintf(
71         "UsdGeom.Points(%s)",
72         primRepr.c_str());
73 }
74 
75 } // anonymous namespace
76 
wrapUsdGeomPoints()77 void wrapUsdGeomPoints()
78 {
79     typedef UsdGeomPoints This;
80 
81     class_<This, bases<UsdGeomPointBased> >
82         cls("Points");
83 
84     cls
85         .def(init<UsdPrim>(arg("prim")))
86         .def(init<UsdSchemaBase const&>(arg("schemaObj")))
87         .def(TfTypePythonClass())
88 
89         .def("Get", &This::Get, (arg("stage"), arg("path")))
90         .staticmethod("Get")
91 
92         .def("Define", &This::Define, (arg("stage"), arg("path")))
93         .staticmethod("Define")
94 
95         .def("GetSchemaAttributeNames",
96              &This::GetSchemaAttributeNames,
97              arg("includeInherited")=true,
98              return_value_policy<TfPySequenceToList>())
99         .staticmethod("GetSchemaAttributeNames")
100 
101         .def("_GetStaticTfType", (TfType const &(*)()) TfType::Find<This>,
102              return_value_policy<return_by_value>())
103         .staticmethod("_GetStaticTfType")
104 
105         .def(!self)
106 
107 
108         .def("GetWidthsAttr",
109              &This::GetWidthsAttr)
110         .def("CreateWidthsAttr",
111              &_CreateWidthsAttr,
112              (arg("defaultValue")=object(),
113               arg("writeSparsely")=false))
114 
115         .def("GetIdsAttr",
116              &This::GetIdsAttr)
117         .def("CreateIdsAttr",
118              &_CreateIdsAttr,
119              (arg("defaultValue")=object(),
120               arg("writeSparsely")=false))
121 
122         .def("__repr__", ::_Repr)
123     ;
124 
125     _CustomWrapCode(cls);
126 }
127 
128 // ===================================================================== //
129 // Feel free to add custom code below this line, it will be preserved by
130 // the code generator.  The entry point for your custom code should look
131 // minimally like the following:
132 //
133 // WRAP_CUSTOM {
134 //     _class
135 //         .def("MyCustomMethod", ...)
136 //     ;
137 // }
138 //
139 // Of course any other ancillary or support code may be provided.
140 //
141 // Just remember to wrap code in the appropriate delimiters:
142 // 'namespace {', '}'.
143 //
144 // ===================================================================== //
145 // --(BEGIN CUSTOM CODE)--
146 
147 namespace {
148 
149 static TfPyObjWrapper
_ComputeExtent(object points,object widths)150 _ComputeExtent(object points, object widths) {
151 
152     // Convert from python objects to VtValue
153     VtVec3fArray extent;
154     VtValue pointsAsVtValue = UsdPythonToSdfType(points,
155         SdfValueTypeNames->Float3Array);
156     VtValue widthsAsVtValue = UsdPythonToSdfType(widths,
157         SdfValueTypeNames->FloatArray);
158 
159     // Check Proper conversion to VtVec3fArray
160     if (!pointsAsVtValue.IsHolding<VtVec3fArray>()) {
161         TF_CODING_ERROR("Improper value for 'points'");
162         return object();
163     }
164 
165     if (!widthsAsVtValue.IsHolding<VtFloatArray>()) {
166         TF_CODING_ERROR("Improper value for 'widths'");
167         return object();
168     }
169 
170     // Convert from VtValue to VtVec3fArray
171     VtVec3fArray pointsArray = pointsAsVtValue.UncheckedGet<VtVec3fArray>();
172     VtFloatArray widthsArray = widthsAsVtValue.UncheckedGet<VtFloatArray>();
173 
174     if (UsdGeomPoints::ComputeExtent(pointsArray, widthsArray, &extent)) {
175         return UsdVtValueToPython(VtValue(extent));
176     } else {
177         return object();
178     }
179 }
180 
181 WRAP_CUSTOM {
182     _class
183         .def("GetWidthsInterpolation", &UsdGeomPoints::GetWidthsInterpolation)
184         .def("SetWidthsInterpolation", &UsdGeomPoints::SetWidthsInterpolation,
185              arg("interpolation"))
186 
187         .def("ComputeExtent",
188             &_ComputeExtent,
189             (arg("points"), arg("widths")))
190         .def("GetPointCount", &UsdGeomPoints::GetPointCount,
191             arg("timeCode")=UsdTimeCode::Default())
192         .staticmethod("ComputeExtent")
193 
194         ;
195 }
196 
197 } // anonymous namespace
198