PyCharm & Maya commands

maya

#1

Hey all,

with some downtime at work I am taking the opportunity to experiment with some python tools and was using the evaluation version of PyCharm (after reading Seth’s and Rob’s praises of PyCharm).

While I am liking the IDE feel so far, there’s some issues I can’t seem to find a way to fix;
first there’s some sort of bug with their perforce module with client spec and relative paths, which I guess would be resolved soon.

But my main issue right now are the unresolved reference warnings with any Maya commands I am using.
Since I was just upgraded to a win7 box I was able to use that trick with the mayapy.exe symbolic link trick to have PyCharm use Maya’s interpreter. But despite having the right paths added to the interpreter’s path it still doesn’t recognized the maya.cmds commands as existing, let alone completion.

I am hoping I just made a mess of my projects settings? Has anyone managed to have it working properly?


#2

I actually went back to Wing after the last build of PyCharm i got (i forget which one it was) was incredibly instable. There were some basic workflow issues with PyCharm i eventually couldn’t get over either, despite some of the really cool things it did as far as type inference, etc.


#3

ah damn
thanks for the feedback though :wink:


#4

I’ll tell you if I ever get this working. I have only done pure python work with PyCharm. I may just go dual-IDE for Maya since I do not want to give up Pycharm.


#5

Eclipse + Pydev = free awesomeness.

There are also modules for MEL on that.


#6

IDE war de-railment warning…

:sigh:

:wink:


#7

[QUOTE=rhexter;11593]IDE war de-railment warning…

:sigh:

;)[/QUOTE]

IDLE FTW, there.:D:


#8

