All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pyResolverContext_v1.h
Go to the documentation of this file.
1 //
2 // Copyright 2020 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 INCLUDE_AR_PY_RESOLVER_CONTEXT
25 #error This file should not be included directly. Include pyResolverContext.h instead
26 #endif
27 
28 #ifndef PXR_USD_AR_PY_RESOLVER_CONTEXT_V1_H
29 #define PXR_USD_AR_PY_RESOLVER_CONTEXT_V1_H
30 
34 
36 
37 #include <boost/python/extract.hpp>
38 #include <boost/python/object.hpp>
39 
40 #include "pxr/pxr.h"
41 #include "pxr/usd/ar/api.h"
43 
44 #include "pxr/base/tf/pyLock.h"
45 #include "pxr/base/tf/pyObjWrapper.h"
46 
47 #include <functional>
48 
49 PXR_NAMESPACE_OPEN_SCOPE
50 
56 template <class Context>
57 void
59 
60 #ifndef doxygen
61 // Private helper functions for converting ArResolverContext
62 // objects to and from Python.
63 
64 template <class Context>
65 bool
66 Ar_ConvertResolverContextFromPython(
67  PyObject* obj,
68  ArResolverContext* context)
69 {
70  boost::python::extract<const Context&> x(obj);
71  if (x.check()) {
72  if (context) {
73  *context = ArResolverContext(x());
74  }
75  return true;
76  }
77  return false;
78 }
79 
80 template <class Context>
81 bool
82 Ar_ConvertResolverContextToPython(
83  const ArResolverContext& context,
84  TfPyObjWrapper* obj)
85 {
86  if (const Context* contextObj = context.Get<Context>()) {
87  if (obj) {
88  TfPyLock lock;
89  *obj = boost::python::object(*contextObj);
90  }
91  return true;
92  }
93  return false;
94 }
95 
96 typedef std::function<bool(PyObject*, ArResolverContext*)>
97  Ar_MakeResolverContextFromPythonFn;
98 typedef std::function<bool(const ArResolverContext&, TfPyObjWrapper*)>
99  Ar_ResolverContextToPythonFn;
100 
101 AR_API
102 void
103 Ar_RegisterResolverContextPythonConversion(
104  const Ar_MakeResolverContextFromPythonFn& convertFunc,
105  const Ar_ResolverContextToPythonFn& getObjectFunc);
106 
107 AR_API
108 bool
109 Ar_CanConvertResolverContextFromPython(PyObject* pyObj);
110 
111 AR_API
113 Ar_ConvertResolverContextFromPython(PyObject* pyObj);
114 
115 AR_API
117 Ar_ConvertResolverContextToPython(const ArResolverContext& context);
118 
119 template <class Context>
120 void
122 {
123  Ar_RegisterResolverContextPythonConversion(
124  Ar_ConvertResolverContextFromPython<Context>,
125  Ar_ConvertResolverContextToPython<Context>);
126 };
127 
128 #endif //doxygen
129 
130 PXR_NAMESPACE_CLOSE_SCOPE
131 
132 #endif
void ArWrapResolverContextForPython()
Register the specified type as a context object that may be converted from a Python into a ArResolver...
Intended to replace a direct include of Python.h, which causes several build problems with certain co...
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...