<type>how-to</type>
<domain>install</domain>
<id>maya</id>
<title>Using Wing with Autodesk Maya</title>
<date>$Date: 2006-05-26 17:43:42 -0400 (Fri, 26 May 2006) $</date>
<product>per+</product>
<summary>Tips for using Wing with Python code running under Autodesk Maya, a commercial 3D modeling application.</summary>

.. image:: /images/screenshots/wing7-screenshot-dark.png
  :alt: Wing Screen Shot
  :width: 574
  :height: 423
  :popup: 1

`Wing <https://wingware.com>`__ is a Python IDE that can be used to develop, test, and
debug Python code written for `Autodesk Maya <http://usa.autodesk.com/maya/>`__, a
commercial 3D modeling application. 

Two versions of Wing are appropriate for use with this document: Wing Pro is the
full-featured Python IDE for professional developers, and Wing Personal is a free
alternative with reduced feature set.

If you do not already have Wing installed, `download it now <https://wingware.com/downloads>`__.

This document describes how to configure Wing for Maya. To get started using Wing as your
Python IDE, please refer to the tutorial in Wing's ``Help`` menu or read the `Quickstart
Guide <ref:howtos/quickstart>`__.

Debugging Setup
---------------

When debugging Python code running under ``Maya``, the debug process is initiated from
outside of Wing, and must connect to the IDE. This is done with ``wingdbstub`` according
to the detailed instructions in the `Debugging Externally Launched Code
<ref:debug/debugging-externally-launched-code>`__ section of the manual. In summary, you
will need to:

1) Copy ``wingdbstub.py`` from your Wing installation into a directory that will be on the
   ``sys.path`` when Python code is run by Maya. You may need to inspect that (after
   ``import sys``) first from Maya, or you can add to the path with ``sys.path.append()``
   before importing ``wingdbstub``.

2) Because of how ``Maya`` sets up the Python interpreter, be sure to set ``kEmbedded=1``
   in your copy of ``wingdbstub.py``

3) In your code, where you initiate debugging, use the debugger API to ensure the debugger
   is connected to the IDE before any other code executes, as follows::

    import wingdbstub
    wingdbstub.Ensure()

4) In some cases you may need to edit ``wingdbstub.py`` to set ``WINGHOME`` to point to
   the directory where Wing is installed. This is usually set up automatically by Wing's
   installer, but won't be if you are using the ``.zip`` installation of Wing. Note that if
   you edit ``wingdbstub.py`` after Maya has already imported it then you will need to
   restart Maya to get it to import the modified ``wingdbstub``.

5) Then click on the bug icon in lower left of Wing's window and make sure that ``Accept
   Debug Connections`` is checked.

At this point, you should be able to reach breakpoints by causing the scripts to be
invoked from Maya. In Maya 2018 at least, running a script does not set up the file name
in the compiled Python code correctly, so breakpoints only work in modules that are
imported into your top-level script. Breakpoints in the main script may work in older Maya
versions.

Once debugging starts, when a breakpoint or exception is reached, Wing should come to the
front and show the place where the debugger stopped. Although the code is running inside
Maya, editing and debugging happens inside Wing.

Using Maya's Python in Wing
---------------------------

You can use the ``mayapy`` executable found in the ``Maya`` application directory to run
Wing's ``Python Shell`` tool and to debug standalone Python scripts. 

To do this, enter the full path of the ``mayapy`` file (``mayapy.exe`` on Windows) in
`Python Executable <script:view_project_properties#highlighted_attrib="proj.pyexec">`__.
in ``Project Properties``, accessed from the ``Project`` menu.

Better Static Auto-completion
-----------------------------

Setting ``Python Executable`` in Wing's ``Project Properties``, as described
above, is also needed to obtain auto-completion for Maya's Python API.

At least in some versions of Maya, Wing cannot statically analyze the files in the Python
API without some additional configuration. As a result, it will fail to offer
auto-completion for the API. The solution to this depends on the version of Maya.

Maya 2018
.........

Maya 2018 ships with .pi files in the ``devkit/pymel/extras/completion/pi`` 
subdirectory of the Maya 2018 install directory.  This can be added to the
Source Analysis > Advanced > `Interface File Path 
<script:show_preferences_gui#prefname="pysource.interfaces-path">`__ 
preference in Wing.

Maya 2016
.........

Maya 2016 is missing necessary developer files so you will need to download and install
the `Maya 2016 devkit
<https://apps.autodesk.com/MAYA/en/Detail/Index?id=6303159649350432165&appLang=en&os=Win64>`__
which should create ``devkit\other\pymel\extras\completion\py\maya\api`` in your Maya
installation. This can then be used by making the following edits:

* In "OpenMaya.py" add ``from _OpenMaya_py2 import *``
* In "OpenMayaAnim.py" add ``from _OpenMayaAnim_py2 import *``
* In "OpenMayaRender.py" add ``from _OpenMayaRender_py2 import *``
* In "OpenMayaUI.py" add ``from _OpenMayaUI_py2 import *``
  
This method is based on `this forum post
<https://forums.autodesk.com/t5/maya-programming/maya-python-api-2-0-ide-code-complete/td-p/6239681>`__.

Instead of editing files in the Maya installation, it is also be possible to add ``.pi``
files with the added source. For example, placing ``OpenMaya.pi`` with contents ``from
_OpenMaya_py2 import *`` in the same directory as ``OpenMaya.py`` causes Wing to merge the
analysis of the ``*.pi`` file with what is found in the ``*.py`` file.

Alternatively, place these files in another directory that is added to the `Source
Analysis > Advanced > Interface File Path
<script:show_preferences_gui#prefname="pysource.interfaces-path">`__ preference in Wing.

You will also want to set the ``Python Executable`` in Wing's ``Project Properties`` to
the full path to ``mayapy.exe`` so that the API is on the Python Path and you are using
the correct version of Python.

Maya 2011+
..........

Maya 2011+ before 2016 also shipped with .pi files that can be used as described for Maya
2018 above.

Older Versions
..............

For older Maya versions, .pi files from the PyMEL distribution at
`http://code.google.com/p/pymel/ <http://code.google.com/p/pymel/>`__ may be used. Just
unpack the distribution and add ``extras/completion/pi`` to the `Source Analysis >
Advanced > Interface File Path
<script:show_preferences_gui#prefname="pysource.interfaces-path">`__ preference in Wing.

Additional Information
----------------------

Some additional information about using Wing with Maya can be found in the `mel wiki
<http://mayamel.tiddlyspot.com/>`__ under the ``wing`` tag.

See also the section ``Using Wing with Maya`` in `Autodesk Maya Online Help: Tips and
tricks for scripters new to Python
<http://download.autodesk.com/us/maya/2010help/index.html?url=WS73099cc142f48755f2fc9df120970276f7-2158.htm,topicNumber=d0e183276>`__.

Related Documents
-----------------

For more information see:

* `Autodesk Maya <http://usa.autodesk.com/maya/>`__ website

* `Quickstart Guide <ref:howtos/quickstart>`__ contains additional basic information
  about getting started with Wing.
  
* `Tutorial <ref:intro/tutorial>`__ provides a gentler introduction to Wing's features.

* `Wing Reference Manual <ref:manual>`__ documents Wing in detail.
