File -q -l not executing in script

File -q -l not executing in script

Anonymous
Not applicable
1,185 Views
8 Replies
Message 1 of 9

File -q -l not executing in script

Anonymous
Not applicable

Hi,

 

The whole script runs successfully but for some reason string $pathName[] = eval("file -q -l"); is not executing when running in the script. When I execute the code in script editor it runs with no error.

 

I'm writing a code to automatically save an mb file into the same folder that the fbx is imported from. Here's the snippet. 

 

string $pathName[] = eval("file -q -l"); 
$pathLength = eval("size $pathName[1]")-4; 
string $fileName = eval("substring $pathName[1] 1 $pathLength"); //exclude the extension from the path
$fileName = $fileName + ".mb";
		
file -rename $fileName;	
file -f -save -options "v=0;" -type "mayaBinary";

print ("File saved at: " + $fileName);

Regards.

0 Likes
Accepted solutions (1)
1,186 Views
8 Replies
Replies (8)
Message 2 of 9

haggi_master
Advocate
Advocate

Do you have an error message? Or can you tell what exactly does not work? In my tests the command exectues without problems. Did you try to print the $pathName[] to check if the array contains anything?

Message 3 of 9

Anonymous
Not applicable

It runs successfully on the first fbx import but when I import second fbx in the new file it saves in the previous location of the first fbx folder.

0 Likes
Message 4 of 9

haggi_master
Advocate
Advocate

Ah, okay. So the command works fine. I suppose the problem is this line:

string $pathName[] = eval("file -q -l"); 
$pathLength = eval("size $pathName[1]")-4; 

You always use the second entry of the $pathName array. I guess if you import fbx, you create some file references which are listed by the file -q -l. If you have a look at the array you will see that the first entry is the name of the current maya scene, the second one is from the first reference the third one if from the last import. Even if it may not be reliable, you can simply try to use the very last element of the list:

 

string $pathName[] = `file -q -l`; 
int $pathLength = size($pathName[size($pathName) - 1])-4; 

This will always give you the last entry.

Message 5 of 9

Anonymous
Not applicable

When the fbx is loaded the first path is C:/Users/Irfan/Documents/maya/projects/default/untitled and the second path is the texture file D:/3ds Max Project Files/scenes/Box/u1650098032/u1650098032.jpg. So in new file the list has 2nd path of the texture file.

 

Now the first file is correctly saved in its respective folder. I updated the code according to your suggestion but it still saves the second file in the first location. When I manually execute string $pathName[] = eval("file -q -l"); then it saves the file in its respective folder.

I've saved this code separately in maya shelf and it works correctly saving each model in its respective folder. But it doesn't update the list when ran in the full script.

 

Regards.

0 Likes
Message 6 of 9

haggi_master
Advocate
Advocate

Can you show the content of the string array after loading the second fbx?

Message 7 of 9

Anonymous
Not applicable

New File->first file is imported

C:/Users/Irfan/Documents/maya/projects/default/untitled
D:/3ds Max Project Files/scenes/Box/u1650098032/u1650098032.jpg
File saved at: D:/3ds Max Project Files/scenes/Box/u1650098032/u1650098032.mb

New File->second file imported

C:/Users/Irfan/Documents/maya/projects/default/untitled
D:/3ds Max Project Files/scenes/Box/u1920092918/u1920092918.jpg
File saved at: D:/3ds Max Project Files/scenes/Box/u1650098032/u1650098032.mb
0 Likes
Message 8 of 9

haggi_master
Advocate
Advocate
Accepted solution

Okay, I think I found the reason. In your script:

string $pathName[] = eval("file -q -l"); 
$pathLength = eval("size $pathName[1]")-4; 
string $fileName = eval("substring $pathName[1] 1 $pathLength"); //exclude the extension from the path
$fileName = $fileName + ".mb";
file -rename $fileName;	
file -f -save -options "v=0;" -type "mayaBinary";
print ("File saved at: " + $fileName);

You use "eval" in different places. As much as I can see, eval() uses global variables instead of local ones. So if you avoid using eval() and use backticks instead, you will get local variables only and the script should work as expected:

string $pathName[] = `file -q -l`; 
int $pathLength = size($pathName[1])-4; 
string $fileName = `substring $pathName[1] 1 $pathLength`; //exclude the extension from the path
$fileName = $fileName + ".mb";
file -rename $fileName;	
file -f -save -options "v=0;" -type "mayaBinary";
print ("File saved at: " + $fileName);
Message 9 of 9

Anonymous
Not applicable

Thank you, really appreciate your help 🙂

0 Likes