Remotely sending code to Maya from Wing

by Nicholas Stevenson

Note:  This is the second part of a series of posts regarding Wing IDE and Maya.  For the first post, please follow this link. Alternatively, look further down this page for the post titled 'Using Wing IDE with Maya'.

Before I continue, I would like to thank Eric Pavey for his work on expanding Wing, allowing you to remotely execute your code in Maya while working in Wing.  

I had the pleasure of working with Eric on Modern Warfare 3 and to say that he was a prolific technical animator wouldn't do him justice, he did an absolutely astounding amount of work on that project.

I later discovered that he had created this Python module that I was already using in Wing.

So, huge thanks to Eric Pavey.

Installing the Wing Module

The first step is to install Eric's Wing Module, I have remotely hosted the Python file: wingHotkeys.py

The original can be found on Eric's web site: http://mayamel.tiddlyspot.com/

After download this file, you will need to place it in a directory where Wing can find it.  Wing already provides a directory for custom scripts, but it can be a little difficult to find, particularly if your OS is hiding system directories.

Open Wing and from the top bar, choose Edit -> Preferences

Towards the bottom of the preference list, choose IDE Extension Scripting

Transient

In this section you can see Wing's primary location for housing custom scripts, as well as a place for adding your own.  

Place the downloaded module in to either Wing's current script directory, or point wing at the directory where you saved the script.

My provided version of the module is  a slightly modified version, though all of its original contents are still available.  The module basically takes your currently highlighted text in Wing, dumps it to a temporary file, and then triggers Maya to read in that text file line by line and execute it as either Python or Mel code.   

The creation of the socket connection will be the largest hurdle, since the OS and its network settings may block certain socket connections.  My home machine worked without hassle, while my work machine took much more persuasion.  

My modified version has an extra line or two which was necessary for my work machine to function properly.

There will be a troubleshooting section at the end of this post and I'll try to explain how you may need to modify this module to work with your current system.  But for now, there is more to be done!

Installing the Maya Module

The next step will be to provide Maya with the module that performs the task of reading in the temporary text file as Python or Mel text.  

Take this provided script (which is also available at Eric's web site): executeWingCode.py

Place this script inside your Maya script directory (Eg. C:\Users\username\Documents\maya\scripts\)

To ensure that Maya can find the module, try importing it inside Maya by using the python code

import executeWingCode

If Maya responds with an error, please see the previous blog post, particular the section about appending your sys.path with other directories that Maya may not be watching.

Modifying Wing's Hotkeys

Next, we need to bind a hotkey inside Wing that will execute our newly installed Module.  

Inside Wing, choose Edit -> Preferences, and on the left, under User Interface, choose Keyboard.

In the center right of the Keyboard section is where you can add custom key combinations to execute code.  In this example, I have bound Control + Return as my key combination for executing my selected text as Python code in Maya.

Transient

The code for the command comes from the wingHotkeys.py file that you placed in Wing's script directory.  You can also bind a combination to mel_to_maya to perform the same with Mel code.

Opening the Command port in Maya

Now that you have installed and bound the Wing module to a hotkey, as well as installed the script that Maya is going to use for executing the text file created by Wing, we now need to open up Maya Command Port.  Opening the command port is very similar to modifying your Firewall to allow in a connection from outside your network.  Maya will ignore all command port connections unless you specifically open one.

Take this code and bind it to a Maya Python shelf button.

cmds.commandPort(name="127.0.0.1:6000", echoOutput=True)

Take this code and bind it to another shelf button, this code will close the command port.  Occasionally the connection between Wing and Maya will break, closing the command port and re-opening it frequently fixes this.

cmds.commandPort(name="127.0.0.1:6000", close=True, echoOutput=True)

Conclusion

If you have followed all of the steps, You can hopefully execute your Wing code in Maya, allowing you to work entirely from inside Wing, without ever needing to use Maya's built in script editor.

Troubleshooting

First, lets go over a checklist and ensure that all assets are accounted for:

  • wingHotkeys.py : Is this located in a place where Wing can find it?

To test, when adding python_to_maya to a hotkey from inside Wing's Preferences -> Keyboard section, Wing should start to fill in the Command section as soon as you start typing python_to_maya.  This shows that Wing has scanned the file and added the functions to its list of available functions.

If Wing doesn't do this, double check the location of the file

  • executeWingCode.py : Is this located in a place where Maya can find it?

To test if Maya is able to find the executeWingCode module, use the following code while inside Maya.  If this fails, follow the directions in the previous blog post that explain how to add specific file paths to your python sys.path.

import executeWingCode
  • Did you open Com Port 6000 inside Maya?

If you are certain that Wing is able to find its module, you can actually monitor what Maya is returning to Wing when Wing attempts to connect through the command port.

In Wing, open up the Messages Tab, and switch the Show Messages option to Scripts

If you see sent plus a random number, this means that the wingHotkeys module is working and is able to connect to Maya.  If however, you see Send to Maya fail, Maya is likely rejecting the socket connection.

Transient

Connection refused...

If Maya is rejecting the connection, or if you are having another issue relates to sockets, there are several things that you can try, and all of them exist inside the wingHotkeys module.

Open up the wingHotkeys.py file that you saved in Wing's script directory.  

Useful Note:  If you have the file open in Wing, and make changes to it, Wing will reload the script after you save the file.  This is really helpful when trying various fixes.

Inside wingHotkeys.py, there are two important sections that you will need to look at.  The first section deals with creating the socket.

Transient

Try commenting out the current mSocket method and un-commenting one of the other two options further up.

Transient

Same with the mSocket.connect lines here, if you are having trouble, comment out the mSocket.connect currently being used, and un-comment out one of the other lines.

You will likely need to fight with both of these sections before you find a combination that will work.  The current setup provided in my hosted file works perfectly for both my Windows 7 machine at home, and my Windows 7 machine at work, though my work machine typically requires several Ctrl+Return entries before it makes a connection.

I hope this helps, and please let me know if you have any issues!

Thank you for reading!