Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ufe bbox support dev #210

Merged
merged 7 commits into from
Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ if(UFE_FOUND)
ufe/UsdAttribute.cpp
ufe/UsdAttributes.cpp
ufe/UsdAttributesHandler.cpp
ufe/UsdObject3d.cpp
ufe/UsdObject3dHandler.cpp
ufe/UsdUndoCreateGroupCommand.cpp
)
endif()
Expand Down Expand Up @@ -365,6 +367,8 @@ if(UFE_FOUND)
ufe/UsdAttribute.h
ufe/UsdAttributes.h
ufe/UsdAttributesHandler.h
ufe/UsdObject3d.h
ufe/UsdObject3dHandler.h
ufe/UsdUndoCreateGroupCommand.h
)
endif()
Expand Down
14 changes: 7 additions & 7 deletions lib/ufe/Global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#ifdef UFE_V2_FEATURES_AVAILABLE
// Note: must come after include of ufe files so we have the define.
#include "UsdAttributesHandler.h"
#include "UsdObject3dHandler.h"
#endif

#include <string>
Expand Down Expand Up @@ -93,14 +94,13 @@ MStatus initialize()
auto usdHierHandler = UsdHierarchyHandler::create();
auto usdTrans3dHandler = UsdTransform3dHandler::create();
auto usdSceneItemOpsHandler = UsdSceneItemOpsHandler::create();
#ifdef UFE_V2_FEATURES_AVAILABLE
auto usdAttributesHandler = UsdAttributesHandler::create();
g_USDRtid = Ufe::RunTimeMgr::instance().register_(
kUSDRunTimeName, usdHierHandler, usdTrans3dHandler, usdSceneItemOpsHandler, usdAttributesHandler, nullptr);
#else
UFE_V2(auto usdAttributesHandler = UsdAttributesHandler::create();)
UFE_V2(auto usdObject3dHandler = UsdObject3dHandler::create();)
g_USDRtid = Ufe::RunTimeMgr::instance().register_(
kUSDRunTimeName, usdHierHandler, usdTrans3dHandler, usdSceneItemOpsHandler);
#endif
kUSDRunTimeName, usdHierHandler, usdTrans3dHandler,
usdSceneItemOpsHandler
UFE_V2(, usdAttributesHandler, usdObject3dHandler));
seando-adsk marked this conversation as resolved.
Show resolved Hide resolved

#if !defined(NDEBUG)
assert(g_USDRtid != 0);
#endif
Expand Down
73 changes: 73 additions & 0 deletions lib/ufe/UsdObject3d.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// ===========================================================================
// Copyright 2019 Autodesk, Inc. All rights reserved.
//
// Use of this software is subject to the terms of the Autodesk license
// agreement provided at the time of installation or download, or which
// otherwise accompanies this software in either electronic or hard copy form.
// ===========================================================================

#include "UsdObject3d.h"
#include "Utils.h"

#include "ufe/types.h"

#include "pxr/usd/usdGeom/bboxCache.h"
#include "pxr/usd/usd/timeCode.h"

namespace {
Ufe::Vector3d toVector3d(const GfVec3d& v)
{
return Ufe::Vector3d(v[0], v[1], v[2]);
}

}

MAYAUSD_NS_DEF {
namespace ufe {

UsdObject3d::UsdObject3d(const UsdSceneItem::Ptr& item)
: Ufe::Object3d(), fItem(item), fPrim(item->prim()) {}

UsdObject3d::~UsdObject3d() {}

/*static*/
UsdObject3d::Ptr UsdObject3d::create(const UsdSceneItem::Ptr& item)
{
return std::make_shared<UsdObject3d>(item);
}

//------------------------------------------------------------------------------
// Ufe::Object3d overrides
//------------------------------------------------------------------------------

Ufe::SceneItem::Ptr UsdObject3d::sceneItem() const
{
return fItem;
}

Ufe::BBox3d UsdObject3d::boundingBox() const
{
// Use USD to compute the bounding box. This is strictly speaking
// incorrect, as a USD node may eventually have a Maya child, given the
// full generality of UFE paths. However, as of 24-Oct-2019, this does not
// exist. To support this use case,
// UsdGeomBoundable::ComputeExtentFromPlugins() allows a plugin to register
// an extent computation; this should be explored.
//
// UsdGeomImageable::ComputeLocalBound() just calls UsdGeomBBoxCache, so do
// this here as well.
//
// Would be nice to know if the object extents are animated or not, so
// we can bypass time computation and simply use UsdTimeCode::Default()
// as the time.
TfTokenVector purposes{UsdGeomTokens->default_};
UsdGeomBBoxCache bboxCache(getTime(sceneItem()->path()), purposes);
auto bbox = bboxCache.ComputeLocalBound(fPrim);
seando-adsk marked this conversation as resolved.
Show resolved Hide resolved
auto range = bbox.GetRange();
auto min = range.GetMin();
auto max = range.GetMax();
return Ufe::BBox3d(toVector3d(min), toVector3d(max));
}

} // namespace ufe
} // namespace MayaUsd
50 changes: 50 additions & 0 deletions lib/ufe/UsdObject3d.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// ===========================================================================
// Copyright 2019 Autodesk, Inc. All rights reserved.
//
// Use of this software is subject to the terms of the Autodesk license
// agreement provided at the time of installation or download, or which
// otherwise accompanies this software in either electronic or hard copy form.
// ===========================================================================
#pragma once