for those interested, I’ve had them open an issue (http://youtrack.jetbrains.net/issue/IDEA-73046) to solve the problem with the perforce client spec I was referring to.

Now, concerning the unresolved references with the maya.cmds commands, it seems the skeletons is not created for this module, since there’s actually nothing to build it from.

import maya.cmds as cmds
cmds.__file__
# Result: C:\Program Files\Autodesk\Maya2012\Python\lib\site-packages\maya\cmds\__init__.pyc # 

That folder is actually empty on my install and I am not sure how to go around this and have the commands recognized by PyCharm (which is properly using the maya python interpreter as the default interpreter).
I am guessing this is a runtime thing and those commands don’t exist until Maya is running or something?


#9

Hmmm…


#10

Pffft. Real men use NPP.


#11

if it ain’t emacs, why am I using it to write code?


#12

[QUOTE=greuh;11718]for those interested, I’ve had them open an issue (http://youtrack.jetbrains.net/issue/IDEA-73046) to solve the problem with the perforce client spec I was referring to.

Now, concerning the unresolved references with the maya.cmds commands, it seems the skeletons is not created for this module, since there’s actually nothing to build it from.

import maya.cmds as cmds
cmds.__file__
# Result: C:\Program Files\Autodesk\Maya2012\Python\lib\site-packages\maya\cmds\__init__.pyc # 

That folder is actually empty on my install and I am not sure how to go around this and have the commands recognized by PyCharm (which is properly using the maya python interpreter as the default interpreter).
I am guessing this is a runtime thing and those commands don’t exist until Maya is running or something?[/QUOTE]

For the client spec issue I was having the same problem here with both PyCharm and Wing. I fixed it by setting the altRoots of our client spec to point to the top level project directories for PyCharm/Wing. Fixed it for our case and we have null as root and use the view to map files all over the place…

As for the command completion, try adding the PyMel .py autocomplete files that the PyMel guys were generous enough to provide for us with Maya. (MAYAVER\devkit\other\pymel\extras\completion\py) The issue is that those commands and the api are coming from compiled extensions (.pyd) and PyCharm/Wing can’t load them because they require Maya to be initialized first.

Also, I have a ruby micro plugin written via PMIP for PyCharm that can send the selected text over to Maya via commandport, I’ll post that up tomorrow if I don’t forget… :slight_smile:

Edit Attached the PMIP script for sending to Maya. Some steps involved in setting up PMIP for your project are here:
http://code.google.com/p/pmip/wiki/GettingStarted
http://code.google.com/p/pmip/wiki/InstallingTheCoreBundle
After you’ve installed the core bundle, just add the line:

plugin 'maya'

to your pmip.rb file and unzip the the folder into your PMIP plugins directory.


#13

Hello, thanks for the info Temujin.
I have 2 questions for you.
1- I run your ruby script, it works fine, except, I can´t use pyqt, is not showing windows, is this a limitation of Mayapy? any workaround for this?
2- Where do you add the autocomplete files in PyCharm?

If is not possible to use qt GUI`s by this implementation, is there any other way to work in pycharm and sending commands to maya? Im currently using gVIM, with Vimya, I could add the auto completion files with ctags, (i dont know how to do that in pycharm), is working fine but I prefer pycharm than gvim, actually gvim is not using Mayapy, is using another python environment just for editing, but the real commands are send to mayapy via vimya, maybe i can do that in pycharm but i would not be able to debug with it.


#14

I don’t seem to have the same issue as you with the pyqt windows, mine display fine.
For the auto-complete, I just add them to sys.path at the end in my sitecustomize for the python exe that my pycharm is using.


#15

[QUOTE=Temujin;16920]I don’t seem to have the same issue as you with the pyqt windows, mine display fine.
For the auto-complete, I just add them to sys.path at the end in my sitecustomize for the python exe that my pycharm is using.[/QUOTE]

Thanks a lot Temujin, I got the auto completion.
However, I don´t know what I´m missing with the ruby plugin, it works but only with one line commands and small scripts.
When I try it with long scripts maya returns weird errors like “syntax error” or missing definitions.
I try to increase the buffersize with no luck.
What I´m missing here?
This is the command with wich I open my port:

cmds.commandPort( n=“localhost:4567”, stp=“python”, bufferSize=99999)

Thanks a lot for your help!


#16

I’ve followed this thread, and my experience so far has been this:

[LIST=2]
[li]The ruby plugin provide simply sends selected text from IDE to Maya. It doesn’t care if the selected text is python or MEL. Thus, the method used to open your commandPort in Maya must match the type of code you’re sending from PyCharm.[/li][li]Even if you open a python flavor commandPort, the text string being sent over is being directed via Ruby syntax, and therefore must be properly escaped according to Ruby rules to ensure a valid string. This limits a lot of normal python code from being properly sent over because of the single quote and colon to name a few.[/li][li]Variables and definitions weren’t being placed into the main namespace, which is different then the normal behavior of executing stuff in the Maya Script Editor directly.[/li][/LIST]

After looking at the plugin, I ended up with a solution similar to what I use in Wing, and adapted it for Ruby. Here’s the new maya/init.rb ruby plugin code:

maya\init.rb


require 'socket'
require 'tempfile'

class SendToMaya < PMIPAction
  #
  # SendToMaya ruby code, uses PMIP plugin in pycharm
  # This script requires you to open a python commandPort in Maya as follows:
  #
  #     import maya.cmds as cmds
  #     cmds.commandPort(n=':4567', stp='python', eo=True, nr=True)
  #
  # :note:  If you changed the host and port below you will also need to
  #         update the commandPort command above to match

  def run(event, context)

    # configures the commandPort address
    host = '127.0.0.1'
    port = 4567

    balloon = Balloon.new(context)
    editor = context.current_editor
    text = editor.selection()

    if not text.length>0
        balloon.warning("Nothing selected to send.")
        return
        end
    else
        # Write our selection to the tmp directory.
        # We'll use this file to relay the code to Maya
        file = Tempfile.new(['pycharm_out','.py'])
        file.write(text)
        file.close

        # Contents of your selection, to be printed in script editor
        outtxt = 'with file(\'' << file.path << '\') as f: print(f.read())'

        # Execute the contents of our tmp file into the main dictionary
        pystr = 'import __main__; execfile(\'' + file.path + '\', __main__.__dict__, __main__.__dict__)'

        begin
            puts 'Executing selection...'
            maya = TCPSocket.open(host, port)
            maya.puts(outtxt)
            maya.puts(pystr)
            result = maya.recv(4096)
            maya.close()
        rescue
            puts("    Warning:  Unable to communicate with Maya.  Expecting an open python commandPort at " << host << ":" << port.to_s)
            balloon.warning("Unable to communicate with Maya.
Expecting an open python commandPort at " << host << ":" << port.to_s)
        ensure
            # Delete the temporary file
            file.unlink
        end

    end

  end

# Set the hotkey for the command
bind 'ctrl E', SendToMaya.new

At this time, the modified plugin only handles python code. I don’t care to write MEL in PyCharm personally, so I didn’t add the logic to deal with it. If someone felt inclined to add it, you could add a if/else case for MEL and parse the tmp file using the MEL “source” command. This plugin also won’t take the active file, and import it when nothing is selected. More often than not, I want to craft the namespace I’m importing to, and you can still do that by executing some selected text. Lastly, if you have remote debugging working, Maya and PyCharm both hang when you attempt to execute code from PyCharm using this plugin. This likely has to do with two TCPSockets waiting for each other to finish, and I’m not sure how to work around this cycle. Just note this doesn’t work given this implementation (but if you can figure it out, let me know!).

I haven’t put it through a ton of stress testing yet, so give it a go and if anyone finds something odd, let me know. Thanks to everyone who started this ball rolling!

-csa


#17

I also did some modifications to the JRuby code, I mixed the approach by Nathan Horne (PMIP+JRuby+PythonCommandPort) with the Vimya approach (TempFile+Tail_LogFile+MelCommandPort), I first try to use a Python Command port, but I was receiving weird errors, probably due to encoding in windows, the same command execfile, didn’t work for me via python port, I have to send the python script using mel, trough mel port, using Jruby, duh! but that made the trick, I also try two JRuby Gems(filewatch-0.3.4, jruby-notify-0.0.3) , to filewatch/tail the log file and output to the PMIP console, but no one suited me or at least I couldn’t make it work properly, refresh was lasting to long, I try some commands in the Java API of Intellij, but seemed to complex for me, I don’t know Java also, so I make a dirty trick and made a Python Console Starting Script for PyCharm, which also colors warnings yellow, errors red, and results green, and maintains tailing the output of the script editor, so I can close that window forever (which save me space in my two monitor setup), I also add Mel support with ctrl+alt+shift+E cause I already was using a MEL port, (someone has Syntax Highlight for MEL in PyCharm??).

So what basically the two scripts do is:

  • CTRL + E, without text selected, saves files and send the current one to Maya.
  • CTRL + E, with text selected sends selection to maya, without saving files.
  • CTRL + SHIFT + ALT + E, sends selection as MEL to maya.
  • Running Python Console, redirects ScriptEditor output to PyCharm, and colors warnings, errors and results (CTRL+C, stops it)


MayaCharm.zip (1.51 KB)

To open the mel port:

  • Add this line to your userSetup.mel in <drive>:\Documents and Settings&lt;username>\My Documents\maya&lt;Version>\scripts
    if (! commandPort -q ":4567") commandPort -n “:4567”;

To view the commands and results from the output of the script editor in PyCharm you need to add the ConsoleScript.py to your console starting script in PyCharm:
-Enter your settings in Pycharm
-Click on Console, then Python Console, and paste the script in Starting Script
-Replace the line that says <REPLACE ME WITH LOG FILE, REPLACE \ for \> with the log file created by the MayaCharm plugin, you can know which it is in your PMIP console output.
-Replace all the slashes with double slashes; \ for \.

I don’t know if this is working in other systems, environments, version, so feedback will be appreciated.
I´m working in a step by step, tut for this, and also setup debugging, for dummies like me, I will post it here when I finish.
Did someone knows something about this?? --> http://tech-artists.org/forum/showthread.php?3135-PyCharm-remote-debugging-Maya-question

Thanks a LOT to Nathan Horne for pointing me out in the right direction, not only in this field, but in others, I´m totally new to this and his posts had helped me a lot.
And excuse my wrong english is not my first language.

Macbeth Reyes F.


#18

I are not appearing to have the same issue as you with the pyqt microsoft windows, mine screen fine.
To the auto-complete, I just add the crooks to sys. path at the conclusion in the sitecustomize for that python exe in which my pycharm is actually using.