Skip to content

Commit

Permalink
Merge PR PixarAnimationStudios#1729 from Autodesk for their Maya USD …
Browse files Browse the repository at this point in the history
…plugin to load in 2023
  • Loading branch information
Nicolas Popravka committed Jan 9, 2023
1 parent 0b3325b commit 13e0046
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 64 deletions.
2 changes: 0 additions & 2 deletions pxr/base/plug/info.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ class Plug_RegistrationMetadata {
enum Type {
UnknownType,
LibraryType,
#ifdef PXR_PYTHON_SUPPORT_ENABLED
PythonType,
#endif // PXR_PYTHON_SUPPORT_ENABLED
ResourceType
};

Expand Down
2 changes: 0 additions & 2 deletions pxr/base/plug/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ class PlugPlugin : public TfRefBase, public TfWeakBase {
private:
enum _Type {
LibraryType,
#ifdef PXR_PYTHON_SUPPORT_ENABLED
PythonType,
#endif // PXR_PYTHON_SUPPORT_ENABLED
ResourceType
};

Expand Down
5 changes: 3 additions & 2 deletions pxr/base/tf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ pxr_library(tf
pathUtils
patternMatcher
pointerAndBits
pyBoostObjWrapper
pyLock
pyObjWrapper
pyTracing
refBase
refCount
refPtr
Expand Down Expand Up @@ -103,14 +106,12 @@ pxr_library(tf
pyModuleNotice
pyNoticeWrapper
pyObjectFinder
pyObjWrapper
pyOptional
pyOverride
pyPolymorphic
pyPtrHelpers
pyResultConversions
pySingleton
pyTracing
pyUtils
pyWrapContext
scriptModuleLoader
Expand Down
8 changes: 3 additions & 5 deletions pxr/base/tf/anyWeakPtr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,11 @@ TfAnyWeakPtr::_EmptyHolder::_IsConst() const
return true;
}

#ifdef PXR_PYTHON_SUPPORT_ENABLED
boost::python::api::object
TfPyBoostObjWrapper
TfAnyWeakPtr::_EmptyHolder::GetPythonObject() const
{
return boost::python::api::object();
return {};
}
#endif // PXR_PYTHON_SUPPORT_ENABLED

const std::type_info &
TfAnyWeakPtr::_EmptyHolder::GetTypeInfo() const
Expand Down Expand Up @@ -164,7 +162,7 @@ boost::python::api::object
TfAnyWeakPtr::_GetPythonObject() const
{
TfPyLock pyLock;
return _Get()->GetPythonObject();
return _Get()->GetPythonObject().get();
}
#endif // PXR_PYTHON_SUPPORT_ENABLED

Expand Down
23 changes: 9 additions & 14 deletions pxr/base/tf/anyWeakPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@

#ifdef PXR_PYTHON_SUPPORT_ENABLED
#include "pxr/base/tf/pyUtils.h"
#include <boost/python/object.hpp>
#endif // PXR_PYTHON_SUPPORT_ENABLED

#include "pxr/base/tf/pyBoostObjWrapper.h"
#include <boost/operators.hpp>

#include <cstddef>
Expand Down Expand Up @@ -157,9 +157,7 @@ class TfAnyWeakPtr : boost::totally_ordered<TfAnyWeakPtr>
virtual TfWeakBase const *GetWeakBase() const = 0;
virtual operator bool() const = 0;
virtual bool _IsConst() const = 0;
#ifdef PXR_PYTHON_SUPPORT_ENABLED
virtual boost::python::api::object GetPythonObject() const = 0;
#endif // PXR_PYTHON_SUPPORT_ENABLED
virtual TfPyBoostObjWrapper GetPythonObject() const = 0;
virtual const std::type_info & GetTypeInfo() const = 0;
virtual TfType const& GetType() const = 0;
virtual const void* _GetMostDerivedPtr() const = 0;
Expand All @@ -174,9 +172,7 @@ class TfAnyWeakPtr : boost::totally_ordered<TfAnyWeakPtr>
TF_API virtual TfWeakBase const *GetWeakBase() const;
TF_API virtual operator bool() const;
TF_API virtual bool _IsConst() const;
#ifdef PXR_PYTHON_SUPPORT_ENABLED
TF_API virtual boost::python::api::object GetPythonObject() const;
#endif // PXR_PYTHON_SUPPORT_ENABLED
TF_API virtual TfPyBoostObjWrapper GetPythonObject() const;
TF_API virtual const std::type_info & GetTypeInfo() const;
TF_API virtual TfType const& GetType() const;
TF_API virtual const void* _GetMostDerivedPtr() const;
Expand All @@ -195,9 +191,7 @@ class TfAnyWeakPtr : boost::totally_ordered<TfAnyWeakPtr>
virtual TfWeakBase const *GetWeakBase() const;
virtual operator bool() const;
virtual bool _IsConst() const;
#ifdef PXR_PYTHON_SUPPORT_ENABLED
virtual boost::python::api::object GetPythonObject() const;
#endif // PXR_PYTHON_SUPPORT_ENABLED
virtual TfPyBoostObjWrapper GetPythonObject() const;
virtual const std::type_info & GetTypeInfo() const;
virtual TfType const& GetType() const;
virtual const void* _GetMostDerivedPtr() const;
Expand Down Expand Up @@ -263,15 +257,16 @@ TfAnyWeakPtr::_PointerHolder<Ptr>::operator bool() const
return bool(_ptr);
}

#ifdef PXR_PYTHON_SUPPORT_ENABLED
template <class Ptr>
boost::python::api::object
TfPyBoostObjWrapper
TfAnyWeakPtr::_PointerHolder<Ptr>::GetPythonObject() const
{
#ifdef PXR_PYTHON_SUPPORT_ENABLED
return TfPyObject(_ptr);
}
#else
return {};
#endif // PXR_PYTHON_SUPPORT_ENABLED

}
template <class Ptr>
const std::type_info &
TfAnyWeakPtr::_PointerHolder<Ptr>::GetTypeInfo() const
Expand Down
2 changes: 2 additions & 0 deletions pxr/base/tf/pyBoostObjWrapper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include "pxr/pxr.h"
#include "pxr/base/tf/pyBoostObjWrapper.h"
65 changes: 65 additions & 0 deletions pxr/base/tf/pyBoostObjWrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#ifndef PXR_BASE_TF_PY_BOOST_OBJ_WRAPPER_H
#define PXR_BASE_TF_PY_BOOST_OBJ_WRAPPER_H

#ifdef PXR_PYTHON_SUPPORT_ENABLED
#include "pxr/base/tf/pyUtils.h"
#include <boost/python/object.hpp>
#else
#include <type_traits>
#endif

#include <cstddef>

PXR_NAMESPACE_OPEN_SCOPE
/// \brief TfPyBoostObjWrapperStub
///
/// Empty stub for ABI compatibility even if Python support is
/// enabled so we can ensure the alignment is the same.
class TfPyBoostObjWrapperStub
{
public:
static constexpr std::size_t Size = 8;
static constexpr std::size_t Align = 8;

private:
std::aligned_storage<Size, Align>::type _stub;
};

/// \brief TfPyBoostObjWrapper
///
/// Wrapper of the boost::python::api::object
///
/// The wrapper provides a uniform type
/// usable when python extensions are accessible
/// and when no python extensions are compiled
#ifdef PXR_PYTHON_SUPPORT_ENABLED
class TfPyBoostObjWrapper
{
using WrappedType = boost::python::api::object;

public:
// Just a constructor that forwards everything to the wrapped object.
template<typename ...Args>
TfPyBoostObjWrapper(Args&&... args)
: _wrappedObject(std::forward<Args>(args)...)
{}

inline WrappedType& get() { return _wrappedObject; }

private:
WrappedType _wrappedObject;
};
// Verify binary compatibility between the expected size from the stub and the class
static_assert(sizeof(TfPyBoostObjWrapper) == sizeof(TfPyBoostObjWrapperStub),
"ABI break: Incompatible class sizes.");
static_assert(alignof(TfPyBoostObjWrapper) == alignof(TfPyBoostObjWrapperStub),
"ABI break: Incompatible class alignments.");
#else
class TfPyBoostObjWrapper : TfPyBoostObjWrapperStub
{
};
#endif

PXR_NAMESPACE_CLOSE_SCOPE

#endif // PXR_BASE_TF_PY_BOOST_OBJ_WRAPPER_H
3 changes: 3 additions & 0 deletions pxr/base/tf/pyObjWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include "pxr/base/tf/pyObjWrapper.h"

#ifdef PXR_PYTHON_SUPPORT_ENABLED
#include "pxr/base/tf/pyLock.h"
#include "pxr/base/tf/pyUtils.h"
#include "pxr/base/tf/type.h"
Expand Down Expand Up @@ -94,3 +95,5 @@ TfPyObjWrapper::operator!=(TfPyObjWrapper const &other) const
}