#include "../base/api.h"
#include "UsdSceneItem.h"

#include "ufe/object3d.h"

MAYAUSD_NS_DEF {
namespace ufe {

//! \brief USD run-time 3D object interface
/*!
This class implements the Object3d interface for USD prims.
*/
class MAYAUSD_CORE_PUBLIC UsdObject3d : public Ufe::Object3d
seando-adsk marked this conversation as resolved.
Show resolved Hide resolved
{
public:
using Ptr = std::shared_ptr<UsdObject3d>;

UsdObject3d(const UsdSceneItem::Ptr& item);
~UsdObject3d() override;

// Delete the copy/move constructors assignment operators.
UsdObject3d(const UsdObject3d&) = delete;
UsdObject3d& operator=(const UsdObject3d&) = delete;
UsdObject3d(UsdObject3d&&) = delete;
UsdObject3d& operator=(UsdObject3d&&) = delete;

//! Create a UsdObject3d.
static UsdObject3d::Ptr create(const UsdSceneItem::Ptr& item);

// Ufe::Object3d overrides
Ufe::SceneItem::Ptr sceneItem() const override;
Ufe::BBox3d boundingBox() const override;

private:
UsdSceneItem::Ptr fItem;
UsdPrim fPrim;

}; // UsdObject3d

} // namespace ufe
} // namespace MayaUsd
39 changes: 39 additions & 0 deletions lib/ufe/UsdObject3dHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// ===========================================================================
// Copyright 2019 Autodesk, Inc. All rights reserved.
//
// Use of this software is subject to the terms of the Autodesk license
// agreement provided at the time of installation or download, or which
// otherwise accompanies this software in either electronic or hard copy form.
// ===========================================================================

#include "UsdObject3dHandler.h"
#include "UsdSceneItem.h"

MAYAUSD_NS_DEF {
namespace ufe {

UsdObject3dHandler::UsdObject3dHandler() : Ufe::Object3dHandler() {}

UsdObject3dHandler::~UsdObject3dHandler() {}

/*static*/
UsdObject3dHandler::Ptr UsdObject3dHandler::create()
{
return std::make_shared<UsdObject3dHandler>();
}

//------------------------------------------------------------------------------
// UsdObject3dHandler overrides
//------------------------------------------------------------------------------

Ufe::Object3d::Ptr UsdObject3dHandler::object3d(const Ufe::SceneItem::Ptr& item) const
seando-adsk marked this conversation as resolved.
Show resolved Hide resolved
{
UsdSceneItem::Ptr usdItem = std::dynamic_pointer_cast<UsdSceneItem>(item);
#if !defined(NDEBUG)
assert(usdItem);
#endif
return UsdObject3d::create(usdItem);
}

} // namespace ufe
} // namespace MayaUsd
46 changes: 46 additions & 0 deletions lib/ufe/UsdObject3dHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// ===========================================================================
// Copyright 2019 Autodesk, Inc. All rights reserved.
//
// Use of this software is subject to the terms of the Autodesk license
// agreement provided at the time of installation or download, or which
// otherwise accompanies this software in either electronic or hard copy form.
// ===========================================================================
#pragma once

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

#include "UsdObject3d.h"

#include "ufe/object3dHandler.h"

