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

MAYA-109455 UFE USD camera commands #2789

Merged
merged 3 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions lib/mayaUsd/ufe/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ if(CMAKE_UFE_V2_FEATURES_AVAILABLE)
UsdTransform3dUndoableCommands.cpp
UsdUIInfoHandler.cpp
UsdUIUfeObserver.cpp
UsdUndoableCommand.cpp
UsdUndoAddNewPrimCommand.cpp
UsdUndoCreateGroupCommand.cpp
UsdUndoInsertChildCommand.cpp
Expand Down
35 changes: 7 additions & 28 deletions lib/mayaUsd/ufe/UsdAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
#include "private/Utils.h"

#include <mayaUsd/ufe/StagesSubject.h>
#include <mayaUsd/ufe/UsdUndoableCommand.h>
#include <mayaUsd/ufe/Utils.h>
#include <mayaUsd/undo/UsdUndoBlock.h>
#include <mayaUsd/undo/UsdUndoableItem.h>

#include <pxr/base/tf/token.h>
#include <pxr/base/vt/value.h>
Expand Down Expand Up @@ -253,30 +252,8 @@ void setUsdAttributeMatrixFromUfe(
}
#endif

class UsdBaseUndoableCommand : public Ufe::UndoableCommand
{
public:
void execute() override
{
MayaUsd::UsdUndoBlock undoBlock(&_undoableItem);
executeUndoBlock();
}

void undo() override { _undoableItem.undo(); }
void redo() override { _undoableItem.redo(); }

protected:
// Actual implementation of the execution of the command,
// executed "within" a UsdUndoBlock to capture undo data,
// to be implemented by the sub-class.
virtual void executeUndoBlock() = 0;

private:
MayaUsd::UsdUndoableItem _undoableItem;
};

template <typename T, typename A = MayaUsd::ufe::TypedUsdAttribute<T>>
class SetUndoableCommand : public UsdBaseUndoableCommand
class SetUndoableCommand : public MayaUsd::ufe::UsdUndoableCommand<Ufe::UndoableCommand>
{
public:
SetUndoableCommand(const typename A::Ptr& attr, const T& newValue)
Expand All @@ -285,15 +262,16 @@ class SetUndoableCommand : public UsdBaseUndoableCommand
{
}

void executeUndoBlock() override { _attr->set(_newValue); }
protected:
void executeImplementation() override { _attr->set(_newValue); }

private:
const typename A::Ptr _attr;
const T _newValue;
};

