# MayaPY not working

When running the following code in a mayapy session

import maya.standalone
maya.standalone.initialize(name='python')


The interpreter never returns for the next line of code.

## TL;DR

was an issue with my userSetup.py where I use cmds.commandPort I guess before maya is ready. I’m guessing this is actually a bug where standalone python reads this before it is ready.

The moral of the story, check the userSetup.py file first when dealing with maya :-)

## Investigation

The following code is working on my mac and when I was testing a script under linux I discovered that it just stopped and never did anything else. Initially I put some print statements in the script but discovered that they were never getting to this point and this lead me to the small snippet of code above.

First test was to use a different user account. In the lab we have a local account we can use for testing with little to no environment setup so I used the following script to test if mayapy was working on the machine using this account.

 /opt/autodesk/maya/bin/mayapy -c "import maya.standalone;maya.standalone.initialize(name='python');maya.cmds.file( f=True, new=True );maya.standalone.uninitialize()"


This worked which means the issue is with my lab setup / user account somewhere, there are several approaches to this, either clean all my environment and start again or add my environment to the vince account.

We can generate a clean account for testing by using the following

env -i bash


Once this was done everything was working fine, so the issue was something with my environment, not to find out what setting was doing it.

LC_PAPER=en_GB.UTF8
MANPATH=/opt/rh/llvm-toolset-7/root/usr/share/man:/opt/rh/devtoolset-9/root/usr/share/man:
RMANTREE=/opt/pixar/RenderManProServer-24.1
XDG_SESSION_ID=5537
LC_MONETARY=en_GB.UTF8
HOSTNAME=w11901
VRAY_TOOLS_MAYA2019=/usr/ChaosGroup/V-Ray/Maya2019-x64/bin
CMAKE_MODULE_PATH=/opt/qt/5.15.2/gcc_64/lib/cmake
TERM=screen-256color
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=10.200.10.106 55523 22
LC_NUMERIC=en_GB.UTF8
X_SCLS=llvm-toolset-7 devtoolset-9
CMAKE_TOOLCHAIN_FILE=/public/devel/2021/vcpkg/scripts/buildsystems/vcpkg.cmake
SSH_TTY=/dev/pts/0
QT_GRAPHICSSYSTEM_CHECKED=1
PCP_DIR=/opt/rh/devtoolset-9/root
USER=jmacey
LC_TELEPHONE=en_GB.UTF8
LD_LIBRARY_PATH=/opt/rh/llvm-toolset-7/root/usr/lib64:/opt/rh/devtoolset-9/root/usr/lib64:/opt/rh/devtoolset-9/root/usr/lib:/opt/rh/devtoolset-9/root/usr/lib64/dyninst:/opt/rh/devtoolset-9/root/usr/lib/dyninst:/opt/rh/devtoolset-9/root/usr/lib64:/opt/rh/devtoolset-9/root/usr/lib
MAIL=/var/spool/mail/jmacey
PATH=/home/jmacey/.pyenv/shims:/opt/rh/llvm-toolset-7/root/usr/bin:/opt/rh/llvm-toolset-7/root/usr/sbin:/opt/rh/devtoolset-9/root/usr/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/public/bin/2021:/opt/qt/5.15.2/gcc_64/bin:/opt/qt/Tools/QtCreator/bin:/opt/pixar/RenderManProServer-24.1/bin:/opt/pixar/Tractor-2.4/bin:/home/jmacey/scripts:/home/jmacey/.pyenv/bin
LC_MESSAGES=en_GB.UTF8
LC_IDENTIFICATION=en_GB.UTF8
LC_COLLATE=en_GB.UTF8
PWD=/home/jmacey
LANG=en_GB.UTF-8
LC_MEASUREMENT=en_GB.UTF8
VRAY_SEND_FEEDBACK=0
PS1=[$\e[33m$\u$\e[m$@$\e[31m$\h:\W$$\e[m$] HISTCONTROL=ignoredups PS2=[$\e[33m$\u$\e[m$@$\e[31m$\h:\W$$\e[m$]
VRAY_PATH=:/opt/autodesk/maya2019/vray/bin
SHLVL=1
HOME=/home/jmacey
PYTHONPATH=/opt/rh/llvm-toolset-7/root/usr/lib/python2.7/site-packages:/opt/pixar/RenderManProServer-24.1/bin:/home/jmacey/NGL/lib:/opt/pixar/RenderManProServer-24.1/bin/pythonbindings
LOGNAME=jmacey
CVS_RSH=ssh
LC_CTYPE=en_GB.UTF8
XDG_DATA_DIRS=/home/jmacey/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share:/usr/share
SSH_CONNECTION=10.200.10.106 55523 10.121.2.251 22
LESSOPEN=||/usr/bin/lesspipe.sh %s
PKG_CONFIG_PATH=/opt/rh/llvm-toolset-7/root/usr/lib64/pkgconfig:/opt/rh/devtoolset-9/root/usr/lib64/pkgconfig:/public/devel/2021/vcpkg/installed/x64-linux/lib/pkgconfig
INFOPATH=/opt/rh/devtoolset-9/root/usr/share/info
CMAKE_PREFIX_PATH=:/home/jmacey/NGL:/opt/qt/5.15.2/gcc_64/lib/cmake/
XDG_RUNTIME_DIR=/run/user/12307
LC_TIME=en_GB.UTF8
CUDA_CACHE_MAXSIZE=268435456
LC_NAME=en_GB.UTF8
_=/usr/bin/env



The environment from the env -i bash call is much smaller

PATH=/home/jmacey/.pyenv/shims:/usr/local/bin:/usr/bin
PWD=/home/jmacey
PS1=[$\e[33m$\u$\e[m$@$\e[31m$\h:\W$$\e[m$] PS2=[$\e[33m$\u$\e[m$@$\e[31m$\h:\W$$\e[m$]
SHLVL=1
PYTHONPATH=:/home/jmacey/NGL/lib
_=/usr/bin/env


After going through a lot of different variables I discovered that unset HOME made it work! (Proper WTF moment).

## Serisouly WTAF?

Ok this is weird, by default

echo \$HOME
/home/jmacey


which is what I expect, however repeating this with env -i bash returns an empty string (as a clean environment).

Doing

export HOME=/tmp
/opt/autodesk/maya/bin/mayapy -c "import maya.standalone;maya.standalone.initialize(name='python');maya.cmds.file( f=True, new=True );maya.standalone.uninitialize()"


Works fine, so the problem is to do with my home directory. Lets try another dir /transfer. This works ok too. I’m getting confused.

Using chmod to change folder permissions has no difference so it’s not that. This has something to do with some files or config in the root of my home directory.

## A solution

Something in my home directory, let’s copy my maya setup to this new tmp dir and see what happens.

export HOME=/tmp
cp -r ~/maya /tmp


This now breaks what was working, so it is something in the maya setup, infact it was in my userSetup.py file

I have the following in my userSetup.py to get the VSCode editor to work (which doesn’t work very well)

melPort=':7004'
pythonPort=':7005'

if cmds.commandPort(melPort ,q=True) :
print("closing mel port")
cmds.commandPort(melPort,cl=True)
cmds.commandPort(name=melPort,echoOutput=True,sourceType='mel')

if cmds.commandPort(pythonPort ,q=True) :
print("closing python port")
cmds.commandPort(pythonPort,cl=True)
cmds.commandPort(name=pythonPort,echoOutput=True, sourceType='python')


removing this and all works. I’m guessing this gets run before maya is ready and it wait for the port and freezes. The easiest way to overcome this is to put this code in the userSetup.mel file which I have now done.

## Final thoughts

I’m guessing it makes sense to use userSetup.mel for most things in maya (I would also guess evalDeffered is best) to not have issues when using standalone maya. Not really sure if this is user error or a bug in maya.standalone as there is little documentation it’s hard to tell. It did make another good example for debugging.

Previous