Maya Python API 2.0 - IDE Code Complete

Maya Python API 2.0 - IDE Code Complete

robotProdigy
Advocate Advocate
18,179 Views
18 Replies
Message 1 of 19

Maya Python API 2.0 - IDE Code Complete

robotProdigy
Advocate
Advocate

I’m attempting to get my Python IDE (PyCharm) to code complete Maya Python API 2.0 objects. Since I’m working in the mayapy.exe environment, and including “C:\Program Files\Autodesk\Maya2016\devkit\other\pymel\extras\completion\py\maya” as an additional environment path, I’m getting good solid code completion for all my Maya.cmds and PyMEL objects. Since Pymel is using Maya’s Python API 1.0, I’m getting code completion for that API as well, but for Maya Python API 2.0, I get nothing. I’ve googled, and tried all kind of tricks, including typing up my custom code-complete skeleton files which literally involves me typing up the whole API 2.0 object model hierarchy into doc strings. Although that works, that’s not going to happen – I will have died of old age before getting half way done.

Any pointers? Solutions?

Accepted solutions (1)
18,180 Views
18 Replies
Replies (18)
Message 2 of 19

robotProdigy
Advocate
Advocate
Accepted solution

Ok, I have a working work-around (and it is a work-around). Even though I'm accepting this post as a solution, I hope Autodesk can include the necessary files in the Maya install at some point so that we get the same auto-complete we get from Maya.cmds and Maya Python API 1.0.

Before the work-around, let me clear something up. There is no need to include an additional environment path in PyCharm in order for it to "see" maya.cmds and maya.OpenMaya. It will do it by default once you set the Python environment to mayapy.exe. 

 

Now - How to get PyCharm to "see", or auto-complete, for Maya Python API 2.0 classes e.g. maya.api.OpenMaya, vs maya.OpenMaya which is API 1.0. This requires modifying 4 files, and has not caused any problems on my end (so far), so take this as an "as is" solution. Also, if you're using Maya 2016, there is a prerequisite - download and install Maya 2016 devkit. If you're using 2015, you already have it. Also, I am using Windows 10, and Maya lives on my C, so bear that in mind if you're using OSX or working off your D drive, etc.

 

  1. Open, and add one line of code to the following 4 devkit files. If you want, make a copy of them first so you can revert back without having to re-download the devkit.
    1. open file C:\Program Files\Autodesk\Maya2016\devkit\other\pymel\extras\completion\py\maya\api\OpenMaya.py. It should be empty. Add the following line of code, and save. 
      from _OpenMaya_py2 import *
    2. Open OpenMayaAnim.py from the same directory. It should be empty. Add the following code and save.
      from _OpenMayaAnim_py2 import *
    3. Open OpenMayaRender.py from the same directory. It should be empty. Add the following code and save.
      from _OpenMayaRender_py2 import *
    4. Open OpenMayaUI.py from the same directory. It should be empty. Add the following code and save.
      from _OpenMayaUI_py2 import *
  2. In PyCharm go to File/Settings. Go to Project: (your project name)/Project Interpreter. From the Project Interpreter dropdown, select Show All
     image_01.png
  3. You should see a Project Interpreters dialog. Select your interpreter, and then the "Show Paths" button - the weird looking button above the cancel button.
    image_02.png
  4. Now select the add button (green plus)
    image_03.png
  5. Surf to C:\Program Files\Autodesk\Maya2016\devkit\other\pymel\extras\completion\py and select OK till you're back in the regular PyCharm editor.
  6. Everything should be in place now. It's a matter of PyCharm discovering the new classes/methods/variables for code completion. Your Pycharm may update now, after you restart, or some other time. Personally, I erase all doubt, and opt to go to File / Invalidate Caches/Restart... This will hose it's current auto-complete database, and force PyCharm to rediscover all your working Python environment Symbols and build a new database, which takes about 10-20 seconds of processing time on my end.
    image_04.png
  7. Now in PyCharm should auto complete for Maya API 2.0 objects, that is, the object model living in maya.api.
    image_05.png
  8. Also "Quick Documentation" will work as well as argument hints (not shown)
    image_06.png

 

I'll update this post if I find any issues, but again, this is working pretty well as of now. Please let me know if this is producing problems, because I would like to know.

Cheers,
Paul W

Message 3 of 19

robotProdigy
Advocate
Advocate
Update: This solution is only working in PyCharm 2016 on my end, but not the previous version of PyCharm which is PyCharm v5.
0 Likes
Message 4 of 19

