All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pyResolverContext.h
Go to the documentation of this file.
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 #ifndef AR_PY_RESOLVER_CONTEXT_H
25 #define AR_PY_RESOLVER_CONTEXT_H
26 
30 
31 #include <Python.h>
32 #include <boost/python/extract.hpp>
33 #include <boost/python/object.hpp>
34 
35 #include "pxr/pxr.h"
36 #include "pxr/usd/ar/api.h"
37 #include "pxr/usd/ar/resolverContext.h"
38 
39 #include "pxr/base/tf/pyLock.h"
40 #include "pxr/base/tf/pyObjWrapper.h"
41 
42 #include <functional>
43 
44 PXR_NAMESPACE_OPEN_SCOPE
45 
51 template <class Context>
52 void
54 
55 #ifndef doxygen
56 // Private helper functions for converting ArResolverContext
57 // objects to and from Python.
58 
59 template <class Context>
60 bool
61 Ar_ConvertResolverContextFromPython(
62  PyObject* obj,
63  ArResolverContext* context)
64 {
65  boost::python::extract<const Context&> x(obj);
66  if (x.check()) {
67  if (context) {
68  *context = ArResolverContext(x());
69  }
70  return true;
71  }
72  return false;
73 }
74 
75 template <class Context>
76 bool
77 Ar_ConvertResolverContextToPython(
78  const ArResolverContext& context,
79  TfPyObjWrapper* obj)
80 {
81  if (const Context* contextObj = context.Get<Context>()) {
82  if (obj) {
83  TfPyLock lock;
84  *obj = boost::python::object(*contextObj);
85  }
86  return true;
87  }
88  return false;
89 }
90 
91 typedef std::function<bool(PyObject*, ArResolverContext*)>
92  Ar_MakeResolverContextFromPythonFn;
93 typedef std::function<bool(const ArResolverContext&, TfPyObjWrapper*)>
94  Ar_ResolverContextToPythonFn;
95 
96 AR_API
97 void
98 Ar_RegisterResolverContextPythonConversion(
99  const Ar_MakeResolverContextFromPythonFn& convertFunc,
100  const Ar_ResolverContextToPythonFn& getObjectFunc);
101 
102 AR_API
103 bool
104 Ar_CanConvertResolverContextFromPython(PyObject* pyObj);
105 
106 AR_API
108 Ar_ConvertResolverContextFromPython(PyObject* pyObj);
109 
110 AR_API
112 Ar_ConvertResolverContextToPython(const ArResolverContext& context);
113 
114 template <class Context>
115 void
117 {
118  Ar_RegisterResolverContextPythonConversion(
119  Ar_ConvertResolverContextFromPython<Context>,
120  Ar_ConvertResolverContextToPython<Context>);
121 };
122 
123 #endif //doxygen
124 
125 PXR_NAMESPACE_CLOSE_SCOPE
126 
127 #endif // AR_PY_RESOLVER_CONTEXT_H
void ArWrapResolverContextForPython()
Register the specified type as a context object that may be converted from a Python into a ArResolver...
Boost Python object wrapper.
Definition: pyObjWrapper.h:66
An asset resolver context allows clients to provide additional data to the resolver for use during re...
const Context * Get() const
Return pointer to the context object held in this asset resolver context if the context is holding an...