0

I currently have a random object generator script for MEL. I'm trying to convert it to Python. This is a homework assignment and I'm stuck at a particular section. I'm trying to add a random scale to each axis. I keep getting "can only concatenate list (not "str") to list #". Here's what I have so far:

#to use: my_rock_gen(1, poly_rock1)

#import python libraries
import maya.cmds as MC
import random

#define procedure with number of rocks and name

def my_rock_gen(number_of_rocks=0, rock_name="poly_rock1"):

    #loop to generate rocks
    for n in range(number_of_rocks):

        #start with creating polygon object, basic cube
        rock=MC.polyCube (name=rock_name)

        #smooth it once
        MC.polySmooth (rock, dv=2)        

        #give random scales
        random_sx= random.uniform (.3, 3)
        random_sy= random.uniform (.3, 3)
        random_sz= random.uniform (.3, 3)

        #set random values to scales
        MC.setAttr ((rock + ".scaleX"), random_sx)"

I'm stuck at the last bit. In MEL here's what I have for the last 2 parts:

//set random scale range
float $random_sx = `rand .3 3`;
float $random_sy = `rand .3 3`;
float $random_sz = `rand .3 3`;

//set random values to scales
setAttr ($rock[0] + ".sx") $random_sx;
setAttr ($rock[0] + ".sx") $random_sy;
setAttr ($rock[0] + ".sx") $random_sz;

The $rock[0] is created with the beginning part of the script. I just can't figure out how the syntax should go for the setAttr part. Thanks for any help offered.

1 Answers1

0

rock is a list, try

rock = [MC.setAttr((x),random_sx) for x in rock] 

to set the attr on the elements of the list (no maya here)

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • I need to repeat this for scale in y and z and rock is already declared earlier. How would I add that? I also use the rock variable again to do a random rotation in all 3 axis. – JonCathcart Nov 17 '17 at 07:04
  • this will set random_sx the same for each eleement in rock. if you want different values or setting more things, use a `for myRock in rock:` loop and do whatever you need to do to `myRock` (tickle it, rotate it, dust it, ...). I did not use maya yet, operating on general python knowledge. – Patrick Artner Nov 17 '17 at 07:35