PDA

View Full Version : setAttr stringArray with Python


Sagroth
02-27-2008, 10:08 PM
Aargh! Can't figure out how to set the stringArray attribute with python. I mean in MEL it's:

setAttr obj.attr -type "stringArray" 1 "text";

but no way anything like this works in python:

cmds.setAttr( "obj.attr", "text", size = 1, type = "stringArray" )

Can anyone help?

Chadrik
02-28-2008, 12:12 AM
with maya.cmds:

import maya.cmds as cmds
cmds.setAttr('loc.strArray',3,"first", "second", "third",type='stringArray')


with pymel:

from pymel import *
setAttr('loc.strArray',["first", "second", "third"] )

Sagroth
02-28-2008, 12:29 AM
Great! Thanks :)

Sagroth
05-05-2008, 06:56 PM
There's a great way my fellow programmer Roman showed me today. Very useful for filling stringArray or componentList attributes without pre-defined number of elements of with a large number of them:

inList = [ 'vtx[100]', 'vtx[200]', 'vtx[300]' ]
cmds.setAttr( grp + '.strArr', type = 'stringArray', *([len(inList)] + inList) )

dmaher
11-13-2008, 03:08 AM
inList = [ 'vtx[100]', 'vtx[200]', 'vtx[300]' ]
cmds.setAttr( grp + '.strArr', type = 'stringArray', *([len(inList)] + inList) )


I have a follow-up question... What does the * symbol do?

NaughtyNathan
11-13-2008, 10:32 AM
I have a follow-up question... What does the * symbol do?


The * before the last parameter in an argument list denotes that parameter as a list of arbitrary length.

:nathaN

Sagroth
11-13-2008, 09:14 PM
Yeah, practically it takes a list and throws each of it's elements as a value for a command, but only at the end of it's arguments.

So you can replace:

move( 1, 2, 3 )

with:

trVar = [1, 2, 3]
move( *trVar )

but since move() asks for object after translation values and * does work only as the last argument, you can't use:

move( *trVar, 'pSphere1' )

but this will be valid:

setAttr( 'pSphere1.translate', type = 'double3', *trVar )

since setAttr receives values as the last argument. Same way when you try to fill your componentList or smth like that with elements of unknown quantity, you can use *.

dmaher
11-14-2008, 12:44 AM
Oh...that makes sense now. Thanks for the explanation. :)

-dm

Sagroth
11-14-2008, 10:59 AM
I had a talk with my friend programmer today... Actually, since move() wants object after values, you can use:

move( *trVar + ['pSphere1'] )

And this * thing is simplified version of apply() command. The full form is like this:

apply( move, trVar + ['pSphere1'] )

CGTalk Moderation
11-14-2008, 10:59 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.