setattr not working for fileTextureName IF opened maya scene changed and saved in maya session. Works if file with modifications is opened in fresh maya session.

704 views
Skip to first unread message

Aleks Berland

unread,
Oct 23, 2013, 10:34:53 AM10/23/13
to python_in...@googlegroups.com
Hi guys!
I'm newish to python in maya, and new to this discussion group. Thanks for reading my topic!

Ok so I have a unique issue, tested in Maya 2013.0 x64.

Issue is I have a python script, reassigning texture paths and when the script is run on an opened file that changes are made to the path of the texture AND the file scene is saved, AND the path is visibly there... 
When the script is run, that fileTextureName node WILL NOT ACCEPT a setAttr var and remains UNMODIFIED.
However, if you reopen that saved file in a fresh maya session, the script works perfectly.
Following is some example code:
          
          import maya.cmds as cmds
files = cmds.ls(type="file")
          project_path = "/wherever"
          
         def myCopy (path, newpath):
              copy2(path, newpath)
              #example of doin somethin to  path.. can be commented out as the return i get in my function prints back below
              dosomethingtothepath = newpath.remove('\\','\')
              return dosomethingtothepath
 

for item in files:

    fullpath = cmds.getAttr('%s.fileTextureName' %item)

    newpath = myCopy(fullpath, project_path) #mycopy uses copy2 and returns the new path

              print "Copied: " + fullpath + " to: " + newpath #newpath echos back correctly here from mycopy function!!!

    cmds.setAttr("%s.fileTextureName" %item, newpath,type="string") #setting the new path

              print cmds.getAttr('%s.fileTextureName' % (item) #echos back old file path!!!!!!!


So to recreate the scenario:
1.  create simple maya scene with primitive and shader with texture. Save scene.
2. edit the project path var so it goes somewhere on your system. run this script.
3. should work correctly first time. so change the texture to some other file in shader, then save. run script... see? attribute not changed as visible in listener.
4. close maya. reopen. reopen modifed saved scene. run script. it works.

One other note is that the final path i am assigning in newpath DOES NOT EXIST locally, though I am not sure this makes a difference.

Thanks for taking time to read about my troubles.

- Aleks


Andres Weber

unread,
Oct 24, 2013, 4:16:34 PM10/24/13
to python_in...@googlegroups.com
I'm a little confused.   I tried running your script and it most definitely didn't work (for more than a few reasons) but it seems like you're very confused about what functions to use and where.  I've converted your script to PyMel (just to show, and also modified the cmds version).  Seems like you think you're working with actual "files" but in reality the "file" is really just a path as a string not a system file.  So you would just do either string manipulation or use manipulate with os.path functions.  You don't need to use copy2 as a function since that's more of an actual system file command rather than string manipulation.  (I also added in the way to get your PROJECT environment variable for fun.

PYMEL

import pymel.core as pm

import os

files = pm.ls(type="file")

project_path = os.getenv('MAYA_PROJECT')

def myCopy (path):

return path.replace('/','') #does both / and //...

for item in files:

fileNode = pm.PyNode(item)

fileNode.fileTextureName.set(myCopy(project_path), type='string') #setting the new path

print fileNode.fileTextureName.get() #echos back old file path!!!!!!!

CMDS

import maya.cmds as cmds

import os

files = cmds.ls(type="file")

project_path = os.getenv('MAYA_PROJECT')

def myCopy (path):

return path.replace('/','') #does both / and //...

for item in files:

cmds.setAttr( "%s.fileTextureName"%item, myCopy(project_path), type='string') #setting the new path

print cmds.getAttr( "%s.fileTextureName"%item) #echos back old file path!!!!!!!

Aleks Berland

unread,
Oct 24, 2013, 5:18:11 PM10/24/13
to python_in...@googlegroups.com
Hi,

Thanks for your response.
I appreciate that my example code was a little useless, but was only to show what i am doing. Pymel access to nodes vs cmds, is very useful tho.

I am not confused about filesystem files vs. string manipulation for sources in maya.
In fact, what this script does IS copy all files used in the scene (textures really), then reassign the path to where i copied them to.
Totally works. Copy first, then set the file handler attribute for textures value (the path) to my new string.

Problem is: when you run such a script, if you change the path in hypershade to texture, then save the file, if you run the script again while maya is open, it WILL NOT set the attribute. Try it.
Actually doesnt matter what the file handler is, the setattr will be ignored.
Reply all
Reply to author
Forward
0 new messages