I am taking a class right now where we are doing some simple scripting. The assignment for this script was to make something where if you migrate files to another folder you could update your paths to reflect the new location by searching an replacing the path, of all files or selected ones.
before I go on here is my code
import maya.cmds as cmds
#all or selected for mySelect input
mySelect='all'
mySearch=raw_input()
myReplace=raw_input()
if mySelect=='all':
myObjects = cmds.ls(type='file')
for obj in myObjects:
newPath = cmds.getAttr(obj+'.ftn')
#Replaces only one occurence of the search string
newPath = newPath.replace ( mySearch, myReplace, 1 )
cmds.setAttr(obj+'.ftn', newPath, type="string")
elif mySelect=='selected':
#doesn't work right now because the ftn isn't actually selected so myObjects has 0 elements
myObjects = cmds.ls(type='file', sl=True)
for obj in myObjects:
newPath = cmds.getAttr(obj+'.ftn')
#Replaces only one occurence of the search string
newPath = newPath.replace ( mySearch, myReplace, 1 )
cmds.setAttr((obj+'.ftn'), newPath, type="string")
else:
cmds.error('Error! Type "all" or "selection" in the first input box')
My problem is in the if block where I try to change the ftn attribute. (Ignore the elif block because I haven't gotten to that yet.)
- I have my variable newPath in there and it doesn't update.
- If I put in a string directly it works.
- If I assign newPath with a hard coded string it works (along with my search and replace)
- Once I use getAttr to assign the initial value of newPath, it doesn't seem to update.
- Even when I print out the value of newPath after the setAttr command, it looks to be fine.
I'm not experienced at scripting as you can see so hopefully someone can point out what I'm doing wrong here.
Replies
I added a file called "C:/path/to/file/desktop_01.jpg" and ran your script, typed "desktop" into the first prompt and "schmesktop" into the second prompt, and I ended up with "C:/path/to/file/schmesktop_01.jpg".
Trying my script on actual file names like MoP did works fine but this was intended as more of a migration script so you could change the paths of a file that had been moved to a diferent location and had the old path references. I guess if you are migrating the new paths should exist.
Now I just need to find out how to get the file nodes for a bunch of selected objects.
here is my script:
import maya.cmds as cmds
import maya.mel as mel
def lag(frame, goal, follower, lagAmt):
goalTrans = cmds.getAttr(goal +".translate", t=frame-lagAmt)
goalRot = cmds.getAttr(goal +".rotate", t=frame-lagAmt)
cmds.setAttr(follower+".translate",goalTrans[0][0], goalTrans[0][1], goalTrans[0][2])
cmds.setAttr(follower+".rotate",goalRot[0][0], goalRot[0][1], goalRot[0][2])
and this is the expression which I want to run but it does not work ;/
python("lag(" + frame + ", 'pSphere1', 'pSphere2', 2)")
Thank you!