PXR_NAMESPACE_CLOSE_SCOPE

#endif // PXR_PYTHON_SUPPORT_ENABLED
34 changes: 34 additions & 0 deletions pxr/base/tf/pyObjWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,35 @@

#include "pxr/base/tf/api.h"

#ifdef PXR_PYTHON_SUPPORT_ENABLED
#include <boost/functional/hash.hpp>
#include <boost/python/object_fwd.hpp>
#include <boost/python/object_operators.hpp>

#include <iosfwd>
#include <memory>

#else

#include <type_traits>

#endif

PXR_NAMESPACE_OPEN_SCOPE

// We define the empty stub for ABI compatibility even if Python support is
// enabled so we can make sure size and alignment is the same.
class TfPyObjWrapperStub
{
public:
static constexpr std::size_t Size = 16;
static constexpr std::size_t Align = 8;

private:
std::aligned_storage<Size, Align>::type _stub;
};


/// \class TfPyObjWrapper
///
/// Boost Python object wrapper.
Expand All @@ -63,6 +83,7 @@ PXR_NAMESPACE_OPEN_SCOPE
/// provides, by virtue of deriving from boost::python::api::object_operators<T>.
/// However it is important to note that callers must ensure the GIL is held
/// before using these operators!
#ifdef PXR_PYTHON_SUPPORT_ENABLED
class TfPyObjWrapper
: public boost::python::api::object_operators<TfPyObjWrapper>
{
Expand Down Expand Up @@ -126,6 +147,19 @@ class TfPyObjWrapper
std::shared_ptr<object> _objectPtr;
};

