Hey guys, I'm curious if anyone has experienced this problem I'm having right now. I'm really novice to scripting, I'm hoping there's something really obvious that I'm missing. I'll try to start by explaining the new problem I'm having, and then the original thought process for my solution.
The new problem:
The script I made is behaving differently in 2 scenarios: When i use it on a file that's been opened via "file open" it behaves as intended. However, when I run it on a file that's been opened using "file > recent files" it doesn't work.
The original problem, solution & thought process:
I have many folders. Each folder contains a maya scene in a source folder and textures in an export folder. Since Maya stores texture paths as absolute strings, an asset wont display the texture until the path is fixed manually. My solution is to convert the path name to a relative value. This happens to work because most of the assets share the same hierarchy structure and naming conventions.
The main concern is there's a pretty big workflow limitation if I can't use this script on files opened via the recent menu.
<pre class="CodeBlock"><code>import maya.cmds as cmds
fileList = []
fileList = cmds.ls(type = 'file')
localDir = '..\Export\\' # local texture path
for f in fileList:
fPath = cmds.getAttr('%s.fileTextureName' %f) # full file path of texture
fileName = fPath.split('/') [-1] # file name with path stripped
newPath = os.path.join(localDir, fileName) # injecting local path into the new path name.
cmds.setAttr('%s.fileTextureName' %f,newPath, type = 'string') # set attributes for texture files
print newPath
Replies
I'm getting hung up on this part foremost; if the file node has an absolute path, why wouldn't it display? (well on your computer, at least.) Totally get the need for relative paths, I'm guessing that os.path isn't getting something from maya for some reason when a Recent File is opened? Could troubleshoot it by adding some print lines to make sure that your path variables are working as expected.
Alternatively, I don't think you need os.path for this, try this instead?
So what happens when it fails though, do you get an error or what?
And just to double check, your folders look something like this, right?
x:\blah\blah\Scene1\scene
x:\blah\blah\Scene1\export
x:\blah\blah\Scene2\scene
x:\blah\blah\Scene2\export
edit: or is not working = texture not showing? If so, what happens if you save and reload that scene?
Yes the folder structure you listed is close, here's a link to some files that imitate my current setup:
https://drive.google.com/open?id=157YozPgMuK0qsgragKijY59qjDmZNZS7
Yes, the textures were not showing in my case. I just tried to save the "not working" scene and reloaded it, and sure enough the textures showed up fine. Would you happen to know if there a way to around saving and reloading the scene?
This whole time I've been 99% sure that any paths within a maya project are stored relative, even if they're not in their designated place, such as storing all textures under a common folder. Sheepishly, I never thought to ask if you had a project set and blindly assumed. My bad!
So I downloaded your files and created a project with default settings, where I have c:\temp\New_Project\Assets\AssetA\Source\
Your modified script works for me when using a Recent file. But what I actually get is this result, with no complaint and the textures showed immediately.
So with that said, do you have a project set for this ...project and do your files all live here?
Yeah, it would be great to just shake Maya awake! I added a line for reloading textures and it's not really doing the trick yet. (shared hacky code at the bottom)
You're also correct about setting the project, I'm was purposefully trying to avoid that. Hopefully that's not making things even more difficult? But it's also not working for me like you've mentioned so maybe I'm not doing something correct.
I've found 2 sequences that if followed give me the broken results I'm talking about.
Sequence 1: File > Open> AssetA, File > Open > AssetB, File > Open AssetC. (Don't save or run script for these actions)
Sequence 2: File > Recent File > AssetA, File > Recent File > AssetB, File > Recent File > AssetC. (Run the script for each of these actions)
From my tests, only AssetC will be working with the script as intended, which is the most recently opened file from the first method...
@Panupat
I've added the changes you suggested. They don't seem to fix the current issue, but it's definitely a cleaner solution that should avoid problems later on. Thanks!
At this point, I don't have any further insights on the Recent Files thing, sorry.
I pulled the texture reloading out of the for loop, definitely not my intention. I also applied some of your other suggestions to the best of my ability, I'm definitely not at a level to write well optimized scripts.....but I'm happy to say that after some more stumbling around I found a solution that works!
The new solution is to use cmds.file() to query the current files location(this gives an absolute path fortunately), split the name up a couple lines and then point it to the export folder. now the script seems to be working a lot better. and a heck of a lot better than crossing my fingers and hoping that maya converts a relative string to an absolute one.