This is a function for executing Maya python script from commandline or editor like Visual Studio Code.
Python 2.7 Maya2018
Windows: \My Documents\maya<version>\scripts\userSetup.py Mac OS X: ~/Library/Preferences/Autodesk/maya//scripts/userSetup.py Linux: ~/maya//scripts/userSetup.py
userSetup.py
open port
import maya.cmds as cmds
cmds.commandPort(name="127.0.0.1:7002", stp="python", echoOutput=True)
fix encoding bug
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
python SendToMaya.py --file FILENAME
execute active python file with command in Visual Studio Code tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "SendToMaya",
"type": "shell",
"command": "Python",
"args": [<SendToMaya.py full path>,"--file","${file}"]
}
]
}
keybindings.json (ctrl+alt+m)
[
{
"key": "ctrl+alt+m",
"command": "workbench.action.tasks.runTask",
"args":"SendToMaya"
}
]