View Full Version : Editing the project path with python/MEL

10 October 2011, 09:57 PM
Today I got involved with a collaborative project here on CGTalk. Devils, Angels & Dating.

We use dropbox to share files, everyone has their cloned dropbox in the same directory so that the paths is the same for everyone. (c:/devils_angels_dating/My Dropbox).

Enter me. A mac user. My path is obviously different.

What I want to do is to temporarily substitute the windows path with the osx path. I don't want to change the paths to the referenced files as that may mess things up for everyone else on the project.

My first thoughts were environment variables, then I thought about python and smiled.

import pymel.core as pm
currentPath = pm.workspace.path
print currentPath

Now I would like to determine if the path needs to changed or not. How is this done in maya?

10 October 2011, 10:04 PM
Hi Elias!

If the project is setup correctly, all path should be local. They should all begin in the project folder themselves, rather then going through the whole harddrive.

For ex, the project im working on here, my reference path to the rig is:


Is that helpful?


10 October 2011, 10:55 PM
Hi Martin nice to see you,

No! The paths are not relative to the project root. :/

The easiest solution is to install windows7 via BootCamp, but I would like to avoid it. I'm told that I can't have an OS dependent solution as it would require all users to implement it. If I could create a virtual drive/directory of some sort that allows maya to access files in that folder while they in reality is somewhere else, that would be good. Do you have any experience with this Martin? :)

10 October 2011, 11:21 PM
Now I would like to determine if the path needs to changed or not.

I would venture a guess that if it contains the string "eliaserikssonrydberg" then it is the wrong path. Python is not one of my strongest skill sets, but I know that you can call MEL from python.
string $mypath=`workspace -q -dir`;
if(gmatch($mypath, "eliaserikssonrydberg")) { // wrong space
workspace -e -dir "C:/Devils_Angels_Dating/My Dropbox/";

Now I am not sure that this works on a Mac, because I don't have one. And it may be a little more complex that I cited, because the lighting file is in "C:/Devils_Angels_Dating/My Dropbox/030_Lighting/Master/". So take this as a framework, rather than a finalized solution.

I think you should be able to manage nicely with manual methods, though. You only actually need to save a Maya file for your finished work, so you can change any paths needed in the reference editor (only needed if the unresolved name is changed) prior to saving. A quick copy and a few pastes should take care of that. I would guess you are doing lighting/rendering, since most of the animation is done or assigned, so most of your production will be image files anyway, and they should not cause any path issues.

<* Wes *>

p.s. If you need any further special help, send me a message at the site. Or to iskander, he is good at scripting.

10 October 2011, 11:28 PM
You are correct, if the string contains my name it would be the mac path. And yes, I could connect the references manually. I've already done this just to try it out, the drawback is that if someone else opens my scenes after I'm done. They will have to set the references back the original settings again. Not really production proof.

I'm leaning towards having my computer pretend it's a windows machine (at least for this specific directory) and leave the paths as they are. Could this be done?

I would like to have the following directory "C:\Documents_and_settings\Angels_Devils_Dating\My Dropbox" in my root and have "My Dropbox" sync through a symbolic link if thats possible.

Edit: nevermind that, I can't create folders with the name "C:". The best plan so far is Win7 via Bootcamp.

10 October 2011, 12:52 AM
Well, what I have been trying to say is that if you can open the files from Dropbox, then somewhere in Dropbox or Maya is some code that already switched the path names. For lighting, you would just need to save the edited lighting file with the unresolved name field still pointing to C:/Devils_Angels_Dating/My Dropbox. Maya on Windows should then be able to find all the files.

Oh, and I do not use any "Project" for this. My current project folder for Maya is on "C:/Users/WesHowe/Documents/", but I just use File->Open Scene... and navigate to the dropbox. I save temp stuff on my regular project folder, the one that the "workspace -q -rd;" command returns. I only save to the Dropbox folder when I have something to upload.

Because I don't have a Mac, I cannot see what the Reference Editor says on your machine. But I would be happy to test a file on a Windows 7 machine with Maya 2011 for you, if you want to see if your solutions work. I wrote the lightingSetup.mel script used on the project, so I am all set up here.

<* Wes *>

10 October 2011, 01:42 AM
Yeah Elias, I just found out that the references indeed cant be relative for some reason. MAYA DOC (,topicNumber=d0e125457).
How is your references path looking right now in the editor?

You can point to a string from MEL(or python i guess) with your disired filepath. Maybe this could be determed on startup what machine is in use with the about tag in MEL.

So you use this tag in the reference:

And execute this lines of code in MEL:
string $checkOs = `about -os`;
string $myProject;
//Declare mac
if ($checkOs == "mac") {
$myProject = "/Users/eliaserikssonrydberg/Dropbox"; }
//Check windows
else if ($checkOs == "windows") {
$myProject = "C:/Devils_Angels_Dating/My Dropbox/"; }

print $myProject;

Then it should be work on both a windows and mac machine, right!?
I dont have any windows to test this out myself.

10 October 2011, 08:03 PM
Wesley, are you saying that I should edit the reference paths while working on it and then edit the back when uploading to Dropbox? I'm sure this can be done, but it's a lot of manual labour. :)

