Reputation: 6665
Find path to mayapy command (Maya's pre-configured external python interpreter)
$ locate mayapy # result: /usr/autodesk/maya2014-x64/bin/mayapy
Launch the Maya-configured python interpreter in a terminal
$ /usr/autodesk/maya2014-x64/bin/mayapy
Create a sphere
import maya.standalone
maya.standalone.initialize( name='python' )
cmds.sphere( radius=4 )
(this "works" and returns the following result:)
`[u'nurbsSphere1', u'makeNurbSphere1']`
Q: how do I make this sphere show up in Maya (which I have open)?
Upvotes: 2
Views: 1048
Reputation: 6665
Here's how I created a new sphere in the currently open Maya using an external python.
1) open a port in Maya (on the bottom of the screen there's a command line that says "MEL") and type:
commandPort -stp "python" -n ":5055" ;
2) open a python shell in a new window/terminal and run the following python code to create a new sphere in Maya:
import socket
maya = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
maya.connect(("127.0.0.1", 5055))
maya.send("""maya.cmds.polySphere( radius=4 )""")
The above code will create a new sphere in your currently running Maya. You can use any python terminal (doesn't have to be mayapy).
(If you're running python3, the last command will produce an error until you change it to:
maya.send(bytes("""maya.cmds.polySphere( radius=4 )""", 'UTF-8'))
Upvotes: 1
Reputation: 12218
Each maya standalone session is it's own copy of maya - it is not connected to your open instance of maya in any way . It's just like opening two maya sessions in gui mode at the same time.
if you want to connect to Maya from your IDE or outside, you can use the commandPort command in Maya to respond to packets sent over tcp (here's an example using the Wing IDE, here's one using Eclipse). For more [complicated stuff you can use a remote procedure call library like RPyC to remotely interact with the Maya session.
Upvotes: 3