Skip to content

Commit

Permalink
Merge pull request #210 from Autodesk/ufe_bbox_support_dev
Browse files Browse the repository at this point in the history
Ufe bbox support dev
  • Loading branch information
Krystian Ligenza authored Jan 31, 2020
2 parents 528c041 + 6cf32fb commit 5cd2826
Show file tree
Hide file tree
Showing 19 changed files with 756 additions and 28 deletions.
5 changes: 5 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,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 @@ -360,13 +362,16 @@ if(UFE_FOUND)
ufe/UsdUndoDuplicateCommand.h
ufe/UsdUndoRenameCommand.h
ufe/Utils.h
ufe/UfeVersionCompat.h
)

if(CMAKE_UFE_V2_FEATURES_AVAILABLE)
list(APPEND mayaUsdUfe_headers
ufe/UsdAttribute.h
ufe/UsdAttributes.h
ufe/UsdAttributesHandler.h
ufe/UsdObject3d.h
ufe/UsdObject3dHandler.h
ufe/UsdUndoCreateGroupCommand.h
)
endif()
Expand Down
16 changes: 9 additions & 7 deletions lib/ufe/Global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
#ifdef UFE_V2_FEATURES_AVAILABLE
// Note: must come after include of ufe files so we have the define.
#include "UsdAttributesHandler.h"
#include "UsdObject3dHandler.h"
#else
#include "UfeVersionCompat.h"
#endif

#include <string>
Expand Down Expand Up @@ -93,14 +96,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));

#if !defined(NDEBUG)
assert(g_USDRtid != 0);
#endif
Expand Down
28 changes: 28 additions & 0 deletions lib/ufe/UfeVersionCompat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// Copyright 2020 Autodesk
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#pragma once

// Make sure ufe.h is included, if it hasn't been already.
#include <ufe/ufe.h>

// UFE v1 has no UFE_V2 macro.
#if !defined(UFE_V2_FEATURES_AVAILABLE) && !defined(UFE_V2)
#define UFE_V2(...)
#endif

#if !defined(UFE_V3_FEATURES_AVAILABLE) && !defined(UFE_V3)
#define UFE_V3(...)
#endif
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);
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
{
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
{
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;

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

0 comments on commit 5cd2826

Please sign in to comment.