static_assert(sizeof(TfPyObjWrapper) == sizeof(TfPyObjWrapperStub),
"ABI break: Incompatible class sizes.");
static_assert(alignof(TfPyObjWrapper) == alignof(TfPyObjWrapperStub),
"ABI break: Incompatible class alignments.");

#else // PXR_PYTHON_SUPPORT_ENABLED

class TfPyObjWrapper : TfPyObjWrapperStub
{
};

#endif // PXR_PYTHON_SUPPORT_ENABLED

PXR_NAMESPACE_CLOSE_SCOPE

#endif // PXR_BASE_TF_PY_OBJ_WRAPPER_H
2 changes: 2 additions & 0 deletions pxr/base/tf/pyTracing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include "pxr/base/tf/pyTracing.h"

#ifdef PXR_PYTHON_SUPPORT_ENABLED
#include "pxr/base/tf/pyInterpreter.h"
#include "pxr/base/tf/pyUtils.h"
#include "pxr/base/tf/staticData.h"
Expand Down Expand Up @@ -153,3 +154,4 @@ void Tf_PyTracingPythonInitialized()
}

PXR_NAMESPACE_CLOSE_SCOPE
#endif // PXR_PYTHON_SUPPORT_ENABLED
14 changes: 12 additions & 2 deletions pxr/base/tf/pyTracing.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,20 @@
#ifndef PXR_BASE_TF_PY_TRACING_H
#define PXR_BASE_TF_PY_TRACING_H

