Callback not working as intended? (addTimeChangeCallback)

650 views
Skip to first unread message

Mariano Merchante

unread,
Jan 11, 2015, 12:40:45 AM1/11/15
to python_in...@googlegroups.com
Hey guys! I'm new around here.

I'm currently trying to calculate some things when time changes, so I tried using the following method:

MCallbackId MDGMessage::addTimeChangeCallback(MMessage::MTimeFunction func, void * clientData = NULL, MStatus * ReturnStatus = NULL)

However, it seems that the callback is called without the expected clientData, but with an MTime given instead. 
Am I doing something wrong?

Snippet: 

import maya.OpenMaya as OpenMaya

def onTimeChangedCallback(*args):
print "addTimeChangeCallback:" + str(list(args))
    
def addNodeAddedCallback(*args):
print "addNodeAddedCallback:" + str(list(args))

def addForceUpdateCallback(*args):
print "addForceUpdateCallback:" + str(list(args))

OpenMaya.MDGMessage.addTimeChangeCallback(onTimeChangedCallback, "testStringTimeChanged")
OpenMaya.MDGMessage.addForceUpdateCallback(addForceUpdateCallback, "testStringForceUpdate")
OpenMaya.MDGMessage.addNodeAddedCallback(addNodeAddedCallback, "dependNode", "testStringAddNode")

The output, when moving the time slider:

addTimeChangeCallback:[<maya.OpenMaya.MTime; proxy of <Swig Object of type 'MTime *' at 0x00000000355106C0> >, <maya.OpenMaya.MTime; proxy of <Swig Object of type 'MTime *' at 0x00000000355106C0> >]
addForceUpdateCallback:[<maya.OpenMaya.MTime; proxy of <Swig Object of type 'MTime *' at 0x00000000355106C0> >, <maya.OpenMaya.MTime; proxy of <Swig Object of type 'MTime *' at 0x00000000355106C0> >]

And when adding any node, just to be sure...

polyCube -w 1 -h 1 -d 1 -sx 1 -sy 1 -sz 1 -ax 0 1 0 -cuv 4 -ch 1; objectMoveCommand;
addNodeAddedCallback:[<maya.OpenMaya.MObject; proxy of <Swig Object of type 'MObject *' at 0x00000000355106C0> >, 'testStringAddNode']
addNodeAddedCallback:[<maya.OpenMaya.MObject; proxy of <Swig Object of type 'MObject *' at 0x0000000035524360> >, 'testStringAddNode']
addNodeAddedCallback:[<maya.OpenMaya.MObject; proxy of <Swig Object of type 'MObject *' at 0x0000000035524360> >, 'testStringAddNode']

This means that the addNodeAddedCallback is working correctly, receiving a test string, while the other two callbacks are just receiving MTime two times.
Is this a bug, or am I doing something wrong here?

Thanks!

Justin Israel

unread,
Jan 11, 2015, 1:10:17 AM1/11/15
to python_in...@googlegroups.com
Seems like a bug. Maybe they are returning the MTime pointer twice instead of the data pointer for the second arg. File a bug?


--
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/39a47333-74e2-489d-846f-d3bd717409e6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mariano Merchante

unread,
Jan 11, 2015, 12:55:09 PM1/11/15
to python_in...@googlegroups.com
Yeah, I was thinking the same thing. Thanks for the quick response!

For anyone looking for a workaround: because the callback is independent of any object, you can set up a callback wrapper that tracks additional clientData through a map or similar.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Mahmoodreza Aarabi

unread,
Jul 14, 2016, 2:14:59 AM7/14/16
to Python Programming for Autodesk Maya

Hello guys
i know that this thread is old, but recently i was searching about one problem and i see this in group.

i do this in another way, but i have different problem that i will say.
i did this:

def do_something(*args, **kwargs):
    print "hello"

idx = OpenMaya.MEventMessage.addEventCallback("timeChanged", get_sec)

