Threading in maya.

So a while back I wanted to create a background thread that checks the user selection at any given point. Now I know I could have done this with a script job but I wanted more flexibility so started looking into threading.

Now threading in maya is somewhat a nightmare as threads are not safe so if a thread crashes so does maya.

But I think I’ve come up with a “somewhat” stable version of threading. I am kind of cheating the function to run on the main thread. This way most Pymel functions will still work correctly without erroring out.

Now be careful though: It can still be unstable especially when using print statements.

The problem is that if one thread gets delayed and the other one starts that there is a slim chance that two or more threads try to do the same operation in maya at the same time. So for example they will both try to move object.vtx[1] to different positions or print to the script editor at the same time it will result in a crash as maya does not know what to do with the situation.

The class here calls the provided function every 1 second. unless specified otherwise. The timer will never be faster than the function takes. so if the function takes 3 seconds to complete then the interval will be 3 seconds even though you might set it to 0.001

It also supports string and mel functions.

Here is the code:

The evalDeferred is the most important part of this class. Without it there is a very high chance that maya will crash as it will most likely try to do two or more operations at the same time.

To see this in action with a funky little example copy this code into a file named:
threadedFunctions.py
into your scripts directory. i.e. C:\Users\<username>\Documents\maya\<maya version>\scripts

Now in maya create a poly plane of 100 units wide and high. (make sure you leave the name as default i.e.: “pPlane1”)
and give it 10 subdivisions in each direction.

and run this code:

Now if you are lucky maya dint crash and you will see a flag like motion without the timeline advancing.

VERY IMPORTANT NOTE!!!. if you want to stop the thread run the following code.

DO NOT REDEFINE THE “flagThread” variable as that will lose your chance to kill the thread.

I hope this is of some use to someone. I guess you could even make a simple game with this in maya haha :D.

 

 

 

 

MEL Autocompletion for Sublime and MayaSublime

Justin Israel made an awesome extension for the Sublime Text editor called MayaSublime.
https://github.com/justinfx/MayaSublime

But I noticed there is no auto completion file for the MEL syntax so I decided to generate one.

This contains all MEL commands that I can find. Half of these are generated by the help of the Maya help command:
help -list “*”

The rest is generated by parsing all the MEL scripts and taking all “global procs” in the Maya installation scripts folder.

Hope this is of some help to people still having to manage legacy MEL scripts ^^.

I also added the script that I wrote to generate this which might be useful if you are working in a different version of Maya than this file is generated from. (Maya 2014)

Feel free use edit and or modify these files in any way you see fit.

The zip file contains the MEL.sublime-completions and the Python file used to generate this file.

SublimeAutoComplete  < CLICK HERE >

(links dont show up properly yet XD)

Copy the MEL.sublime-completions here:

C:\Users\<userName>\AppData\Roaming\Sublime Text 3\Packages\MayaSublime

If you want to generate this file do the following steps:

1) Copy the SublimeMelAutocompletion.py into your scripts folder.

2) C:\Users\<UserName>\Documents\maya\<Maya Version>\scripts

3) Load up maya.

4) open the script editor

5) type:

Flags:

mayaOnly = This will export only maya commands and scripts that are in the install folder ( C:\Program Files\Autodesk\<mayaVersion>\scripts)

Setting this to False will look in all possible scripts paths defined in the environment variable called: MAYA_SCRIPT_PATH

apiOnly = only generate the commands that are listed in the maya command reference.

filePath = The file path the document will be exported to. i.e. C:\MEL.sublime-completions

If you leave the filePath argument out of it then it will export to the default sublimeText3 mayaSublime folder if it exists:

C:\Users\<userName>\AppData\Roaming\Sublime Text 3\Packages\MayaSublime

It will try to detect your user name automatically.

 

 

Maya’s “pasted_” is history and maya root namespace.

PASTED_

In maya the copy paste feature can be really useful. The major downside however is that every node that gets pasted gets a nice:

PASTED_ prefix attached to it. Resulting in a horribly messy scene that no one ever cares to clean up… until you have to revisit the scene and have to dive into the world of pasted_ nodes trying to find the correct shader etc.

Now there is a really easy fix for it:

In your maya instalation directory there is a file called cutCopyPaste.mel (of course thanks autodesk … )

in that is a function called pasteScene()

replace that function with the following code:

This will merge all the pasted nodes directly into the scene (root namespace ) without adding any prefix or suffix. (Only when the node all ready exists in the same depth it will add a number to the end).

Root Namespace

Whenever I loaded in a referenced scene maya would create a namespace (either with an normal namespace or it would prefix the nodes with the filename and an underscore). However Autodesk has finally been nice enough to open the functionality in the UI to load a scene into the root namespace (making sure the referenced scene does not get any weird naming or namespace. Making it easier to work with)

When going to File > Create reference

The trick is not to disable namespaces on load but enabling them and selecting the : ( root )  namespace and set the following option:

Merge into selected namespace and rename …

Mel and Python do go hand in hand at times.

Seeing as some developers still work with mel and a lot of legacy scripts within companies are still written and maintained in mel its sometimes useful to call python functions in mel when having to extend those old legacy scripts. now most people know you can do a:

inside of mel.

But I recently found a mel wrapper function that does this for you!!! And not only that it also adds the proper from module import function path as well!

So this is actually quite usefull so i thought i’d share it here:

for example. Let say you have the following python script that connects two attributes.

Save this to a file and store it somewhere in the maya scripts paths.

(i.e. my s\Documents\maya\2014-x64 )

Now import the script:

This will run the script and as I added a print it will output the actual code it evaluates:

and now you can go to mel and do:

*note i added the “rehash” to make sure the mel scripts are refreshed.

 

If you want to get more information about this wrapper function run the following lines inside of a python tab in maya:

Or take a look here:

http://download.autodesk.com/global/docs/maya2014/en_us/index.html?url=files/GUID-8A96A8DB-FD6F-434F-A878-288DD84E99C7.htm,topicNumber=d30e727627