mayamel

how to go about safely calling procedures defined in shelft items elsewhere?


Lately I have been dealing with a situation where, I will try to call a function, whose definition only exists in a shelf item, via a hotkey or the script editor or in other places, etc etc. And I will end up getting an error:

// Error: Cannot find procedure "hello".

If this was an isolated instance, I would be fine with it but its something I am dealing with constantly, I have a few scripts I have bought/created that are only stored in shelf items. So this creates a problem where, if I have so far not called the function by clicking the shelf item, I will get an error.

A solution that comes to mind is to just copy and paste these definitions to the custom scripts ("hotkey editor" > "custom scripts") window but this just moves the problem to another location. As I then, would not be able to call the procedure from the shelf, the script editor, etc etc.

I will have to

I am essentially asking for a design or a solution, where as soon as I start the Maya programme, I can call a procedure that is saved in the shelf from the script editor. Furthermore, avoid repetition work, where every time a developer updates their tools, I have to update it at multiple locations.

If such a feature is not available, then I would like to hear how you guys approach this issue, surely I am not the only one that has confronted it.

What I tired: I thought the global keyword was for this very purpose. I tested it by saving a procedure in a shelf item and trying to call it in the script editor, and I get the above error:

global proc hello3(){
    print("\n hello world \n");
}
hello();

Am on Maya 2025, thanks for any input.


Solution

  • What you're basically asking for is a function library; a series of script/code files containing functions. Ideally these files are loaded when the application starts so you can simply call/execute them. Most of Maya's Mel/Python functions are loaded this way.

    Normally, shelf buttons shouldn't hold all the script code precisely for the reason you're describing. Instead they simply "source" or "import" the file's function and there's usually a line to call/execute the function to perform an action/task.

    In a nutshell: you need to move the code called from your shelf button into a mel file and then have this "sourced" every time Maya opens so it's accessible.

    Be warned that beyond this point you're crossing into technical/scripting territory so I won't elaborate (you can research this at your own pace if you don't understand), but here is an overview to get you started:

    1. Create a mel/python file and declare one or more functions that will perform some task(s). In your case you would move your shelf button code under a new function within a file.
    2. Test the function(s) you created by manually sourcing/importing them in the script/editor and executing them, making sure the function(s) work as intended and have no issues.
    3. To manually source a mel file, in a Script Editor MEL Tab enter source "d:/path/to/my_script.mel";. Replace "d:/path/to/my_script.mel" with the actual path to a file. You should be able to execute any functions declared in the file.
    4. To make the function(s) visible every time you open Maya, you must modify a special file Maya looks for called userSetup.mel for Mel code, or userSetup.py for Python code. You'll have to research where this file lives (you might even need to create it).
    5. For the userSetup.mel you would need to add a line to "source" your file(s): source "d:/path/to/my_script.mel";. This ensures that every time Maya opens, that file is "sourced" and the functions declared within are visible throughout your Maya session.
    6. Restart Maya and see if you can execute the functions contained in your file(s) WITHOUT sourcing them manually.

    Once you've achieved all of the above, you should now be able to replace the code assigned to the shelf button with simply a function call. If you need to modify the code, simply edit the file, save, and restart Maya.