saihtam
Collaborator
Collaborator
Thanks for the info on this. I'm just about to jump into some Python Dec and will be using PyCharm. A coworker has been doing a lot of work with this stuff, I'll try to remember and check if he has any insights.
- Mathias
0 Likes
Message 5 of 19

stephanosterburg6SYHS
Explorer
Explorer

Hello, I am trying to follow your footsteps but no luck on my side. That said I am in a Mac environment and use Maya 2017.

 

1. In regards to step 1, the files are not empty.  I tried it with and without the lines added.

2. after that I followed your steps, but the auto-completion won't work for me passed the import line (see image)

 

I tried also to follow https://vimeo.com/149598498

 

thanks for the help.

 

 

 

pycharm_maya.jpeg

pycharm_maya2.jpg

0 Likes
Message 6 of 19

robotProdigy
Advocate
Advocate

Too bad you're having issues on your end.

 

So I tried setting this all back up on my end, and I absolutely cannot get code completion to work on anything. Not maya.cmds, pymel.core, OpenMaya, etc. When I wrote the original doc, I was using an earlier version of PyCharm 2016. I'm now on PyCharm 2016.2.3. I'm not sure if the issue is there, or I'm overlooking something on my end. Pretty frustrating.

 

-Paul

0 Likes
Message 7 of 19

robotProdigy
Advocate
Advocate

OK I got a little further on my end. 

 

After removing this from my interpreter path:

C:\Program Files\Autodesk\Maya2016\Python\Lib\site-packages

 

And adding this to my interpreter path:

C:\Program Files\Autodesk\Maya2016\devkit\other\pymel\extras\completion\py 

 

I got my maya.cmds and pymel.core auto completing. The reason (I think) is that the site-packages path contained a maya.cmds __init__.py file that was empty while the ...completion\py contained a maya.cmds __init__.py file with content which populates PyCharm auto complete widget. 

  

For it all to make better sense, take a look at the image. Take a minute to digest it. PyCharm pulls code completion data from the Interpreter Paths specified for your interpreter which of course is mayapy. Notice how the directories/files listed in the Interpreter Paths window below show up in the External Libraries tree on the left. The ...\extras\completeion\py Interpreter Path is listed as py in the External Libraries tree. Since there is a listRelatives() function in the cmds module which is inside the maya module under the ...\extras\completeion\py directory, PyCharm is able to auto-complete on the right. 

 

 pycharm autocomplete demo2.png

 

So you can find the maya/api/OpenMaya __init__.py file, in the External Libraries tree, look inside, that might be telling of what's going on. If it's an empty file, OpenMaya won't auto complete. OR if there is another directory in your External Libraries tree that also has a maya/api/OpenMaya __init__.py file, it could be overriding your good OpenMaya __init__ file. That's what was happening with my maya.cmds earlier in the post.

 

So if you're following on this, it maybe a matter of tweaking things a little different on your mac to get things up and running. Pain in the butt, obviously, but it might be possible if you're persistent with it with a touch of OCD 🙂

 

-Paul

 

Message 8 of 19

stephanosterburg6SYHS
Explorer
Explorer

Thank you so much for your help. I will give a try.

0 Likes
Message 9 of 19

stephanosterburg6SYHS
Explorer
Explorer

Thanks for the explanation, it works now.

0 Likes
Message 10 of 19

Anonymous
Not applicable

Hey I did everything done in this post like the other guy my files weren't empty so i skipped the first step and this is what it looks nice if you found a way to make it work please share

https://gyazo.com/a5e48277380df14b14cd0775d77a8c50

0 Likes
Message 11 of 19

robotProdigy
Advocate
Advocate

Hey Nazuko,

 

I looked over my instructions, and in step 3, I didn't explicitly state that you should pick mayapy.exe as the interpreter, although it does show it in the image. If that slipped by you, maybe that's all you need to get it to work. What version of Maya are you using? I haven't tried this on Maya 2017 yet, so not sure if there are any caveats.

 

 

 

0 Likes
Message 12 of 19

Anonymous
Not applicable

I'm using maya 2017 and yeah I was just following the image. What would I point it to then? do I install python 2.7 and point it to that? it made sense to me to point it to mayapy.exe like the steps. 

0 Likes
Message 13 of 19

robotProdigy
Advocate
Advocate

Yes, keep it pointed to mayapy.exe

 

I would be taken aback if installing Python 2.7 would somehow make this work, but I've seen stranger things. I'm not sure what the solution is to get yours to work. It's possible stephanosterburg6SYHS had the same problem you are and got passed it, possibly after taking into consideration my response to him.

 