MAYAUSD_NS_DEF {
namespace ufe {

//! \brief USD run-time 3D object handler.
/*!
Factory object for Object3d interfaces.
*/
class MAYAUSD_CORE_PUBLIC UsdObject3dHandler : public Ufe::Object3dHandler
{
public:
typedef std::shared_ptr<UsdObject3dHandler> Ptr;

UsdObject3dHandler();
~UsdObject3dHandler() override;

// Delete the copy/move constructors assignment operators.
UsdObject3dHandler(const UsdObject3dHandler&) = delete;
UsdObject3dHandler& operator=(const UsdObject3dHandler&) = delete;
UsdObject3dHandler(UsdObject3dHandler&&) = delete;
UsdObject3dHandler& operator=(UsdObject3dHandler&&) = delete;

//! Create a UsdObject3dHandler.
static UsdObject3dHandler::Ptr create();

// UsdObject3dHandler overrides
Ufe::Object3d::Ptr object3d(const Ufe::SceneItem::Ptr& item) const override;

}; // UsdObject3dHandler

} // namespace ufe
} // namespace MayaUsd
43 changes: 43 additions & 0 deletions lib/ufe/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
#include "private/Utils.h"
#include "UsdStageMap.h"
#include "ProxyShapeHandler.h"
#include "../nodes/proxyShapeBase.h"

#include <pxr/base/tf/hashset.h>
#include <pxr/usd/usd/stage.h>

#include <maya/MGlobal.h>
#include <maya/MSelectionList.h>
#include <maya/MObjectHandle.h>
#include <maya/MFnDependencyNode.h>

#include <cassert>
#include <string>
Expand Down Expand Up @@ -218,5 +221,45 @@ MDagPath nameToDagPath(const std::string& name)
return dag;
}

UsdTimeCode getTime(const Ufe::Path& path)
{
// Path should not be empty.
if (!TF_VERIFY(!path.empty())) {
return UsdTimeCode::Default();
}

// Get the time from the proxy shape. This will be the tail component of
// the first path segment.
auto proxyShapePath = Ufe::Path(path.getSegments()[0]);

// Keep a single-element path to MObject cache, as all USD prims in a stage
// share the same proxy shape.
static std::pair<Ufe::Path, MObjectHandle> cache;
seando-adsk marked this conversation as resolved.
Show resolved Hide resolved

MObject proxyShapeObj;

if (cache.first == proxyShapePath && cache.second.isValid()) {
proxyShapeObj = cache.second.object();
}
else {
// Not found in the cache, or no longer valid. Get the proxy shape
// MObject from its path, and put it in the cache. Pop the head of the
// UFE path to get rid of "|world", which is implicit in Maya.
auto proxyShapeDagPath = nameToDagPath(
proxyShapePath.popHead().string());
TF_VERIFY(proxyShapeDagPath.isValid());
proxyShapeObj = proxyShapeDagPath.node();
cache = std::pair<Ufe::Path, MObjectHandle>(
proxyShapePath, MObjectHandle(proxyShapeObj));
}

// Get time from the proxy shape.
MFnDependencyNode fn(proxyShapeObj);
auto proxyShape = dynamic_cast<MayaUsdProxyShapeBase*>(fn.userNode());
TF_VERIFY(proxyShape);

return proxyShape->getTime();
}

} // namespace ufe
} // namespace MayaUsd
6 changes: 6 additions & 0 deletions lib/ufe/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <ufe/pathSegment.h>

#include <pxr/usd/usd/prim.h>
#include <pxr/usd/usd/timeCode.h>
#include <pxr/usd/sdf/layer.h>
#include <pxr/base/tf/token.h>

Expand Down Expand Up @@ -81,5 +82,10 @@ Ufe::PathSegment dagPathToPathSegment(const MDagPath& dagPath);
MAYAUSD_CORE_PUBLIC
MDagPath nameToDagPath(const std::string& name);

//! Get the time along the argument path. A gateway node (i.e. proxy shape)
//! along the path can transform Maya's time (e.g. with scale and offset).
MAYAUSD_CORE_PUBLIC
UsdTimeCode getTime(const Ufe::Path& path);

} // namespace ufe
} // namespace MayaUsd
1 change: 1 addition & 0 deletions test/lib/ufe/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ if(CMAKE_UFE_V2_FEATURES_AVAILABLE)
#
# testDuplicateCmd.py
# testMoveCmd.py
testObject3d.py
# testRotateCmd.py
# testScaleCmd.py
# testTransform3dTranslate.py
Expand Down
Loading