domingo, 30 de noviembre de 2014

My first Maya Python/PyQt plugin

THE PLUGIN

What i'm gonna show in the next video is the first plugin for Maya i've coded. I wanted to do something with GUI so i forced myself to use PyQt and the QTDesigner to get familiar with it. The idea of this plugin came up during one of the sessions of the Máster in audiovisual production with Autodesk Maya that i'm following at CICE here in Madrid.
I recalled that 3ds max has a very complete tool for pivot alignment and orientation. One can do the practically the same with Maya but the local axis alignment. This comes handy when we cannot do a snap to the grid or geometric vertex to position the object's pivot.

Modifying the object's pivot position is a very usual operation when we find ourselves modeling stuff. It allows for example to rotate an object's duplicate around a geometric center that can be found through the bounding box of the proper object itself.

ABOUT MY INTEREST IN MAYA TOOLS DEVELOPMENT

The course is mainly focused on 3d concepts such as NURBS & polygon modeling, lighting, texturing, rendering, dynamics etc, all from a Maya artist point of view so no coding at all taught. I felt very disappointed when the other Master i was about to do was cancelled, this one oriented exclusively to Maya tools development and API understanding. The reason was that there wasnt people enough interested in the course. Truth is we were at first 10 people (enough for it) but 5 of them were being sent by the same company as an education at work thing and this company backed off at the last moment. So there we were only 5 people, which the school though was not a proper amount to dedicate a room during five and a half months, in other words, it wasn't profitable.
'Okay, then i'll try in April 2015 while im working' (the schedule allowed me to balance it with work) but then new surprise "the teacher is leaving to work for MPC in Canada, the course will not be released anymore".

Well then find another teacher!!! Apparently this is such a "high profile" that there is little demand for this kind of education (usually 3d artist dont wanna know anything about coding) and at the same time it's difficult to find a teacher. Is this caused by the fact that there are few Technical Directors or Tech Artists out there? is it a "niche" job?

All this said, that's why im trying to make it up and start learning tools development on my own since there is no place else where to study this here in Spain. It will require more effort and the steps will be smaller but with help from other technical directors in a facebook group and mainly my will power  (yes! i can !) i hope to get enough understanding and experience to land a job as CG Generalist/TD.

Ohhhh excuse me, this post is about the plugin. I 'll let the video do the talking!!




lunes, 24 de noviembre de 2014

Adding a Custom Script Folder To Be Loaded On Maya Startup

Now that we have our IDE setup there is one more thing to do: 
we need to tell Maya where to look for our custom scripts.
The method i ve used is basically to create a "userSetup.py" file in my "My Documents\maya\2015-x64\scripts" which was previously empty.

Anything written python wise in that file will run on Maya startup, so i decided to add a path to the python system path as explained hereafter.

The following has been taken from 


Initializing the Maya Environment in and for Python

Maya runs any Python commands in the userSetup.py file whenever it starts up. You can use this file to set up your working environment or execute commonly used Python commands such as importing the maya.cmds module.
The userSetup.py script is executed during the initialization and setup phase of Maya; therefore, only commands which set up your working environment and have no dependencies on Maya functionality can be successfully run in this script.
Note:
You can use maya.utils.executeDeferred() to delay code execution until after the Maya scene is initialized. For more information, see maya.utils.
  1. Create a file named userSetup.py in the following folder:
    • Windows: <drive>:\Documents and Settings\<username>\My Documents\maya\<Version>\scripts
    • Mac OS X: ~/Library/Preferences/Autodesk/maya/<version>/scripts
    • Linux: ~/maya/<version>/scripts
  2. In the userSetup.py file, type the commands you want Maya to run on start up; for example, import maya.cmds as mc.
    Note:
    Make sure you save the file with the right extension (.py).

Adding items to your Python path

To add items to your path in Python, do one of the following:
  1. Set PYTHONPATH in your Maya.env file, or in your environment before you run Maya
  2. Append to sys.path in your userSetup.py or other script once Maya is running.
Here is an example of appending sys.path
import sys sys.path.append( '/Users/jdoe/maya/Scripts' )
Note: userSetup.py must be located in the initial python path ( sys.path) in order to be run automatically at startup. The default Python path can be added to by setting the environment variable PYTHONPATH.

MEL and Python importing

If you have a MEL script in your path, you do not need to source it before accessing any single global procedure it contains with the same name. However, Python requires you to import a script explicitly before accessing any classes or functions it contains; for example:
# This will access the function "bar" in the file "foo.py"
import foo
foo.bar()

Visual Studio Setup for Maya Python and Qt4 programming

I'm following a course on 3d using Maya and having worked as a programmer for a couple of years i want to learn to code some python tools for Maya as well as some C++ plug-ins. So the first thing to do is to choose the development environment.

I ve heard in some places that people tend to use SublimeText and that it's really easy to configure for Maya python programming. Well the truth is i'm rather used to VS since i ve programmed in C++ and C# doing some things on my own. Plus, lately i ve been programming some stuff in python and pygame and i used VS with the python tools so this is my selected IDE.

Now i will explain how to setup Visual Studio 2013 for Maya Python programming.

VISUAL STUDIO 2013 SETUP FOR MAYA TOOLS PROGRAMMING IN PYTHON

1. Download and install Python Tools for Visual Studio (PTVS).You can get them here.

2. Download and install PyQt4 for python 2.7 (since Maya 2015 is using python version 2.7.3). You can get it here. You need to install PyQt4 in the following path:

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

3. Open VS 2013 and create a Python Application project.
Under project properties->debug you need to add the following folders to the search path:

C:\Program Files\Autodesk\Maya2015\devkit\other\pymel\extras\completion\py
C:\Program Files\Autodesk\Maya2015\Python\Lib\site-packages\maya
C:\Program Files\Autodesk\Maya2015\Python\Lib\site-packages\pymel
C:\Program Files\Autodesk\Maya2015\Python\Lib\site-packages\PySide
C:\Program Files\Autodesk\Maya2015\Python\Lib\site-packages\PyQt4

Now we have autocompletion for maya.cmds, pyqt4 etc!!

Under "interpreter path" add the line:

C:\Program Files\Autodesk\Maya2015\bin\mayapy.exe

You can have the mayapy.exe interpreter if you want or the one in python27 folder if you prefer. We ve made sure both python version are the same 2.7

4. Now we will add the possibility to execute code from VS directly into Maya.
Download the fast script execution (toMaya) by Josbalcaen here.

- once installed, restart VS 2013
- go to Tools->Options->Keyboard
- search for "ToMaya" and create a new shortcut for example ALT+SPACE

5. write the following code in the Maya script editor

1
2
3
4

import maya.cmds as cmds
try: cmds.commandPort(name="127.0.0.1:6000", close=True, echoOutput=True)
except:    pass
cmds.commandPort(name="127.0.0.1:6000", echoOutput=True)

and put it in a custom shelf or configure your userSetup.mel file to execute the script on Maya startup.

Click on the script, now Maya is listening to this port which is the one ToMaya uses for sending the scripts via VS.

That's all, we should have maya.cmds, PyQt and Python auto completion along with direct execution in Maya!!.

If you prefer, i ve gathered all the necessary stuff needed and put it here. (except for the PTVS, sorry!!)