Add support for onAttributeNameChanged callbacks for nested Attributes#3115
Open
cbentejac wants to merge 5 commits into
Open
Add support for onAttributeNameChanged callbacks for nested Attributes#3115cbentejac wants to merge 5 commits into
onAttributeNameChanged callbacks for nested Attributes#3115cbentejac wants to merge 5 commits into
Conversation
In the case where we would be attempting to get the root name of an attribute within a `ListAttribute` while the element for that attribute in the list does not exist yet (or, more likely, is being created), the access through the index would fail.
…n lists If an `Attribute` is contained within a `ListAttribute`, either directly or as a parent at any level, `isInsideList` will be set to `True`. An `Attribute`'s hierarchy cannot ever change, so this property only needs to be evaluated (by going through the list of parent up to the initial root attribute) once, upon the `Attribute`'s creation.
…ributes' callbacks
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #3115 +/- ##
===========================================
+ Coverage 85.39% 85.50% +0.10%
===========================================
Files 73 73
Lines 11403 11498 +95
===========================================
+ Hits 9738 9831 +93
- Misses 1665 1667 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Contributor
Alxiice
reviewed
Jun 4, 2026
Comment on lines
1570
to
1607
| def _onAttributeChanged(self, attr: Attribute): | ||
| """ | ||
| When an attribute value has changed, a specific function can be defined in the descriptor | ||
| and be called. | ||
|
|
||
| Args: | ||
| attr: The Attribute that has changed. | ||
| """ | ||
|
|
||
| if self.isCompatibilityNode: | ||
| # Compatibility nodes are not meant to be updated. | ||
| return | ||
|
|
||
| if attr.isOutput and not self.isInputNode: | ||
| # Ignore changes on output attributes for non-input nodes | ||
| # as they are updated during the node's computation. | ||
| # And we do not want notifications during the graph processing. | ||
| return | ||
|
|
||
| if not attr.keyable and attr.value is None: | ||
| # Discard dynamic values depending on the graph processing. | ||
| return | ||
|
|
||
| if self.graph and self.graph.isLoading: | ||
| # Do not trigger attribute callbacks during the graph loading. | ||
| return | ||
|
|
||
| callback = self._getAttributeChangedCallback(attr) | ||
|
|
||
| if callback: | ||
| callback(self) | ||
|
|
||
| self.hasInvalidAttributeChanged.emit() | ||
|
|
||
| if self.graph: | ||
| # If we are in a graph, propagate the notification to the connected output attributes | ||
| for edge in self.graph.outEdges(attr): | ||
| edge.dst.valueChanged.emit() |
Contributor
There was a problem hiding this comment.
It's just an idea but in this implementation if an element of the list change we don't even call the list update callback.
It's a draft but here's a snippet that make it partially work :
def _onAttributeChanged(self, attr: Attribute):
"""
When an attribute value has changed, a specific function can be defined in the descriptor
and be called.
Args:
attr: The Attribute that has changed.
"""
if self.isCompatibilityNode:
# Compatibility nodes are not meant to be updated.
return
if attr.isOutput and not self.isInputNode:
# Ignore changes on output attributes for non-input nodes
# as they are updated during the node's computation.
# And we do not want notifications during the graph processing.
return
if not attr.keyable and attr.value is None:
# Discard dynamic values depending on the graph processing.
return
if self.graph and self.graph.isLoading:
# Do not trigger attribute callbacks during the graph loading.
return
callback = self._getAttributeChangedCallback(attr)
if not callback and attr.root is not None and attr.isInsideList:
def getLastCallable(attr):
if not attr.root or not attr.isInsideList:
return attr
return getLastCallable(attr._root())
attr = getLastCallable(attr) # Important to replace attr for the call on edges
callback = self._getAttributeChangedCallback(attr)
if callback:
callback(self)
self.hasInvalidAttributeChanged.emit()
if self.graph:
# If we are in a graph, propagate the notification to the connected output attributes
for edge in self.graph.outEdges(attr):
edge.dst.valueChanged.emit()Once again my test :
class TestNodeB(desc.Node):
category = 'TestPlugin'
inputs = [
desc.GroupAttribute(
name="parentGrp",
exposed=True,
items=[
desc.ListAttribute(
name="listAttr",
exposed=True,
elementDesc=desc.IntParam(
name="listItem",
value=0,
)
)
]
)
]
def display_func_infos(self, fname, node):
print(f"[Func {fname}] (node {node.name})")
def onParentGrpChanged(self, node):
self.display_func_infos(inspect.stack()[0][3], node)
def onParentGrpListAttrChanged(self, node):
self.display_func_infos(inspect.stack()[0][3], node)
def onParentGrpListAttrListItemChanged(self, node):
self.display_func_infos(inspect.stack()[0][3], node)
def process(self, node):
self.display_func_inf
And the output :
[Func onParentGrpListAttrChanged] (node TestC_1) # Update the closest attr that we can update
[Func onParentGrpListAttrChanged] (node TestD_1) # Call update on child param
[Func onParentGrpChanged] (node TestD_1) # Update parent on child param
# MISSING : Call onParentGrpChanged on node TestC_1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Description
This PR adds the support of the
on{AttributeName}Changedcallbacks for attributes that are nested inGroupAttributes. Prior to it, callbacks could not be used for nested attributes; either the callback was defined for the highest parentGroupAttributeorListAttribute(and was thus triggered for every single change occurring in thatGrouporList), or no change could be tracked for that attribute.Elements within
ListAttributesstill cannot use these callbacks; the callback can be setup for the list itself, meaning it will be triggered for any insertion/deletion in that list, but not for element value changes.To determine whether an attribute with a parent can be bound to a callback, we introduce a new
isInsideListproperty, that is evaluated once upon the attribute's creation, and determines whether there is aListAttributein the hierarchy of the current attribute.Tests are added for both the binding of callbacks and the
isInsideListproperty.Features list
ValueErrorexception in_getRootNamewhen it is called for a list element while the element is being added to the list.isInsideListproperty to determine whether an attribute is, directly or not, part of aListAttribute.isInsideListalways evaluates correctly.on{AttributeName}Changedcallbacks for attributes that are nested withinGroupAttributes.on{AttributeName}Changedcallbacks are bound correctly depending on their attribute's status.