4

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.

John Connor
  • 175
  • 1
  • 2
  • 7

2 Answers2

2

I know you've already accepted an answer, and it will work to use the iterator number in conjunction with the source node's name - provided you don't already have a few duplicates in the scene...

A more "bulletproof" solution would be to capture what the duplicate command returns into a variable, and then use that item to do your rotation on. This way, you do not need to construct the name of the duplicate since you actually already have it regardless of whatever maya decided to name it.

If this is something you need to do a lot of, you can also capture your selection to use as a source node before doing the duplication and rotation. So, you could use the following code by selecting an item, and then running code.

import maya.cmds as mc

src = mc.ls(sl=True)[0]
dup = src
for i in xrange(50):
    dup = mc.duplicate(dup)[0]
    mc.rotate(0,i*20,0, dup)

also, a minor note - it doesn't seem like you're using randint in your code... you might be using it elsewhere, though...

silent_sight
  • 492
  • 1
  • 8
  • 16
1

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

cmds.duplicate('solitude'+str(i))
Ghoul Fool
  • 6,249
  • 10
  • 67
  • 125
developer_hatch
  • 15,898
  • 3
  • 42
  • 75