this snippet works well, but problem is that i want to get all callbacks of the current scene and remove it.
i know that the code is something like this:

OpenMaya.MMessage.removeCallback(idx)

but, finding the idx from the current scene is a problem now.

i can’t run this codes in one step, maybe i want to run the second code after a while, for example, after closing the scene again.

any idea?

thanks

Marcus Ottosson

unread,
Jul 14, 2016, 2:23:28 AM7/14/16
to python_in...@googlegroups.com

You can temporarily store it somewhere global, such as in any of your own modules.

import my_module

# Add
my_module.idx = OpenMaya.MEventMessage.addEventCallback("timeChanged", get_sec)

# Remove
OpenMaya.MEventMessage.removeEventCallback(my_module.idx)

--
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/a9930e11-c931-4a0e-837a-a9329248c2e7%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Mahmoodreza Aarabi

unread,
Jul 14, 2016, 2:26:24 AM7/14/16
to python_in...@googlegroups.com
thanks marcus
this is when i run a module in maya and in a function in it i want to remove it,
but maybe i saved the scene with this callback
restart maya and want to remove it.
what is the solution?
make sense?


For more options, visit https://groups.google.com/d/optout.



--


Bests,
madoodia

Mahmoodreza Aarabi

unread,
Jul 14, 2016, 2:30:14 AM7/14/16
to python_in...@googlegroups.com
i mean the callback will stick to the scene or not?
i think custom callbacks does not stick to the scene.
--


Bests,
madoodia

Marcus Ottosson

unread,
Jul 14, 2016, 2:42:18 AM7/14/16
to python_in...@googlegroups.com
They do not, you will have to re-add it for each session.


For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Mahmoodreza Aarabi

unread,
Jul 14, 2016, 3:01:35 AM7/14/16
to python_in...@googlegroups.com
i think so
but removing it in the current session does not work.
i mean when i create a callback i have to find it in all callbacks and then remove it when i want.
make sense?


For more options, visit https://groups.google.com/d/optout.



--


Bests,
madoodia

Mahmoodreza Aarabi

unread,
Jul 14, 2016, 3:19:04 AM7/14/16
to python_in...@googlegroups.com
there is any way to find all custom callbacks in a scene?

--


Bests,
madoodia

Cesar Saez

unread,
Jul 14, 2016, 6:33:31 AM7/14/16
to python_in...@googlegroups.com
You don't even need an external module to save your ids in memory :)

---

from maya import OpenMaya


# setup
if not globals().get("my_callbacks"):  # this avoid overriding the ids on re-run
    my_callbacks = dict()

def addCallback(func):
    idx = OpenMaya.MEventMessage.addEventCallback("timeChanged", func)
    my_callbacks[idx] = func
    return idx

def clearCallbacks():
    for idx in my_callbacks.keys():
        OpenMaya.MEventMessage.removeCallback(idx)
    my_callbacks.clear()

# usage

def do_something(*args, **kwargs):
    print "hello"

addCallback(do_something)
addCallback(do_something)
addCallback(do_something)

print my_callbacks  # {id: func}
clearCallbacks()
print my_callbacks  # empty

Mahmoodreza Aarabi

unread,
Jul 14, 2016, 6:58:09 AM7/14/16
to python_in...@googlegroups.com

Hey Cesar
nice idea

in recent hours i found a way in one module that is like your idea but with list:

callbacks = []

def do_something():
    print "anything"

def main():
    global callbacks
    idx = OpenMaya.MEventMessage.addEventCallback("timeChanged", get_sec)
    callbacks.append(idx)

def remove_callbacks():
    for call in callbacks:
        OpenMaya.MMessage.removeCallback(call)

it works fine for me

thanks man
thanks to marcus


--
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.

For more options, visit https://groups.google.com/d/optout.



--


Bests,
madoodia
Reply all
Reply to author
Forward
0 new messages