Reputation: 211
I am still learning Python in Maya. I cannot get my code to work. I am trying to create a locator and parent it under my selection and then zero out the locators transforms.
import maya.cmds as cmds
sel = cmds.ls(selection = True)
loc = cmds.spaceLocator()
cmds.parent(loc, sel)
cmds.setAttr(loc, "translateX", 0)
I always get this error message:
#Error: TypeError: file <maya console> line 7: Invalid argument 1, '[u'locator6']'. Expected arguments of type ( list, )
Sometimes with a bit other code I get something like:
#There does not exist something with name 'translateX'
I know that it does work when I replace loc
with the name of the locator, but I try to keep the code as universal as I can and not bound to the name of a locator.
How does the setAttr
function work in Maya with variables? Maya docs and other questions at various forums could not help... :/
Upvotes: 0
Views: 2487
Reputation: 111
I know you seem to be asking asking for maya cmds but I'd like to chime this in as an alternative.
In PyMel the spaceLocator function returns the created object itself. The attributes are then methods inside the object, allowing you to do the following. One reason why I love PyMel!
import pymel.core as pm
sel = pm.selected()[0]
loc = pm.spaceLocator()
pm.parent(loc, sel)
loc.translateX.set(0)
Upvotes: 0
Reputation: 12218
setAttr
takes one complete attribute as its argument:
cmds.setAtt( loc + ".translateX", 0)
where loc
has to be a single string. spaceLocator
like most creation commands returns a list, not a single string. So the whole thing is:
sel = cmds.ls(selection = True)
loc = cmds.spaceLocator()
cmds.parent(loc, sel)
cmds.setAttr(loc[0] + ".translateX", 0)
Upvotes: 1