#ifdef UFE_V3_FEATURES_AVAILABLE
class SetUndoableMetadataCommand : public UsdBaseUndoableCommand
class SetUndoableMetadataCommand : public MayaUsd::ufe::UsdUndoableCommand<Ufe::UndoableCommand>
{
public:
SetUndoableMetadataCommand(
Expand All @@ -306,7 +284,8 @@ class SetUndoableMetadataCommand : public UsdBaseUndoableCommand
{
}

void executeUndoBlock() override
protected:
void executeImplementation() override
{
#ifdef UFE_V4_FEATURES_AVAILABLE
_attr._setMetadata(_key, _newValue);
Expand Down
196 changes: 181 additions & 15 deletions lib/mayaUsd/ufe/UsdCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "pxr/usd/usdGeom/camera.h"
#include "pxr/usd/usdGeom/metrics.h"

#include <mayaUsd/ufe/UsdUndoableCommand.h>
#include <mayaUsd/ufe/Utils.h>
#include <mayaUsd/utils/util.h>

Expand Down Expand Up @@ -68,14 +69,49 @@ bool UsdCamera::isCameraToken(const PXR_NS::TfToken& token)
// Ufe::Camera overrides
//------------------------------------------------------------------------------

namespace {

float convertToStageUnits(float value, double valueUnits, const PXR_NS::UsdPrim& prim)
{
// Figure out the stage unit
UsdStageWeakPtr stage = prim.GetStage();

double stageUnits = UsdGeomLinearUnits::centimeters;
if (UsdGeomStageHasAuthoredMetersPerUnit(stage)) {
stageUnits = UsdGeomGetStageMetersPerUnit(stage);
}

return UsdMayaUtil::ConvertUnit(value, valueUnits, stageUnits);
}

float convertToTenthOfStageUnits(float value, double valueUnits, const PXR_NS::UsdPrim& prim)
{
// Tenth of units means the values are ten times greater.
// For example, if the stage units is cm, then 10th of stage units is mm.
// So 1cm becomes 10mm, multiplying the value by 10.
return 10.0f * convertToStageUnits(value, valueUnits, prim);
}

} // namespace

const Ufe::Path& UsdCamera::path() const { return fItem->path(); }

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

Ufe::HorizontalApertureUndoableCommand::Ptr
UsdCamera::horizontalApertureCmd(float horizontalAperture)
Ufe::HorizontalApertureUndoableCommand::Ptr UsdCamera::horizontalApertureCmd(float value)
{
return nullptr;
auto command = [this, value]() -> bool {
// The unit of this horizontal aperture function are inches.
// The USD schema specifies horizontal aperture in tenths of a stage unit.
const float convertedValue
= convertToTenthOfStageUnits(value, UsdGeomLinearUnits::inches, prim());
PXR_NS::UsdGeomCamera usdGeomCamera(prim());
PXR_NS::UsdAttribute attr = usdGeomCamera.CreateHorizontalApertureAttr();
return attr.Set<float>(convertedValue);
};

return std::make_shared<UsdFunctionUndoableSetCommand<Ufe::HorizontalApertureUndoableCommand>>(
command, sceneItem()->path());
}

float UsdCamera::horizontalAperture() const
Expand All @@ -100,9 +136,20 @@ float UsdCamera::horizontalAperture() const
return UsdMayaUtil::ConvertUnit(horizontalAperture, stageUnits, UsdGeomLinearUnits::inches);
}

Ufe::VerticalApertureUndoableCommand::Ptr UsdCamera::verticalApertureCmd(float verticalAperture)
Ufe::VerticalApertureUndoableCommand::Ptr UsdCamera::verticalApertureCmd(float value)
{
return nullptr;
auto command = [this, value]() -> bool {
// The unit of this vertical aperture function are inches.
// The USD schema specifies vertical aperture in tenths of a stage unit.
const float convertedValue
= convertToTenthOfStageUnits(value, UsdGeomLinearUnits::inches, prim());
PXR_NS::UsdGeomCamera usdGeomCamera(prim());
PXR_NS::UsdAttribute attr = usdGeomCamera.CreateVerticalApertureAttr();
return attr.Set<float>(convertedValue);
};

return std::make_shared<UsdFunctionUndoableSetCommand<Ufe::VerticalApertureUndoableCommand>>(
command, sceneItem()->path());
}

float UsdCamera::verticalAperture() const
Expand All @@ -127,9 +174,22 @@ float UsdCamera::verticalAperture() const
return UsdMayaUtil::ConvertUnit(verticalAperture, stageUnits, UsdGeomLinearUnits::inches);
}

Ufe::HorizontalApertureOffsetUndoableCommand::Ptr UsdCamera::horizontalApertureOffsetCmd(float)
Ufe::HorizontalApertureOffsetUndoableCommand::Ptr
UsdCamera::horizontalApertureOffsetCmd(float value)
{
return nullptr;
auto command = [this, value]() -> bool {
// The unit of this horizontal aperture offset function are inches.
// The USD schema specifies horizontal aperture offset in tenths of a stage unit.
const float convertedValue
= convertToTenthOfStageUnits(value, UsdGeomLinearUnits::inches, prim());
PXR_NS::UsdGeomCamera usdGeomCamera(prim());
PXR_NS::UsdAttribute attr = usdGeomCamera.CreateHorizontalApertureOffsetAttr();
return attr.Set<float>(convertedValue);
};

return std::make_shared<
UsdFunctionUndoableSetCommand<Ufe::HorizontalApertureOffsetUndoableCommand>>(
command, sceneItem()->path());
}

float UsdCamera::horizontalApertureOffset() const
Expand All @@ -155,9 +215,21 @@ float UsdCamera::horizontalApertureOffset() const
horizontalApertureOffset, stageUnits, UsdGeomLinearUnits::inches);
}

