Callback not append

73 views
Skip to first unread message

Rémi Deletrain

unread,
Aug 4, 2020, 6:11:34 AM8/4/20
to Python Programming for Autodesk Maya
Hello everyone,

It's been a long time since I came here to ask questions ... :)
I have a problem again that I haven't been able to solve yet.

I have attributes of type string which are paths. I must have a callback on it to call a resolver if path change.
As long as my attribute is not connected the callback works. However, if my string attribute is connected, the value changes but the callback is not called...

Does anyone know why?

Marcus Ottosson

unread,
Aug 4, 2020, 6:42:28 AM8/4/20
to python_in...@googlegroups.com
Which callback are you using? Can you post something to reproduce the problem?

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/894123b8-674c-48be-85d1-d1e5affed42fo%40googlegroups.com.

Rémi Deletrain

unread,
Aug 4, 2020, 7:35:12 AM8/4/20
to Python Programming for Autodesk Maya
in new scene launch this code:

from maya import cmds, OpenMaya

msl = OpenMaya.MSelectionList()

def get_object(s):
    mo = OpenMaya.MObject()
    msl.clear()
    msl.add(s)
    msl.getDependNode(0, mo)
    return mo

def cb(*args, **kwargs):
    
    msg = args[0]
    plug = args[1]
    other_plug = args[2]

    print msg
    print '----- Attribute {0} Change'.format(plug.name())

s_attr = 'className'

s_node_1 = cmds.createNode('transform', name='Node1')
mo_node_1 = get_object(s_node_1)
cmds.addAttr(s_node_1, longName=s_attr, dataType='string')

s_node_2 = cmds.createNode('transform', name='Node2')
cmds.addAttr(s_node_2, longName=s_attr, dataType='string')
i_call_back_id = OpenMaya.MNodeMessage().addAttributeChangedCallback(get_object(s_node_2), cb)

cmds.connectAttr('{0}.{1}'.format(s_node_1, s_attr), '{0}.{1}'.format(s_node_2, s_attr))

If you change the value of Node1.className nothing is printed in script editor.
If you disconnect Node1.className to Node2.className and change value of attribute Node2.className I have my print of my callback.

Justin Israel

unread,
Aug 4, 2020, 5:08:32 PM8/4/20
to python_in...@googlegroups.com
Looks like if you use MNodeMessage.addNodeDirtyPlugCallback() instead, it will trigger either when the attribute directly changes or when it is connected as is indirectly changed.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Rémi Deletrain

unread,
Aug 5, 2020, 4:06:46 AM8/5/20
to Python Programming for Autodesk Maya
thanks for your reply Justin.

Indeed, via this callback I enter into my function. However the plug that we gave does not yet contain the value.
So my resolver cannot process anything.

Rémi Deletrain

unread,
Aug 5, 2020, 5:40:20 AM8/5/20
to Python Programming for Autodesk Maya
In fact I recive the value before attribute change.

Justin Israel

unread,
Aug 5, 2020, 6:02:52 PM8/5/20
to python_in...@googlegroups.com
You could use the dirty callback to know that the connected plug has changed, even though it hasn't evaluated yet. And then defer actually handling its new value until after evaluation.

from maya import cmds, OpenMaya, utils

def handle_attr(attr):
    print '-- Attribute {0} Changed: {1}'.format(attr, cmds.getAttr(attr))

def cb(msg, plug, *args, **kwargs):
    print '-- Attribute {0} Dirty'.format(plug.name())
    utils.executeDeferred(handle_attr, plug.name())



On Wed, Aug 5, 2020 at 9:40 PM Rémi Deletrain <remi.de...@gmail.com> wrote:
In fact I recive the value before attribute change.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Rémi Deletrain

unread,
Aug 6, 2020, 3:20:13 AM8/6/20
to Python Programming for Autodesk Maya
Thanks Justin ! Your solution works.

I did not know the utils module and the executeDeferred method ...

Thank you again !
Message has been deleted

Justin Israel

unread,
Aug 13, 2020, 4:32:42 AM8/13/20
to python_in...@googlegroups.com
I have no explanation, but it seems that once you connect the addNodeDirtyPlugCallback, it causes the connected attribute of the second node to not update its value until the second time you set it. I've tried 2 versions of maya and it is the same behaviour.


On Wed, Aug 12, 2020 at 9:23 PM Rémi Deletrain <remi.de...@gmail.com> wrote:
Hi !

I reiterate because in fact the problem is not completely resolved.

```
from maya import cmds, OpenMaya, utils

def qd_handle_attr_cb(mp):
    print mp.adString()

def qd_dirty_plug_cb(mo, mp, *args, **kwargs):
    if OpenMaya.MFnAttribute(mp.attribute()).name() != 'fileName':
        return
    print mp.adString()
    utils.executeDeferred(qd_handle_attr_cb, OpenMaya.MPlug(mp.node(), mp.attribute()))

cmds.addExtension(nodeType='transform', longName='fileName', dataType='string')

s_node_1 = cmds.createNode('transform')
s_node_2 = cmds.createNode('transform')
cmds.connectAttr('{0}.fileName'.format(s_node_1), '{0}.fileName'.format(s_node_2))

msl = OpenMaya.MSelectionList()
msl.add(s_node_2)
mo = OpenMaya.MObject()
msl.getDependNode(0, mo)

i_call_back = OpenMaya.MNodeMessage.addNodeDirtyPlugCallback(mo, qd_dirty_plug_cb)
# OpenMaya.MNodeMessage.removeCallback(i_call_back)
```

If I set s_node_1.fileName my callback is append but the value setted on s_node_2.fileName is not valid. The attribute s_node_2.fileName has a delay value.
You have solution for this case ?

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Rémi Deletrain

unread,
Aug 14, 2020, 4:02:37 AM8/14/20
to Python Programming for Autodesk Maya
Ok thanks for your test Justin !

It's possible top considerate that a bug of attribute string ?
Reply all
Reply to author
Forward
0 new messages