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

HYDRA-456 : Add test for USD stage layer muting #17

Merged
merged 2 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 test/lib/mayaUsd/render/mayaToHydra/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ set(TEST_SCRIPT_FILES
cpp/testSelectionSceneIndex.py
cpp/testWireframeSelectionHighlightSceneIndex.py
cpp/testFlowViewportAPIViewportInformation.py
cpp/testUsdStageLayerMuting.py
)

# Test use of mesh adapter code for mesh support, using environment variable.
Expand Down
1 change: 1 addition & 0 deletions test/lib/mayaUsd/render/mayaToHydra/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ target_sources(${TARGET_NAME}
testWireframeSelectionHighlightSceneIndex.cpp
testFvpViewportInformationMultipleViewports.cpp
testFvpViewportInformationRendererSwitching.cpp
testUsdStageLayerMuting.cpp
)

# -----------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2023 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.
//

#include "testUtils.h"

#include <pxr/base/gf/vec3d.h>

#include <gtest/gtest.h>

PXR_NAMESPACE_USING_DIRECTIVE

namespace {

constexpr float kUnmutedExtent = 4.0f;

FindPrimPredicate findCubePredicate = PrimNamePredicate("USDCube");

HdDataSourceLocator extentMinLocator = HdDataSourceLocator(TfToken("extent"), TfToken("min"));
HdDataSourceLocator extentMaxLocator = HdDataSourceLocator(TfToken("extent"), TfToken("max"));

} // namespace

TEST(UsdStageLayerMuting, testSubLayerUnmuted)
{
// Get the terminal scene index
const auto& sceneIndices = GetTerminalSceneIndices();
ASSERT_GT(sceneIndices.size(), 0u);
SceneIndexInspector inspector(sceneIndices.front());

// Find the cube prim
PrimEntriesVector cubePrims = inspector.FindPrims(findCubePredicate);
ASSERT_EQ(cubePrims.size(), 1u);
HdSceneIndexPrim cubePrim = cubePrims.front().prim;

// Retrieve the extent/min and extent/max data sources
auto extentMinDataSource = HdTypedSampledDataSource<GfVec3d>::Cast(
HdContainerDataSource::Get(cubePrim.dataSource, extentMinLocator));
ASSERT_TRUE(extentMinDataSource);
auto extentMaxDataSource = HdTypedSampledDataSource<GfVec3d>::Cast(
HdContainerDataSource::Get(cubePrim.dataSource, extentMaxLocator));
ASSERT_TRUE(extentMaxDataSource);

// Ensure the extents have the correct values
EXPECT_TRUE(GfIsClose(
extentMinDataSource->GetTypedValue(0), -GfVec3d(kUnmutedExtent), DEFAULT_TOLERANCE));
EXPECT_TRUE(GfIsClose(
extentMaxDataSource->GetTypedValue(0), GfVec3d(kUnmutedExtent), DEFAULT_TOLERANCE));
}

TEST(UsdStageLayerMuting, testSubLayerMuted)
{
// Get the terminal scene index
const auto& sceneIndices = GetTerminalSceneIndices();
ASSERT_GT(sceneIndices.size(), 0u);
SceneIndexInspector inspector(sceneIndices.front());

// Find the cube prim
PrimEntriesVector cubePrims = inspector.FindPrims(findCubePredicate);
ASSERT_EQ(cubePrims.size(), 1u);
HdSceneIndexPrim cubePrim = cubePrims.front().prim;

// Ensure there is no "extent" data source with the sublayer muted,
// since the root layer did not define any special extents.
auto extentDataSource = cubePrim.dataSource->Get(TfToken("extent"));
EXPECT_FALSE(extentDataSource);
}
73 changes: 73 additions & 0 deletions test/lib/mayaUsd/render/mayaToHydra/cpp/testUsdStageLayerMuting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Copyright 2023 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.
#
import maya.cmds as cmds

import fixturesUtils
import mtohUtils
import unittest

from testUtils import PluginLoaded

class TestUsdStageLayerMuting(mtohUtils.MayaHydraBaseTestCase):
# MayaHydraBaseTestCase.setUpClass requirement.
_file = __file__

CUBE_PRIM_PATH = "/USDCube"
SUB_LAYER_CUBE_SIZE = 8.0

def setSubLayerMuted(self, isMuted: bool):
import maya.cmds as cmds
cmds.mayaUsdLayerEditor(self.subLayer.identifier, edit=True, muteLayer=(isMuted, self.usdProxyShapeUfePathString))

def setupUsdStage(self):
import maya.cmds as cmds
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit-pick: importing maya.cmds twice, may as well factor it out.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that import was even already factored out at the top so those were useless.

import mayaUsd
import mayaUsd_createStageWithNewLayer
from pxr import UsdGeom, Sdf

self.setHdStormRenderer()
cmds.refresh()

# Create a USD stage
self.usdProxyShapeUfePathString = mayaUsd_createStageWithNewLayer.createStageWithNewLayer()
stage = mayaUsd.lib.GetPrim(self.usdProxyShapeUfePathString).GetStage()

# Define a cube prim in the stage (implicit surface)
UsdGeom.Cube.Define(stage, self.CUBE_PRIM_PATH)

# Add a sublayer to the stage
rootLayer = stage.GetRootLayer()
subLayerId = cmds.mayaUsdLayerEditor(rootLayer.identifier, edit=True, addAnonymous="testSubLayer")[0]
self.subLayer = Sdf.Layer.Find(subLayerId)

# Author an opinion on the sublayer
stage.SetEditTarget(self.subLayer)
cubePrim = stage.GetPrimAtPath(self.CUBE_PRIM_PATH)
cubePrim.GetAttribute('size').Set(self.SUB_LAYER_CUBE_SIZE)

cmds.refresh()

@unittest.skipUnless(mtohUtils.checkForMayaUsdPlugin(), "Requires Maya USD Plugin.")
def test_UsdStageLayerMuting(self):
self.setupUsdStage()
with PluginLoaded('mayaHydraCppTests'):
cmds.mayaHydraCppTest(f="UsdStageLayerMuting.testSubLayerUnmuted")
self.setSubLayerMuted(True)
cmds.mayaHydraCppTest(f="UsdStageLayerMuting.testSubLayerMuted")
self.setSubLayerMuted(False)
cmds.mayaHydraCppTest(f="UsdStageLayerMuting.testSubLayerUnmuted")

if __name__ == '__main__':
fixturesUtils.runTests(globals())
21 changes: 21 additions & 0 deletions test/lib/mayaUsd/render/mayaToHydra/cpp/testUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,27 @@ class SceneIndexInspector
HdSceneIndexBasePtr _sceneIndex;
};

class PrimNamePredicate
{
public:
PrimNamePredicate(const std::string& primName) : _primName(primName) {}

/**
* @brief Predicate to match a prim name.
*
* @param[in] sceneIndex The scene index in which the prim path to test is contained.
* @param[in] primPath The prim path to test.
*
* @return True if the argument prim path's name matches the predicate's prim name, false otherwise.
*/
bool operator()(const HdSceneIndexBasePtr& sceneIndex, const SdfPath& primPath) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sceneIndex argument is unused?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, as this class is to be used as a FindPrimPredicate, whose signature includes a SceneIndex. This was indeed not explained, so I've renamed the unused scene index variable to _, mentioned that it is unused and clarified why that is.

return primPath.GetName() == _primName;
}

private:
const std::string _primName;
};

class SceneIndexDisplayNamePred {
const std::string _name;
public:
Expand Down