简体   繁体   中英

How do I send commands from PHP script to Terminal running a Python interpreter?

I want to access the Terminal from a PHP script to open a standalone version of Maya and then run a Python script in Maya from the Terminal.

I've been able to send commands to the Terminal from my PHP script using the exec() function. For example, I've been able to open Maya with an .OBJ file using the following code.

//Open Maya with .OBJ
$cmd='open -a /Applications/Autodesk/maya2015/maya.app /Applications/AMPPS/www/webGL/upload/Character.obj';
exec($cmd);

I then found that I could open a Maya Python interpreter in the Terminal using this directory: /Applications/Autodesk/maya2016/Maya.app/Contents/bin/mayapy

When this directory is executed in the Terminal, it runs the Maya Python interpreter. When the interpreter is running it seems that I am no longer able to send commands (which is now Python code) to the Terminal from my PHP script.

Am I going down the right path with using the exec() function to carry out what I want to do? Or does this function simply execute single commands and not multiple commands consecutively in the same Terminal? Do I need to use a different function that sends commands to a specific shell so that all the commands are being executed in the same Terminal?

Here is the code that I have been trying to send to the Terminal to run the Maya Python interpreter and then import the Maya standalone library and initialise it.

//Open Maya interpreter
$cmd2="/Applications/Autodesk/maya2016/Maya.app/Contents/bin/mayapy";
//Import standalone and initialise
$cmd3="import maya.standalone; maya.standalone.initialize( name='python' )"

//Execute commands
shell_exec($cmd2);
shell_exec($cmd3);

You don't need to code separate with mayapy, you can run script directly with mayapy

test.py

import os
import maya.standalone
maya.standalone.initialize()
import maya.cmds as cmds
print dir(cmds)

You can simply call this script like this

mayapy /your/path/test.py

In your case

$cmd2="/Applications/Autodesk/maya2016/Maya.app/Contents/bin/mayapy /your/path/test.py";
shell_exec($cmd2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM