Loading...
Searching...
No Matches
pyListEditorProxy.h
Go to the documentation of this file.
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_USD_SDF_PY_LIST_EDITOR_PROXY_H
8#define PXR_USD_SDF_PY_LIST_EDITOR_PROXY_H
9
11
12#include "pxr/pxr.h"
14#include "pxr/usd/sdf/listOp.h"
16
19#include "pxr/base/tf/pyCall.h"
20#include "pxr/base/tf/pyResultConversions.h"
21#include "pxr/base/tf/pyLock.h"
22#include "pxr/base/tf/pyUtils.h"
24#include "pxr/external/boost/python.hpp"
25
26PXR_NAMESPACE_OPEN_SCOPE
27
28class Sdf_PyListEditorUtils {
29public:
30 template <class T, class V>
31 class ApplyHelper {
32 public:
33 ApplyHelper(const T& owner, const pxr_boost::python::object& callback) :
34 _owner(owner),
35 _callback(callback)
36 {
37 // Do nothing
38 }
39
40 std::optional<V> operator()(SdfListOpType op, const V& value)
41 {
42 using namespace pxr_boost::python;
43
44 TfPyLock pyLock;
45 object result = _callback(_owner, value, op);
46 if (! TfPyIsNone(result)) {
47 extract<V> e(result);
48 if (e.check()) {
49 return std::optional<V>(e());
50 }
51 else {
52 TF_CODING_ERROR("ApplyEditsToList callback has "
53 "incorrect return type.");
54 }
55 }
56 return std::optional<V>();
57 }
58
59 private:
60 const T& _owner;
62 };
63
64 template <class V>
65 class ModifyHelper {
66 public:
67 ModifyHelper(const pxr_boost::python::object& callback) :
68 _callback(callback)
69 {
70 // Do nothing
71 }
72
73 std::optional<V> operator()(const V& value)
74 {
75 using namespace pxr_boost::python;
76
77 TfPyLock pyLock;
78 object result = _callback(value);
79 if (! TfPyIsNone(result)) {
80 extract<V> e(result);
81 if (e.check()) {
82 return std::optional<V>(e());
83 }
84 else {
85 TF_CODING_ERROR("ModifyItemEdits callback has "
86 "incorrect return type.");
87 }
88 }
89 return std::optional<V>();
90 }
91
92 private:
94 };
95};
96
97template <class T>
98class SdfPyWrapListEditorProxy {
99public:
100 typedef T Type;
101 typedef typename Type::TypePolicy TypePolicy;
102 typedef typename Type::value_type value_type;
103 typedef typename Type::value_vector_type value_vector_type;
104 typedef typename Type::ApplyCallback ApplyCallback;
105 typedef typename Type::ModifyCallback ModifyCallback;
106 typedef SdfPyWrapListEditorProxy<Type> This;
107 typedef SdfListProxy<TypePolicy> ListProxy;
108
109 SdfPyWrapListEditorProxy()
110 {
111 TfPyWrapOnce<Type>(&This::_Wrap);
112 SdfPyWrapListProxy<ListProxy>();
113 }
114
115private:
116 static void _Wrap()
117 {
118 using namespace pxr_boost::python;
119
120 class_<Type>(_GetName().c_str(), no_init)
121 .def("__str__", &This::_GetStr)
122 .add_property("isExpired", &Type::IsExpired)
123 .add_property("explicitItems",
124 &Type::GetExplicitItems,
125 &This::_SetExplicitProxy)
126 .add_property("addedItems",
127 &Type::GetAddedItems,
128 &This::_SetAddedProxy)
129 .add_property("prependedItems",
130 &Type::GetPrependedItems,
131 &This::_SetPrependedProxy)
132 .add_property("appendedItems",
133 &Type::GetAppendedItems,
134 &This::_SetAppendedProxy)
135 .add_property("deletedItems",
136 &Type::GetDeletedItems,
137 &This::_SetDeletedProxy)
138 .add_property("orderedItems",
139 &Type::GetOrderedItems,
140 &This::_SetOrderedProxy)
141 .def("GetAddedOrExplicitItems", &Type::GetAppliedItems,
142 return_value_policy<TfPySequenceToTuple>()) // deprecated
143 .def("GetAppliedItems", &Type::GetAppliedItems,
144 return_value_policy<TfPySequenceToTuple>())
145 .add_property("isExplicit", &Type::IsExplicit)
146 .add_property("isOrderedOnly", &Type::IsOrderedOnly)
147 .def("ApplyEditsToList",
148 &This::_ApplyEditsToList,
149 return_value_policy<TfPySequenceToList>())
150 .def("ApplyEditsToList",
151 &This::_ApplyEditsToList2,
152 return_value_policy<TfPySequenceToList>())
153
154 .def("CopyItems", &Type::CopyItems)
155 .def("ClearEdits", &Type::ClearEdits)
156 .def("ClearEditsAndMakeExplicit", &Type::ClearEditsAndMakeExplicit)
157 .def("ContainsItemEdit", &Type::ContainsItemEdit,
158 (arg("item"), arg("onlyAddOrExplicit")=false))
159 .def("RemoveItemEdits", &Type::RemoveItemEdits)
160 .def("ReplaceItemEdits", &Type::ReplaceItemEdits)
161 .def("ModifyItemEdits", &This::_ModifyEdits)
162
163 // New API (see bug 8710)
164 .def("Add", &Type::Add)
165 .def("Prepend", &Type::Prepend)
166 .def("Append", &Type::Append)
167 .def("Remove", &Type::Remove)
168 .def("Erase", &Type::Erase)
169 ;
170 }
171
172 static std::string _GetName()
173 {
174 std::string name = "ListEditorProxy_" +
175 ArchGetDemangled<TypePolicy>();
176 name = TfStringReplace(name, " ", "_");
177 name = TfStringReplace(name, ",", "_");
178 name = TfStringReplace(name, "::", "_");
179 name = TfStringReplace(name, "<", "_");
180 name = TfStringReplace(name, ">", "_");
181 return name;
182 }
183
184 static std::string _GetStr(const Type& x)
185 {
186 return x._listEditor ? TfStringify(*x._listEditor) : std::string();
187 }
188
189 static void _SetExplicitProxy(Type& x, const value_vector_type& v)
190 {
191 x.GetExplicitItems() = v;
192 }
193
194 static void _SetAddedProxy(Type& x, const value_vector_type& v)
195 {
196 x.GetAddedItems() = v;
197 }
198
199 static void _SetPrependedProxy(Type& x, const value_vector_type& v)
200 {
201 x.GetPrependedItems() = v;
202 }
203
204 static void _SetAppendedProxy(Type& x, const value_vector_type& v)
205 {
206 x.GetAppendedItems() = v;
207 }
208
209 static void _SetDeletedProxy(Type& x, const value_vector_type& v)
210 {
211 x.GetDeletedItems() = v;
212 }
213
214 static void _SetOrderedProxy(Type& x, const value_vector_type& v)
215 {
216 x.GetOrderedItems() = v;
217 }
218
219 static value_vector_type _ApplyEditsToList(const Type& x,
220 const value_vector_type& v)
221 {
222 value_vector_type tmp = v;
223 x.ApplyEditsToList(&tmp);
224 return tmp;
225 }
226
227 static value_vector_type _ApplyEditsToList2(const Type& x,
228 const value_vector_type& v,
229 const pxr_boost::python::object& cb)
230 {
231 value_vector_type tmp = v;
232 x.ApplyEditsToList(&tmp,
233 Sdf_PyListEditorUtils::ApplyHelper<Type, value_type>(x, cb));
234 return tmp;
235 }
236
237 static void _ModifyEdits(Type& x, const pxr_boost::python::object& cb)
238 {
239 x.ModifyItemEdits(Sdf_PyListEditorUtils::ModifyHelper<value_type>(cb));
240 }
241};
242
243PXR_NAMESPACE_CLOSE_SCOPE
244
245#endif // PXR_USD_SDF_PY_LIST_EDITOR_PROXY_H
Low-level utilities for informing users of various internal and external diagnostic conditions.
Miscellaneous Utilities for dealing with script.
TF_API bool TfPyIsNone(pxr_boost::python::object const &obj)
Return true iff obj is None.
Represents a single list of list editing operations.
Definition: listProxy.h:37
Convenience class for accessing the Python Global Interpreter Lock.
Definition: pyLock.h:105
Demangle C++ typenames generated by the typeid() facility.
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
Definition: diagnostic.h:68
std::string TfStringify(const T &v)
Convert an arbitrary type into a string.
Definition: stringUtils.h:562
TF_API std::string TfStringReplace(const std::string &source, const std::string &from, const std::string &to)
Replaces all occurrences of string from with to in source.
STL namespace.
Utilities for calling python callables.
Definitions of basic string utilities in tf.
Provide a way to call a Python callable.
Definition: pyCall.h:40