Ufe::VerticalApertureOffsetUndoableCommand::Ptr UsdCamera::verticalApertureOffsetCmd(float)
Ufe::VerticalApertureOffsetUndoableCommand::Ptr UsdCamera::verticalApertureOffsetCmd(float value)
{
return nullptr;
auto command = [this, value]() -> bool {
// The unit of this vertical aperture offset function are inches.
// The USD schema specifies vertical aperture offset in tenths of a stage unit.
const float convertedValue
= convertToTenthOfStageUnits(value, UsdGeomLinearUnits::inches, prim());
PXR_NS::UsdGeomCamera usdGeomCamera(prim());
PXR_NS::UsdAttribute attr = usdGeomCamera.CreateVerticalApertureOffsetAttr();
return attr.Set<float>(convertedValue);
};

return std::make_shared<
UsdFunctionUndoableSetCommand<Ufe::VerticalApertureOffsetUndoableCommand>>(
command, sceneItem()->path());
}

float UsdCamera::verticalApertureOffset() const
Expand All @@ -182,14 +254,34 @@ float UsdCamera::verticalApertureOffset() const
return UsdMayaUtil::ConvertUnit(verticalApertureOffset, stageUnits, UsdGeomLinearUnits::inches);
}

Ufe::FStopUndoableCommand::Ptr UsdCamera::fStopCmd(float) { return nullptr; }
Ufe::FStopUndoableCommand::Ptr UsdCamera::fStopCmd(float value)
{
auto command = [this, value]() -> bool {
// The unit of this fStop function are mm.
// The USD schema specifies fStop in stage unit.
//
// TODO: Actually, the UsdGeomCamera docs fails to mention units...
// Moreover, it makes no sense for fstops to have units?
const float convertedValue
= convertToStageUnits(value, UsdGeomLinearUnits::millimeters, prim());
PXR_NS::UsdGeomCamera usdGeomCamera(prim());
PXR_NS::UsdAttribute attr = usdGeomCamera.CreateFStopAttr();
return attr.Set<float>(convertedValue);
};

return std::make_shared<UsdFunctionUndoableSetCommand<Ufe::FStopUndoableCommand>>(
command, sceneItem()->path());
}

float UsdCamera::fStop() const
{
// inspired by UsdImagingCameraAdapter::UpdateForTime
UsdGeomCamera usdGeomCamera(prim());
GfCamera gfCamera = usdGeomCamera.GetCamera(getTime(sceneItem()->path()));
// The USD schema specifies fStop in stage units. Store the fStop in stage units.
//
// TODO: Actually, the UsdGeomCamera docs fails to mention units...
// Moreover, it makes no sense for fstops to have units?
float fStop = gfCamera.GetFStop();

// Convert the fStop value to mm, the return unit of this function.
Expand All @@ -210,7 +302,21 @@ float UsdCamera::fStop() const
#endif
}

Ufe::FocalLengthUndoableCommand::Ptr UsdCamera::focalLengthCmd(float) { return nullptr; }
Ufe::FocalLengthUndoableCommand::Ptr UsdCamera::focalLengthCmd(float value)
{
auto command = [this, value]() -> bool {
// The unit of this focal length function are mm.
// The USD schema specifies focal length in tenths of a stage unit.
const float convertedValue
= convertToTenthOfStageUnits(value, UsdGeomLinearUnits::millimeters, prim());
PXR_NS::UsdGeomCamera usdGeomCamera(prim());
PXR_NS::UsdAttribute attr = usdGeomCamera.CreateFocalLengthAttr();
return attr.Set<float>(convertedValue);
};

return std::make_shared<UsdFunctionUndoableSetCommand<Ufe::FocalLengthUndoableCommand>>(
command, sceneItem()->path());
}

float UsdCamera::focalLength() const
{
Expand All @@ -234,7 +340,21 @@ float UsdCamera::focalLength() const
return UsdMayaUtil::ConvertUnit(focalLength, stageUnits, UsdGeomLinearUnits::millimeters);
}

