Search code examples
pythonmaya

Instancing maya objects with a sequential suffix, object name string not seen by cmds.instance


I have a question about string usage in lists in python for Maya. I am writing a script meant to take a selected object, then instance it 100 times with random translate, scale, and orient attributes. The script itself works and does what it's meant to, however I'm not being able to decipher how to instance the objects with the original object name, and then add a suffix that ends with "_instance#", where # assigns 1, 2, 3, etc. in order to the copies of the original mesh. This is where I'm at so far:

#Capture selected objects, sort into list
    thing = MC.ls(sl=True)
    print thing

#Create instances of objects
    instanceObj = MC.instance(thing, name='thing' + '_instance#')

This returns a result that looks like "thing_instance1, thing_instance2".

Following this, I figured the single quote around the string for the object was causing it to just name it "thing", so I attempted to write it as follows

MC.instance(thing, name=thing + '_instance1'

I guess because instance uses a list, it's not accepting the second usage of the string as valid and returns a concatenate error. I've tried rewriting this a few times and the closest I get is with

instanceObj = MC.instance(thing)

which results in a list of (pCube1,2,3,4), but is lacking the suffix.

I'm not sure where to go from here to end up with a result where the instanced objects are named with the convention "pCube1_instance1, pCube1_instance2" etc.

Any assistance would be appreciated.


Solution

  • It is not clear if you want to use only one source object or more. In any case the

    MC.ls(sl=True)
    

    returns a list of strings. And concatenating a list and a string does not work. So use thing[0] or simply

    MC.ls(sl=True)[0]
    

    If you get errormessages, please always include the message in your question, it helps a lot to see what error appears.