Thanks Martin, that's what I need to figure out what path to use based on what OS I'm running. What I was asking for in the original post.

This could be going somewhere! With an expression the script could be executed when the scene is opened and closed. If OS is mac, change "C:\Docum..." to "Users/el...". If not, do nothing. When scene is saved change all paths to C:\Docum..." again.

10 October 2011, 08:18 PM
Yeah, that should work. Although then you cant save the scene when you are working in it. Is it not easier for you then to just have two script buttons in your shelf, on you execute when you open the scene and one when you save and close it.

10 October 2011, 08:42 PM
That would work too. Could do that for now.

If the paths are edited every open/close it would be completely transparent to the artist. Allowing work to be done on whatever OS is most suitable for the task at hand. And yes, it seems like expressions can be run while maya is shutting down, before the scene is closed an expression executes a MEL procedure (before saving I would hope!). :D

10 October 2011, 10:25 PM
Wesley, are you saying that I should edit the reference paths while working on it and then edit the back when uploading to Dropbox? I'm sure this can be done, but it's a lot of manual labour. :)

While the count varies based on how many assets are used in each scene, I counted a total of 13 references in scene 0400. That's one type, one copy and 12 pastes. That would not take me very long to do... for the few times you would be uploading files, it is less work than writing and testing a script.

But choosing whether you want to stick with OSX or use parallels is your choice, all I was trying to do was to put this in some perspective.

<* Wes *>

10 October 2011, 11:15 PM
Yeah, you are correct, manual labour is sometimes faster than scripting.

The quickest way is to open the Light master in a text editor and replacing the windwos path with the OSX path. I would have to do this for the master and the shot scene to have all (?) references to work properly.

10 October 2011, 12:00 AM
string $checkOs = `about -os`;
string $user = `getenv "USER"`;
string $myProject;
string $windowsPath = "C:/Devils_Angels_Dating/My Dropbox/";
//OSX path
if ($checkOs == "mac")
$myProject = "/Users/"+$user+"/Dropbox";
//Windows path
else if ($checkOs == "windows")
$myProject = $windowsPath;

print ("My Path:" + $myProject + "\n");

string $referenceSelection[] = `ls -references`;
print ("\n" + "References:" + "\n");
print $referenceSelection;

for ($each in $referenceSelection)
string $referenceDir = `referenceQuery -filename $each`;
print ("\n"+$referenceDir);

python( "path = maya.mel.eval('referenceQuery -filename $each')" );
python( "path=path.split('/',4)" );
string $python = `python( "path[4]" )`;
string $myProject = ($windowsPath + $python);
print ( "\n" + $myProject + "\n");

Did some quick scripting. If I don't get my hands on a W7 tomorrow I will continue development.

Please note how the script utilizes the user environment variable, proud of it! :D

10 October 2011, 04:49 AM
Pretty good work on researching the getenv command, making your script more portable, or usable with different user accounts there.

I'm not sure that you can do this project of yours in a single script, if your objective is to just run on a Mac instead of Windows, because the script should always detect the OS as "mac". You may need to have the "To Mac" and "To Windows" just broken into two different scripts.

But maybe I misunderstood your plan, and you are going to use Maya on Windows to get your files, convert them to use on your Mac OS, and convert back to Windows before uploading. It should work for that.

<* Wes *>

10 October 2011, 08:51 AM
At this point, my goal is to write two scripts to change the paths from OSX->Windows and vice versa. I could have these on my shelf and use before saving and uploading.

If I can I want to run a Windows->OSX script while the scene is opening, so that the references are correct for my OS and won't give an error like:
"the references was not found. (Browse/Skip/Abort file read)"

The alternative is to open the file with "Selective preload" checked, unload all references, run script and load them. Preferably this would be done automatically. But maybe I should split it into several scripts.

My plan was to never leave OSX unless necessary. If I get my hands on a W7 disk I can run it through bootcamp and won't have to edit the paths at all.

CGTalk Moderation
10 October 2011, 08:51 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.