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

Fix lost connection for output port with parent #2729

Merged
merged 7 commits into from
Dec 1, 2022
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
48 changes: 29 additions & 19 deletions lib/mayaUsd/ufe/UsdAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,28 +504,37 @@ bool UsdAttributes::canRenameAttribute(

static void setConnections(
const PXR_NS::UsdPrim& prim,
const SdfPath& OldPropertyPath,
const SdfPath& oldPropertyPath,
Copy link
Collaborator

Choose a reason for hiding this comment

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

The method does not set the connection it only updates existing connections:
updateConnections()

const SdfPath& newPropertyPath)
{
// Update the connections with the new attribute name.
for (const auto& node : prim.GetChildren()) {
for (auto& attribute : node.GetAttributes()) {
PXR_NS::UsdAttribute attr = attribute.As<PXR_NS::UsdAttribute>();
PXR_NS::SdfPathVector sources;
attr.GetConnections(&sources);
bool hasChanged = false;
// Check if the node attribute is connected to the original property path.
for (size_t i = 0; i < sources.size(); ++i) {
if (sources[i] == OldPropertyPath) {
sources[i] = newPropertyPath;
hasChanged = true;
}
}
// Update the connections with the new property path.
if (hasChanged) {
attr.SetConnections(sources);
for (const auto& attribute : prim.GetAttributes()) {
PXR_NS::UsdAttribute attr = attribute.As<PXR_NS::UsdAttribute>();
PXR_NS::SdfPathVector sources;
attr.GetConnections(&sources);

bool hasChanged = false;
// Check if the node attribute is connected to the original property path.
for (size_t i = 0; i < sources.size(); ++i) {
if (sources[i] == oldPropertyPath) {
sources[i] = newPropertyPath;
hasChanged = true;
}
}
// Update the connections with the new property path.
if (hasChanged) {
attr.SetConnections(sources);
}
}
neilh-adsk marked this conversation as resolved.
Show resolved Hide resolved
}

static void setConnectionsOfAllChildren(
const PXR_NS::UsdPrim& prim,
const SdfPath& oldPropertyPath,
const SdfPath& newPropertyPath)
{
// Update the connections with the new attribute name.
for (const auto& node : prim.GetChildren()) {
setConnections(node, oldPropertyPath, newPropertyPath);
}
}

Expand Down Expand Up @@ -592,9 +601,10 @@ Ufe::Attribute::Ptr UsdAttributes::doRenameAttribute(
// Given the unidirectional nature of connections, we discriminate whether the source is
// input or output
if (baseNameAndType.second == PXR_NS::UsdShadeAttributeType::Input) {
setConnections(prim, kOldPropertyPath, kNewPropertyPath);
setConnectionsOfAllChildren(prim, kOldPropertyPath, kNewPropertyPath);
}
if (baseNameAndType.second == PXR_NS::UsdShadeAttributeType::Output) {
setConnectionsOfAllChildren(prim.GetParent(), kOldPropertyPath, kNewPropertyPath);
setConnections(prim.GetParent(), kOldPropertyPath, kNewPropertyPath);
}
}
Expand Down
54 changes: 53 additions & 1 deletion test/lib/ufe/testAttributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,17 @@ def testRenamingAttribute(self):

self.assertNotIn("inputs:file2:varnameStr", standardSurfaceAttrs.attributeNames)
self.assertIn("inputs:file2:varname", standardSurfaceAttrs.attributeNames)


# Test undo.
cmds.undo()
self.assertIn("inputs:file2:varnameStr", standardSurfaceAttrs.attributeNames)
self.assertNotIn("inputs:file2:varname", standardSurfaceAttrs.attributeNames)

# Test redo.
cmds.redo()
self.assertNotIn("inputs:file2:varnameStr", standardSurfaceAttrs.attributeNames)
self.assertIn("inputs:file2:varname", standardSurfaceAttrs.attributeNames)

# Test the connections.

ufeItemTexture = ufeUtils.createUfeSceneItem(shapeNode,
Expand Down Expand Up @@ -300,6 +310,48 @@ def testRenamingAttribute(self):
self.assertEqual(ufe.PathString.string(dstAttr.path),
'|stage|stageShape,/pCube2/Looks/standardSurface2SG/standardSurface2')
self.assertEqual(dstAttr.name, 'inputs:base_color')

# Create the SceneItem for a compound with out connection to the parent.
ufeCompoundItem = ufeUtils.createUfeSceneItem(shapeNode,
'/pCube2/Looks/standardSurface2SG/NodeGraph1')
self.assertIsNotNone(ufeCompoundItem)

# Then create the attributes interface for that item.
compoundAttrs = ufe.Attributes.attributes(ufeCompoundItem)
self.assertIsNotNone(compoundAttrs)

# Rename the attribute.
cmd = compoundAttrs.renameAttributeCmd("outputs:out", "outputs:out1")
self.assertIsNotNone(cmd)

ufeCmd.execute(cmd)

self.assertNotIn("outputs:out", compoundAttrs.attributeNames)
self.assertIn("outputs:out1", compoundAttrs.attributeNames)

# Test the connection.

ufeParentItem = ufeUtils.createUfeSceneItem(shapeNode,
'/pCube2/Looks/standardSurface2SG')
self.assertIsNotNone(ufeParentItem)

connectionHandler = ufe.RunTimeMgr.instance().connectionHandler(ufeParentItem.runTimeId())
self.assertIsNotNone(connectionHandler)
connections = connectionHandler.sourceConnections(ufeParentItem)
self.assertIsNotNone(connectionHandler)
conns = connections.allConnections()
self.assertEqual(len(conns), 2)

srcAttr = conns[1].src
dstAttr = conns[1].dst

self.assertEqual(ufe.PathString.string(srcAttr.path),
'|stage|stageShape,/pCube2/Looks/standardSurface2SG/NodeGraph1')
self.assertEqual(srcAttr.name, 'outputs:out1')

self.assertEqual(ufe.PathString.string(dstAttr.path),
'|stage|stageShape,/pCube2/Looks/standardSurface2SG')
self.assertEqual(dstAttr.name, 'outputs:out')
Copy link
Collaborator

@hodoulp hodoulp Nov 24, 2022

Choose a reason for hiding this comment

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

Could you enhance the test with cmds.undo() ?


if __name__ == '__main__':
unittest.main(verbosity=2)
33 changes: 30 additions & 3 deletions test/testSamples/MaterialX/MayaSurfaces.usda
Original file line number Diff line number Diff line change
Expand Up @@ -111,25 +111,35 @@ def Mesh "pCube2" (

def Scope "Looks"
{
def Material "standardSurface2SG"
def Material "standardSurface2SG" (
prepend apiSchemas = ["NodeGraphNodeAPI"]
)
{
string inputs:file2:varnameStr = "st"
token outputs:mtlx:surface.connect = </pCube2/Looks/standardSurface2SG/standardSurface2.outputs:surface>
token outputs:out.connect = </pCube2/Looks/standardSurface2SG/NodeGraph1.outputs:out>
uniform float2 ui:nodegraph:node:pos = (0.055555556, 0.055555556)

def Shader "standardSurface2"
def Shader "standardSurface2" (
prepend apiSchemas = ["NodeGraphNodeAPI"]
)
{
uniform token info:id = "ND_standard_surface_surfaceshader"
float inputs:base = 1
color3f inputs:base_color.connect = </pCube2/Looks/standardSurface2SG/MayaNG_standardSurface2SG.outputs:baseColor>
float inputs:specular = 1
float inputs:specular_roughness = 0.4
token outputs:surface
uniform float2 ui:nodegraph:node:pos = (1.1166667, -0.33888888)
}

def NodeGraph "MayaNG_standardSurface2SG"
def NodeGraph "MayaNG_standardSurface2SG" (
prepend apiSchemas = ["NodeGraphNodeAPI"]
)
{
string inputs:file2:varnameStr.connect = </pCube2/Looks/standardSurface2SG.inputs:file2:varnameStr>
color3f outputs:baseColor.connect = </pCube2/Looks/standardSurface2SG/MayaNG_standardSurface2SG/MayaSwizzle_file2_rgb.outputs:out>
uniform float2 ui:nodegraph:node:pos = (-0.5777778, -0.33888888)

def Shader "file2"
{
Expand Down Expand Up @@ -157,6 +167,23 @@ def Mesh "pCube2" (
color3f outputs:out
}
}

def NodeGraph "NodeGraph1" (
prepend apiSchemas = ["NodeGraphNodeAPI"]
)
{
token outputs:out.connect = </pCube2/Looks/standardSurface2SG/NodeGraph1/surface1.outputs:out>
uniform float2 ui:nodegraph:node:pos = (1.0759555, -1.774011)

def Shader "surface1" (
prepend apiSchemas = ["NodeGraphNodeAPI"]
)
{
uniform token info:id = "ND_surface"
token outputs:out
uniform float2 ui:nodegraph:node:pos = (1.0496833, -1.7418168)
}
}
}
}
}
Expand Down