#include "pxr/base/tf/pySafePython.h"

#include "pxr/pxr.h"

#ifdef PXR_PYTHON_SUPPORT_ENABLED
#include "pxr/base/tf/pySafePython.h"
#endif // PXR_PYTHON_SUPPORT_ENABLED

#include "pxr/base/tf/api.h"

#include <functional>
#include <memory>

PXR_NAMESPACE_OPEN_SCOPE

#ifdef PXR_PYTHON_SUPPORT_ENABLED
/// \struct TfPyTraceInfo
/// Structure passed to python trace functions. See the Python C API
/// documentation reference for the meaning of \a what and \a arg.
Expand All @@ -59,6 +62,13 @@ void Tf_PyFabricateTraceEvent(TfPyTraceInfo const &info);

// For internal use only. Do not use.
void Tf_PyTracingPythonInitialized();
#else
/// \struct For storage alignment when PXR_PYTHON_SUPPORT_ENABLED
/// is not enabled.
struct TfPyTraceInfo;
typedef std::function<void (TfPyTraceInfo const &)> TfPyTraceFn;
typedef std::shared_ptr<TfPyTraceFn> TfPyTraceFnId;
#endif // PXR_PYTHON_SUPPORT_ENABLED

PXR_NAMESPACE_CLOSE_SCOPE

Expand Down
6 changes: 0 additions & 6 deletions pxr/base/trace/collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@
#include "pxr/base/tf/declarePtrs.h"
#include "pxr/base/tf/mallocTag.h"

#ifdef PXR_PYTHON_SUPPORT_ENABLED
#include "pxr/base/tf/pyTracing.h"
#endif // PXR_PYTHON_SUPPORT_ENABLED

#include "pxr/base/tf/singleton.h"
#include "pxr/base/tf/refBase.h"
Expand Down Expand Up @@ -591,13 +589,11 @@ class TraceCollector : public TfWeakBase {
//
TraceThreadId _threadIndex;

#ifdef PXR_PYTHON_SUPPORT_ENABLED
// When auto-tracing python frames, this stores the stack of scopes.
struct PyScope {
Key key;
};
std::vector<PyScope> _pyScopes;
#endif // PXR_PYTHON_SUPPORT_ENABLED
};

TRACE_API static std::atomic<int> _isEnabled;
Expand All @@ -607,10 +603,8 @@ class TraceCollector : public TfWeakBase {

std::string _label;

#ifdef PXR_PYTHON_SUPPORT_ENABLED
std::atomic<int> _isPythonTracingEnabled;
TfPyTraceFnId _pyTraceFnId;
#endif // PXR_PYTHON_SUPPORT_ENABLED
};

TRACE_API_TEMPLATE_CLASS(TfSingleton<TraceCollector>);
Expand Down
2 changes: 0 additions & 2 deletions pxr/base/vt/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,14 +493,12 @@ operator<<(std::ostream &out, const VtValue &self) {
return self.IsEmpty() ? out : self._info->StreamOut(self._storage, out);
}

#ifdef PXR_PYTHON_SUPPORT_ENABLED
TfPyObjWrapper
VtValue::_GetPythonObject() const
{
return _info.GetLiteral() ?
_info.Get()->GetPyObj(_storage) : TfPyObjWrapper();
}
#endif // PXR_PYTHON_SUPPORT_ENABLED

static void const *
_FindOrCreateDefaultValue(std::type_info const &type,
Expand Down
Loading

0 comments on commit 13e0046

Please sign in to comment.