PDA

View Full Version : External Interpreter and maya for all time!


MF3dream
06-08-2011, 07:10 AM
Hello everyone....
I want to ask how you approach writing a python script for maya from
An external editor?
Do you even use maya's internal script editor? and if you write down your
Scripts in an external editor how you connect it to maya?
I've tried editors like Notepad ++ and Eclipse.
In terms of Notepad ++ I should say that I founded a plugin for it that adds
The ability to run the python scripts from the program itself but still without
Connectivity to maya!
And eclipse, it has also a good plugin and it is pyDev, plus ones that you can
Find in www.creativecrash.com (http://www.creativecrash.com) that adds auto completion and connectivity
Function for maya, BUT for using it you have to wait for updates if you
Going to use it for latest version of maya and besides I like NP ++ more!

I asked a question here before about maya and an external interpreter
And some nice guy answered to it like this:

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# fileName : commandPort.py
import socket
#HOST = '192.168.1.122' # The remote host
HOST = '127.0.0.1' # the local host
PORT = 54321 # The same port as used by the server
ADDR=(HOST,PORT)

def SendCommand():
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
command = 'polyCube()' # the commang from external editor to maya

MyMessage = command
client.send(MyMessage)
data = client.recv(1024) #receive the result info
client.close()

print 'The Result is %s'%data

if __name__=='__main__':
SendCommand()

But when I'm replacing the "polyCube()" with a multiline statement
Its not going to work!

I've been seeing the other guys asking the same question here so lets
Turn this topic to a Real Answer to all of this type of questions.

Thanks in advance

Azrail
06-08-2011, 08:17 AM
How do you format your multiline command? Python sockets need each line to be ended with "\n\r" or "\r\n".

EDIT: actually it works with "\n" only, at least on linux and maya 2011
command = 'file -q -sn\nworkspace -q -fn\ndate -f "MM-DD-YYYY hh:mm:sss"'

MF3dream
06-08-2011, 08:42 AM
I missed one point in my initial post and it is the "command" should be in mel
not python because the maya command port initially stated in mel.
Now my code is python and I want to see the result of my Py code in maya not mel.

Azrail
06-08-2011, 09:51 AM
it doesn't matter if the command is py or mel, in either case it is passed to maya and executed correctly. The problem I see is that python commands doesn't seem to return anything than None. Here is the py command I've tested:
command = 'import maya.cmds as mc\nmc.polySphere()\nmc.polyPlane()\nimport os\nmc.warning(os.environ["USER"])'

It works but returns None trough the socket

MF3dream
06-09-2011, 06:46 PM
Originally posted by "Azrail":
it doesn't matter if the command is py or mel, in either case it is passed to maya and executed correctly. The problem I see is that python commands doesn't seem to return anything than None. Here is the py command I've tested:
Code:
command = 'import maya.cmds as mc\nmc.polySphere()\nmc.polyPlane()\nimport os\nmc.warning(os.environ["USER"])'



I can't get it through... can you tell me which version of python you're using?
I tried to send the command with my Notepad++ python plugin which is v2.7.
It can run the mel version but not py command. I copied and paste your piece of code
Exactly but nothing happend (I checked the maya also, nothing was there!)

And can you tell me how I can find maya's python version!

Thanks

cgrebeld
06-10-2011, 06:20 AM
I missed one point in my initial post and it is the "command" should be in mel
not python because the maya command port initially stated in mel.
Now my code is python and I want to see the result of my Py code in maya not mel.

With Maya 2011+, you can start a commandPort which executes python scripts:
http://download.autodesk.com/us/maya/2011help/CommandsPython/commandPort.html#flagsourceType

commandPort -stp "python" -n ":2222"

Azrail
06-10-2011, 10:21 AM
I'm running Maya 2011 and sending the commands trough Komodo-Edit. Both Maya and Komodo are using python 2.6. Also you should make sure you're opening a 'python' port as cgrebeld explained

MF3dream
06-10-2011, 11:33 AM
Thanks for your help "cgrebeld".
I had tried to open a python port but without the "\n"
Character that "Azrail" mentioned.
Now I can send my commands directly from Notepad++ to maya!

So here is sum of the works that must be done for using notepad++
As an external interpreter for maya:

1. Download it from here: http://notepad-plus-plus.org/download
2. Its python plugin: http://npppythonscript.sourceforge.net/ (http://notepad-plus-plus.org/download)
3. Open a python command port for maya like this:
import maya.cmds as cmds cmds.commandPort(stp="python", n=":54321")


4. Use this script as the interface for sending commands to maya:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# fileName : commandPort.py
import socket
#HOST = '192.168.1.122' # The remote host
HOST = '127.0.0.1' # the local host
PORT = 54321 # The same port as used by the server
ADDR=(HOST,PORT)

def SendCommand():
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
command = 'import maya.cmds as mc\nmc.polySphere()\nmc.polyPlane()\nimport os\nmc.warning(os.environ["USER"])' # the commang from external editor to maya

MyMessage = command
client.send(MyMessage)
data = client.recv(1024) #receive the result info
client.close()

print 'The Result is %s'%data

if __name__=='__main__':
SendCommand()
5. Use "\n" character between each commands (little annoying)
6. When you installed the python script for notepad++, open
The console by going to plugin menu, python script, show console and
Type the following code in its run text box:
exec(editor.getText())

If anyone still know a better way to use an external interpreter, I like to hear!

Azrail
06-10-2011, 04:41 PM
in Komodo you can create a shortcut for a given command, so you'll just need to save your file and hit the shortcut to execute the python. Also about the '\n' thing - I don't know what is the method you use to create the commands, but if you have them as an array(list) you can just make a
cmd = '\n'.join(cmdList)

and send it trought the socket...
also on my machine, which is dependand on the network setup I guess, it's better to use "localhost" as a host name, instead of the IP

MF3dream
06-10-2011, 06:44 PM
Much thanks for your help "Azrail", but still I didn't get your idea correctly.
Can you explain it with a piece of code like your previous examples?

zoharl
06-10-2011, 09:03 PM
If anyone still know a better way to use an external interpreter, I like to hear!

You meant external editor, right?

And what about debugging?

Take a look at this page to see what maya recommends:

http://download.autodesk.com/us/maya/2010help/index.html?url=WS73099cc142f48755f2fc9df120970276f7-2158.htm,topicNumber=d0e183276

I actually installed wind ide yesterday, and in the future I'll be able to tell you how it is.

MF3dream
06-11-2011, 07:07 AM
Also have a look at these links:
http://www.wingware.com/doc/howtos/maya (http://download.autodesk.com/us/maya/2010help/index.html?url=WS73099cc142f48755f2fc9df120970276f7-2158.htm,topicNumber=d0e183276)
http://www.bigfatalien.com/?p=233

CGTalk Moderation
06-11-2011, 07:07 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.