Search code examples
pythonpython-2.7loopsmayaautodesk

For Loop to Duplicate objects in Maya — Python 2


I have been using Maya for over 5 years and now want to start writing scripts for specific actions I want to perform in maya, using python. I do have some experience scripting in JS.

To briefly cover what I've already done:

I wanted to create multiple objects in Maya using a python for loop, to alter their positon or rotation. I was successful in this.

Now I want to select an object by name in Maya, and duplicate it, rotate/move/scale using a for loop. The problem I'm having is that I can't change the name of the object I am targeting when I duplicate it.

Normally using JS I would just use "i" and add it to the end of the name in the loop 'nameOfObject'+ i

Python only allows the name to be a string, and inputting an integer value gives me a syntax error.

Is there a way to perform this action?

My Code:

import maya.cmds as cmds
from random import randint

for i in range(0,50):
    cmds.duplicate('solitude')
    cmds.rotate(0,i*20,0)

It creates 50 of the same objects but I need to select the newly created object without hard coding the entire thing.


Solution

  • This type of error I think is common, try do the following:

    cmds.duplicate('solitude'+str(i))