Ufe::FocusDistanceUndoableCommand::Ptr UsdCamera::focusDistanceCmd(float) { return nullptr; }
Ufe::FocusDistanceUndoableCommand::Ptr UsdCamera::focusDistanceCmd(float value)
{
auto command = [this, value]() -> bool {
// The unit of this focus distance function are cm.
// The USD schema specifies focus distance in stage units.
const float convertedValue
= convertToStageUnits(value, UsdGeomLinearUnits::centimeters, prim());
PXR_NS::UsdGeomCamera usdGeomCamera(prim());
PXR_NS::UsdAttribute attr = usdGeomCamera.CreateFocusDistanceAttr();
return attr.Set<float>(convertedValue);
};

return std::make_shared<UsdFunctionUndoableSetCommand<Ufe::FocusDistanceUndoableCommand>>(
command, sceneItem()->path());
}

float UsdCamera::focusDistance() const
{
Expand All @@ -258,7 +378,25 @@ float UsdCamera::focusDistance() const
return UsdMayaUtil::ConvertUnit(focusDistance, stageUnits, UsdGeomLinearUnits::centimeters);
}

Ufe::NearClipPlaneUndoableCommand::Ptr UsdCamera::nearClipPlaneCmd(float) { return nullptr; }
Ufe::NearClipPlaneUndoableCommand::Ptr UsdCamera::nearClipPlaneCmd(float value)
{
auto command = [this, value]() -> bool {
// The unit of this near clip plane function are unspecified.
// The USD schema specifies near clip plane in stage units.
// Since UFE does not specify units, we do no conversion.
PXR_NS::UsdGeomCamera usdGeomCamera(prim());
PXR_NS::UsdAttribute attr = usdGeomCamera.CreateClippingRangeAttr();

GfVec2f range;
attr.Get<GfVec2f>(&range);
range[0] = value;

return attr.Set<GfVec2f>(range);
};

return std::make_shared<UsdFunctionUndoableSetCommand<Ufe::NearClipPlaneUndoableCommand>>(
command, sceneItem()->path());
}

float UsdCamera::nearClipPlane() const
{
Expand All @@ -276,7 +414,25 @@ float UsdCamera::nearClipPlane() const
return nearClipPlane;
}

Ufe::FarClipPlaneUndoableCommand::Ptr UsdCamera::farClipPlaneCmd(float) { return nullptr; }
Ufe::FarClipPlaneUndoableCommand::Ptr UsdCamera::farClipPlaneCmd(float value)
{
auto command = [this, value]() -> bool {
// The unit of this near clip plane function are unspecified.
// The USD schema specifies near clip plane in stage units.
// Since UFE does not specify units, we do no conversion.
PXR_NS::UsdGeomCamera usdGeomCamera(prim());
PXR_NS::UsdAttribute attr = usdGeomCamera.CreateClippingRangeAttr();

GfVec2f range;
attr.Get<GfVec2f>(&range);
range[1] = value;

return attr.Set<GfVec2f>(range);
};

return std::make_shared<UsdFunctionUndoableSetCommand<Ufe::FarClipPlaneUndoableCommand>>(
command, sceneItem()->path());
}

float UsdCamera::farClipPlane() const
{
Expand All @@ -296,7 +452,17 @@ float UsdCamera::farClipPlane() const

Ufe::ProjectionUndoableCommand::Ptr UsdCamera::projectionCmd(Ufe::Camera::Projection projection)
{
return nullptr;
auto command = [this, projection]() -> bool {
const TfToken value = projection == Ufe::Camera::Orthographic
? PXR_NS::UsdGeomTokens->orthographic
: PXR_NS::UsdGeomTokens->perspective;
PXR_NS::UsdGeomCamera usdGeomCamera(prim());
PXR_NS::UsdAttribute attr = usdGeomCamera.CreateProjectionAttr();
return attr.Set<TfToken>(value);
};

return std::make_shared<UsdFunctionUndoableSetCommand<Ufe::ProjectionUndoableCommand>>(
command, sceneItem()->path());
}

Ufe::Camera::Projection UsdCamera::projection() const
Expand Down
Loading