writing stringArray attribute with python api

188 views
Skip to first unread message

vince touache

unread,
Aug 12, 2019, 12:12:48 AM8/12/19
to Python Programming for Autodesk Maya
hi folks, 

For optimisation purpose, I'm trying to write/read some stringArray attributes. Never had any issue to do that in cpp, and it works fine in python as well for reading, but I can't figure out how to write this attribute with python, since it doesn't include any MStringArray (unlike MPointArray or other type of maya arrays).
Right now, I have :
# create a dummy stringArray plug
sph = cmds.polySphere(ch=0)[0]
cmds.addAttr(sph, ln='foo', dt='stringArray')
strplug = '.'.join((sph, 'foo'))
# get the function set
sel=MGlobal.getSelectionListByName(strplug)
hPlug=sel.getPlug(0).asMDataHandle()
fn=MFnStringArrayData(hPlug.data())
# fail to do anything with it.... also, noticed that by default it seems to error instead of returning an empty array..
fn.set('bar')

anyone has done that before ? What am I missing ? 

Thanks a lot

vince touache

unread,
Aug 12, 2019, 10:05:23 AM8/12/19
to Python Programming for Autodesk Maya
figured it out

plug  = MGlobal.getSelectionListByName('node.attr').getPlug(0)
dummy = MFnStringArrayData().create(['foo', 'bar'])
plug.setMObject(dummy)
Reply all
Reply to author
Forward
0 new messages