PDA

View Full Version : Maya Python Scripting Help


m4dcow
09-30-2010, 10:11 AM
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.

MoP
09-30-2010, 12:35 PM
That's weird, I tried it here and it works fine on Maya 2011.

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".

m4dcow
09-30-2010, 01:17 PM
I figured out where I may have an issue. I had been testing this in Maya 2010, and my instructor told me that Maya 2011 will not rename paths that don't exist, apparently this behaviour is the same in 2010 it seems and maybe earlier.

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.

ciubaka
04-19-2011, 05:33 AM
Hello I'm wondering is it possible to run mel expressions not through expression editor but inside python script
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!