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
25 #include "pxr/pxr.h"
26 #include "pxr/base/gf/matrix4d.h"
27 #include "pxr/base/gf/rotation.h"
28 #include "pxr/base/gf/transform.h"
29 #include "pxr/base/gf/vec3d.h"
30
31 #include "pxr/base/tf/stringUtils.h"
32 #include "pxr/base/tf/pyUtils.h"
33 #include "pxr/base/tf/wrapTypeHelpers.h"
34
35 #include <boost/python/args.hpp>
36 #include <boost/python/class.hpp>
37 #include <boost/python/copy_const_reference.hpp>
38 #include <boost/python/init.hpp>
39 #include <boost/python/operators.hpp>
40 #include <boost/python/return_arg.hpp>
41
42 #include <string>
43
44 using std::string;
45 using std::vector;
46 using namespace boost::python;
47
48 PXR_NAMESPACE_USING_DIRECTIVE
49
50 namespace {
51
_NoTranslation()52 static GfVec3d _NoTranslation() { return GfVec3d(0,0,0); }
_IdentityScale()53 static GfVec3d _IdentityScale() { return GfVec3d(1,1,1); }
_NoRotation()54 static GfRotation _NoRotation() { return GfRotation( GfVec3d::XAxis(), 0.0 ); }
55
_Repr(GfTransform const & self)56 static string _Repr(GfTransform const &self)
57 {
58 string prefix = TF_PY_REPR_PREFIX + "Transform(";
59 string indent(prefix.size(), ' ');
60
61 // Use keyword args for clarity.
62 // Only use args that do not match the defaults.
63 vector<string> kwargs;
64 if (self.GetTranslation() != _NoTranslation())
65 kwargs.push_back( "translation = " +
66 TfPyRepr(self.GetTranslation()) );
67 if (self.GetRotation() != _NoRotation())
68 kwargs.push_back( "rotation = " +
69 TfPyRepr(self.GetRotation()) );
70 if (self.GetScale() != _IdentityScale())
71 kwargs.push_back( "scale = " +
72 TfPyRepr(self.GetScale()) );
73 if (self.GetPivotPosition() != _NoTranslation())
74 kwargs.push_back( "pivotPosition = " +
75 TfPyRepr(self.GetPivotPosition()) );
76 if (self.GetPivotOrientation() != _NoRotation())
77 kwargs.push_back( "pivotOrientation = " +
78 TfPyRepr(self.GetPivotOrientation()) );
79
80 return prefix + TfStringJoin(kwargs, string(", \n" + indent).c_str()) + ")";
81 }
82
83 } // anonymous namespace
84
wrapTransform()85 void wrapTransform()
86 {
87 typedef GfTransform This;
88
89 class_<This>( "Transform", init<>() )
90
91 .def(init<const GfVec3d&, const GfRotation&, const GfVec3d&,
92 const GfVec3d&, const GfRotation&>
93 ((args("translation") = _NoTranslation(),
94 args("rotation") = _NoRotation(),
95 args("scale") = _IdentityScale(),
96 args("pivotPosition") = _NoTranslation(),
97 args("pivotOrientation") = _NoRotation()),
98 "Initializer used by 3x code."))
99
100 // This is the constructor used by 2x code. Leave the initial
101 // arguments as non-default to force the user to provide enough
102 // values to indicate her intentions.
103 .def(init<const GfVec3d &, const GfRotation &, const GfRotation&,
104 const GfVec3d &, const GfVec3d &>
105 ((args("scale"),
106 args("pivotOrientation"),
107 args("rotation"),
108 args("pivotPosition"),
109 args("translation")),
110 "Initializer used by old 2x code. (Deprecated)"))
111
112 .def(init<const GfMatrix4d &>())
113
114 .def( TfTypePythonClass() )
115
116 .def( "Set",
117 (This & (This::*)( const GfVec3d &, const GfRotation &,
118 const GfVec3d &, const GfVec3d &,
119 const GfRotation & ))( &This::Set ),
120 return_self<>(),
121 (args("translation") = _NoTranslation(),
122 args("rotation") = _NoRotation(),
123 args("scale") = _IdentityScale(),
124 args("pivotPosition") = _NoTranslation(),
125 args("pivotOrientation") = _NoRotation()))
126
127 .def( "Set",
128 (This & (This::*)( const GfVec3d &, const GfRotation &,
129 const GfRotation &, const GfVec3d &,
130 const GfVec3d &))&This::Set,
131 return_self<>(),
132 (args("scale"),
133 args("pivotOrientation"),
134 args("rotation"),
135 args("pivotPosition"),
136 args("translation")),
137 "Set method used by old 2x code. (Deprecated)")
138
139 .def( "SetMatrix", &This::SetMatrix, return_self<>() )
140 .def( "GetMatrix", &This::GetMatrix )
141
142 .def( "SetIdentity", &This::SetIdentity, return_self<>() )
143
144 .add_property( "translation", make_function
145 (&This::GetTranslation,
146 return_value_policy<return_by_value>()),
147 &This::SetTranslation )
148
149 .add_property( "rotation", make_function
150 (&This::GetRotation,
151 return_value_policy<return_by_value>()),
152 &This::SetRotation )
153
154 .add_property( "scale", make_function
155 (&This::GetScale,
156 return_value_policy<return_by_value>()),
157 &This::SetScale )
158
159 .add_property( "pivotPosition", make_function
160 (&This::GetPivotPosition,
161 return_value_policy<return_by_value>()),
162 &This::SetPivotPosition )
163
164 .add_property( "pivotOrientation", make_function
165 (&This::GetPivotOrientation,
166 return_value_policy<return_by_value>()),
167 &This::SetPivotOrientation )
168
169 .def("GetTranslation", &This::GetTranslation,
170 return_value_policy<return_by_value>())
171 .def("SetTranslation", &This::SetTranslation )
172
173 .def("GetRotation", &This::GetRotation,
174 return_value_policy<return_by_value>())
175 .def("SetRotation", &This::SetRotation)
176
177 .def("GetScale", &This::GetScale,
178 return_value_policy<return_by_value>())
179 .def("SetScale", &This::SetScale )
180
181 .def("GetPivotPosition", &This::GetPivotPosition,
182 return_value_policy<return_by_value>())
183 .def("SetPivotPosition", &This::SetPivotPosition )
184
185 .def("GetPivotOrientation", &This::GetPivotOrientation,
186 return_value_policy<return_by_value>())
187 .def("SetPivotOrientation", &This::SetPivotOrientation )
188
189
190 .def( str(self) )
191 .def( self == self )
192 .def( self != self )
193 .def( self *= self )
194 .def( self * self )
195
196 .def("__repr__", _Repr)
197
198 ;
199
200 }
201