Open Maya in Houdini using Python

   2020   5   0
User Avatar
Member
7 posts
Joined: 2月 2017
Offline
Hello smarter people!

To start, I'm not the best python scripter out there, so I'm a bit stuck with a problem.

import subprocess

cmd = "maya.exe -file c:/temp/temp_mb_scene.mb"

subprocess.Popen(cmd)


If I run the code above in "VS Code" or "Windows PowerShell" it works fine. It opens Maya and the Maya scene. But if I try to run the code above in Houdini, Maya starts, but crashes right away. Maya also crashes if I leave out the Maya scene file. Does anyone have a clue how to solve this? Or how to open Maya in Houdini with Python?

Attachments:
maya_crash_report.png (33.6 KB)

User Avatar
Member
48 posts
Joined: 8月 2017
Offline
Hey, I think the issues you might running on is that when you run maya from whitin Houdini, the maya instance will inherit all the Houdini's environments variable, and some of them conflict with maya.
you would need to make a copy of the variable passed to Houdini before it start manipulating them itself and pass that copy to the maya process you want to execute.
I do not know if there is any point in the Houdini lifecycle that lets you execute a python script to make that copy before the env are changed.
worse case scenario you just recreate them from scratch.

you simply need to pass a dictionary to the Popen(env=dict) function

https://docs.python.org/3/library/subprocess.html [docs.python.org]
Edited by SciTheSqrl - 2023年4月19日 18:13:47
User Avatar
Member
7 posts
Joined: 2月 2017
Offline
Thank you mate,

The code below worked! It probably can be written better, but I'm happy it worked!

import subprocess, os

# set the necessary environment variables for Maya
os.environ = "C:/Program Files/Autodesk/Maya2024/"


# set the maya executable + file to open
cmd = "maya.exe -file c:/temp/temp_mb_scene.mb"

# open maya
subprocess.Popen(cmd, env=os.environ)
User Avatar
Member
48 posts
Joined: 8月 2017
Offline
be carefull because by overriding os.environ like you did, you might be breaking houdini environement itself, os.environ is a dictionary of variable, you replacing it by a single string which probably get discarded by Popen(),

if you want an empty environment you can do Popen(cmd, env={})

but by doing so, maya will start up with a completely clean environment (no PATH, module, plugin, or working directory), so it may not work correctly.
User Avatar
Member
7 posts
Joined: 2月 2017
Offline
For some reason it stopped working. And I couldn't get it back to work. I did upgrade to Maya 2024.01, but i doubts that that is the problem. I tried all you tips and some more, but still no luck. So I abandoned the Maya part in the script and only use the Blender part, what doesn't give me any headaches.

Still thank you for your time. And have a great day!
User Avatar
Member
7 posts
Joined: 4月 2023
Offline
Hello this is Gulshan Negi
Well, I searched about it on the Google and I found that if you're experiencing compatibility issues when trying to launch Maya from Houdini using Python, you can try using the Houdini-specific hou module or the subprocess.run() function instead of subprocess.Popen(). Additionally, you may need to ensure that Houdini is set up to recognize the maya.exe command, either by adding it to your system PATH or configuring Houdini to recognize its location.
Thanks
  • Quick Links