Wednesday, July 24, 2013

Using Eclipse as Maya IDE

This post is going to be a little step-by-step guide to setup Ecplipse and use it as python IDE for Maya on windows7 (BTW I'm using  Maya 2013 x64). Well, let's begin!

What we need?


Eclipse Standard - http://www.eclipse.org/downloads/


Maya Editor script - http://www.creativecrash.com/maya/downloads/applications/syntax-scripting/c/eclipse-maya-editor


Setup:


1. Unzip eclipse-standard-kepler-R-win32-x86_64.zip (I downloaded the 64bit version but you can use the 32bit version if you don't have a x64 processor).

2. Unzip org.eclipse.EclipseMayaEditorProject.zip in a temp folder (after the installing we are not gonna need it anymore)


3. Enter in eclipse folder and execute eclipse.exe. Go to Help > Install new software

4. Press Add button and then in Local select the folder where you have unzipped EclipseMayaEditorProject.


5. It will appear as Uncategorized, select it and press Next. Accept license, etc.


Maya editor buttons:

  • Send entire contents to Maya
  • Send highlighted selection to Maya
  • Button menu to open various documentation
  • Reconnect Eclipse to Maya


6. Let's install PyDev! In eclipse go to Window > Preferences


7.  Go to Install/Update > Available Software Sites.. and press Add...

8. Type PyDev on Name and paste the url http://pydev.org/updates


9. Now, go back to Help > Install new software. On "Work with" choose PyDev and choose the 2 items. Press Next.


10. Choose "Keep my installation the same..." and press Next.

11. Select and accept certificates.



12. Configure the python interpreter on Eclipse. Go to Window > Preferences. On PyDev > Interpreter - Python... press "New.." button.

13. Browse and select where mayapy.exe is.


14. Select all the paths you will need and press OK.


15. Server setup to send info from eclipse to Maya. Go to Window > Preferences > Maya Editor Preferences. The default port is 7720.


16. Create a userSetup.py (if we dont have it) and add the next lines. The next time we open Maya will receive data from the port 7720.

import sys
import maya.cmds as cmds

newPath = "D:/+ENGINES/MAYA/Maya-Scripts"
sys.path.append(newPath)

if cmds.commandPort(':7720', q=True) !=1:
    cmds.commandPort(n=':7720', eo = False, nr = True)


17. Create a new Project. New > New > Project... and select PyDev Project


18. Select the Directory project, set Grammar Version to 2.6 and select mayapy.exe as Interpreter.


17. And finally the funny part!! Let's test it. Create a new file on our project folder and write a name (ex: test.py). It's important to add the .py extension.


18. Write some code and send to Maya pressing Ctrl+Enter or the menu button.


19. The result :)


This tutorial is based/inspired on rbublitz work on CreativeCrash:
http://www.creativecrash.com/maya/tutorials/using-tools-scripts/c/using-eclipse-as-a-maya-ide

Wednesday, July 3, 2013

Write and Read a XML in Maya with Python

It's been a while since the last post. I'm not in Andorra anymore... so I have been working on new stuff. In this case I bring to you 2 simple scripts to write and read a xml file in Maya using Python.

Some example uses:

  • Construct our game level on maya and import it to the game engine with the xml file. 
  • Copying animation poses.
  • Copying light settings.
  • ... 


Write the XML file:
from xml.dom.minidom import Document
import maya.cmds as cmds

doc = Document()

root_node = doc.createElement("scene")
doc.appendChild(root_node)

# Selection: 
# grab all selected objects
#selection = cmds.ls(sl=True)

# grab all visible objects, which type is transform
selection = cmds.ls(type="transform", v=True )

for object in selection:
    
    # create object element
    object_node = doc.createElement("object")
    root_node.appendChild(object_node)
    
    # BEWARE: after freeze transformations the translate is a bit tricky
    #object_translation = cmds.getAttr(object + ".translate")[0]
    object_translation = cmds.xform(object, query = True, worldSpace = True, rotatePivot = True) 
    
    # set attributes
    object_node.setAttribute("name", str(object))
    object_node.setAttribute("translateX", str(object_translation[0]))
    object_node.setAttribute("translateY", str(object_translation[1]))
    object_node.setAttribute("translateZ", str(object_translation[2]))
    

xml_file = open("C:/Temp/test.xml", "w")
xml_file.write(doc.toprettyxml())
xml_file.close()

print
print doc.toprettyxml()

Reading the XML file:
from xml.dom.minidom import parse

dom = parse("C:/Temp/test.xml")

 # visit every object node 
for node in dom.getElementsByTagName('object'): 
    
    # method 1: using keys
    attrs = node.attributes.keys()
    for a in attrs:
        pair = node.attributes[a]
        print (str(pair.name) + " = " + str(pair.value))
    print
    
    #method 2: by attribute name
"""        
    print str(node.getAttribute("name"))
    print str(node.getAttribute("translateX"))
    print str(node.getAttribute("translateY"))
    print str(node.getAttribute("translateZ"))
    print
"""

Based and inspired from Luiz Kruel's script