Best of luck.

0 Likes
Message 14 of 19

Anonymous
Not applicable

I got it to partly work and only some stuff works like   import maya.cmds doesnt work but import pymel.core lists correctly and its strange, but 

 

import maya.cmds as cmds  << this line on the maya. << cmds or any maya stuff doesn't pop up, but 

 

cmds.   << all the cmds stuff like sphere and add pop up. no document stuff pop up at all, but at least it's slightly better then before. I'll look into the cmds folder to see what's up with it .

0 Likes
Message 15 of 19

robotProdigy
Advocate
Advocate

Since I have to do this anyway, I spent some time getting my PyCharm auto-completing for Maya 2017.

 

I followed my own instructions, and had some trouble because I didn't remove site-packages from my interpreter paths - although this is mentioned by myself a bit further down the thread. If you haven't done this yet, be sure and remove that path (below).

removesitepackages.png

 

Also, for Maya 2017, those 4 files mentioned in step 1 of the instructions of course are no longer empty. I simply added the content I would have added to the empty 2016 version of those files. For example, for OpenMaya.py, I added from _OpenMaya_py2 import * under the docstring like so

"""
# Copyright 2012 Autodesk, Inc. All rights reserved.
#
# Use of this software is subject to the terms of the Autodesk
# license agreement provided at the time of installation or download,
# or which otherwise accompanies this software in either electronic
# or hard copy form.
"""

from _OpenMaya_py2 import *

class MFloatVector(object):
    bla bla bla...

I can't rule out that adding these wild-card imports won't make something somewhere else unhappy, but at the moment, I can't imagine what.

 

Now my Pycharm is auto-completing for maya.cmds, pymel.core, Python API1, and Python API2.

 

Hope this helps.

0 Likes
Message 16 of 19

Anonymous
Not applicable

I added those lines, but when I type import maya. it doesn't find cmds for the auto complete or any modules. When I write cmds. stuff like sphere come up, but they don't have the documentation. They should really make this easier to do. Thanks for your help btw

0 Likes
Message 17 of 19

Anonymous
Not applicable

I followed you instructions [using Maya 2018] and it was still not working for me. Found some video where the user removed "C:\Program Files\Autodesk\Maya2018\Python\Lib\site-packages" from the "Interpreter Paths" (your #4 step). Once I had done that it worked for me.

 

Thanks for the tip!

0 Likes
Message 18 of 19

g2m.agent
Collaborator
Collaborator

it works! but i found a problem: Qt programs will no longer be able to run.

 

# simple example

 

import sys
from PySide2.QtWidgets import QApplication, QLabel
app = QApplication(sys.argv)
label = QLabel("hello world!")
label.show()
sys.exit(app.exec_())

 

 

# get error

 

    app = QApplication(sys.argv)
TypeError: 'NoneType' object is not callable

 

0 Likes
Message 19 of 19

bradowado
Contributor
Contributor

For Maya 2018, PyCharm 2019.2, these instructions worked as described above. 

1. Added the mayapy.exe interpreter

2. Edited the interpreter paths to remove the site-packages

 

What I found unnecessary were the suggested changes to any of the devkit source files (e.g. OpenMayaUI.py). In your script, just import API 2.0 as described in the docs:

http://help.autodesk.com/view/MAYAUL/2018/ENU//?guid=__files_GUID_B1CD0989_EB49_46BE_934F_F23BFB8691...

 

For OpenMayaUI, you can do something like:

import maya.api.OpenMayaUI as omui

omui.MMaterial.getTextureTransformation()

This gets you autocompletion and the documentation.

 

The downside, is that you lose the ability to go to the source for pymel commands inside site-packages. For example, with this code:

 

import pymel.core as pm

pm.exportSelected()

when you right click on exportSelected() and choose Go To -> Declaration:

 

- With site-packages included, this goes to the source in: Autodesk\Maya2018\Python\Lib\site-packages\pymel\core

def exportSelected(exportPath, **kwargs):
    _setTypeKwargFromExtension(exportPath, kwargs)
    kwargs['exportSelected'] = True
    res = cmds.file(exportPath, **kwargs)
    if res is None:
        res = exportPath
    return Path(_correctPath(res))

- Without site-packages, it goes to the docs in: devkitBase\devkit\other\pymel\extras\completion\py\pymel\core

def exportSelected(exportPath, **kwargs):
    """
    Export the selected items into the specified file. Returns the name of the exported file.                  
    
    Flags:
      - force:
...