Calling user function from module

26 views
Skip to first unread message

Nicolas Chaverou

unread,
May 28, 2018, 6:20:36 AM5/28/18
to python_in...@googlegroups.com
Hi guys,

I'm trying to implement a user callback mechanism;
I'm having a function in a predefined module called glm.utils :

def createNodeUserCallback(functionName, nodeName):
    callbackFunction = functionName + 'UserCallback'
    call = callbackFunction + '(\'' + nodeName + '\')'
    try:
        eval(call)
    except:
        mutils.warning('Error while calling Python user callback')


This function is called here and there with different parameters and expects our users to declare and source some functions
Those functions are sometimes declared in their own modules (not known from our side) or in the Maya Script Editor

Problem is those functions are not available from our glm.utils module.
Someone has an idea about how I could solve this ?

Thanks

Marcus Ottosson

unread,
May 28, 2018, 6:34:46 AM5/28/18
to python_in...@googlegroups.com

How about this, for the user:

import glm

def mycallback():
  print("Did it!")

glm.registerUserCallback(mycallback)

That way, your registerUserCallback can take and store the callback, regardless of whether it’s from the Script Editor or another module.


--
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_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFS5DCarx4ENBitQT8G-_dcqpSDuVtSdmW5Q0rpAy76naVN%2BnA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Justin Israel

unread,
May 28, 2018, 7:01:01 AM5/28/18
to python_in...@googlegroups.com
I definitely second the suggestion from Marcus and recommend to avoid trying to construct a text based expression to pass to eval. Inverting the design and offering a callback registration is much more flexible and reliable. 

To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

--
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/CAFRtmOCjyGX-Nz0L7pJ4J0v8jfNVEUA%2Bf5OXzcnSF5stCme_cw%40mail.gmail.com.

Nicolas Chaverou

unread,
May 28, 2018, 11:57:54 AM5/28/18
to python_in...@googlegroups.com
Totally valid. Thanks for the suggestion, it's way easier and pythonic :)


To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

--
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_maya+unsub...@googlegroups.com.

--
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_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA2%3Dvpnk4d14S5TCYGw%2BVkjPXuLySoMR_ssspd9_4sqzPA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages