Loop and Scatter


#1

Hey guys, I’m doing an assignment for school and I have no Python experience and my instructor isn’t much help. I keep having an issue that I can’t seem to resolve.

Error: TypeError: file line 18: can only concatenate list (not “str”) to list

This is the code I’m working with any help would be greatly appreciated.

import maya.cmds as cmds
import random

name=‘scale’;

#1) Create a polyCube, assign the name of the transform node to a variable named ‘cube’
myCube = cmds.polyCube(name=‘cube’)[0];
#2) Create a circle and assign the name of the transform node to a variable named ‘ctrl’, with the normals facing Y axis
myCtrl = cmds.circle(name=‘Ctrl’, nr=(0, 1, 0) );
#3) Create a spaceLocator and assign the name of the transform node to a variable named ‘locator’
mylocator = cmds.spaceLocator(name=‘locator’);
#4) SetAttr the cube.translateY to 0.5
cmds.setAttr(myCube+ ‘.ty’, random.uniform(-0.5,0.5));
#5) SetAttr the ctrl.translateY to 0.5
cmds.setAttr (myCtrl+ ‘.ty’, random.uniform(-0.5,0.5));
#6) Parent cube under ctrl
cmds.parent(‘myCube’, ‘Ctrl’, relative=True);
#7) Parent ctrl under locator
cmds.parent(‘Ctrl’, ‘locator’, relative=True);
#8) SetAttr locator.translateX randomly from -20 to 20
cmds.setAttr(locator+ ‘.tx’, random.uniform(-20,20));
#9) SetAttr locator.translateY randomly from -20 to 20
cmds.setAttr(locator+ ‘.ty’, random.uniform(-20,20));
#10) SetAttr locator.translateZ randomly from -20 to 20
cmds.setAttr(locator+ ‘.tz’, random.uniform(-20,20));
#11) SetAttr ctrl.rotateY randomly from 0 to 360
cmds.setAttr(Ctrl+ ‘.ry’, random.uniform(0,360));
#12) Assign a random value from 0.1 to 3 to a variable called “myScale”
myscale = random.uniform(0.1,3);
#13) in a for loop of [‘x’, ‘y’, ‘z’] (axis) do the following: setAttr locator.scale of axis to the value of “myScale”
count = 50;
scatter = 5;

for number in range(count):
myCube = cmds.polyCube()[0];
cmds.setAttr(myCube+’.tx’, random.uniform(-scatter, scatter));
cmds.setAttr(myCube+’.ty’, random.uniform(-scatter, scatter));
cmds.setAttr(myCube+’.tz’, random.uniform(0,360));
scale_value=random.uniform(0.1,5);

for axis in [‘x’,‘y’,‘z’]:
cmds.setAttr(myCube + ‘.s’ + axis, scale_value);


#2

Notice how myCube is assigned like this:

myCube = cmds.polyCube(name=‘cube’)[0];

cmds.polyCube returns a list, and the [0] means get the first element of the list.

Well you need to do the same thing when creating the circle:

myCtrl = cmds.circle(name=‘Ctrl’, nr=(0, 1, 0) )[0];

so that later when you do myCtrl+ ‘.ty’ it will concatenate two strings instead of a list and a string.

By the way, once you fix that, I’m seeing several other errors further down. If you need more help, let me know.