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.


#3

hey i got this exact assignment and im lost on it to o

help
lol

size=5
cube = cmds.polycube()[0] # ‘pCube1’
#cmds.polyCube(name = “cube”, width=size, height=size, depth=size)
cmds.circle(name = “crtl”, nr=[0,1,0], radius= size)
cmds.spaceLocator (name=‘locator’)
cmds.setAttr(“cube”,‘translateX’, 0.5)
cmds.setAttr(“ctrl”,‘translateY’, 0.5)
cmds.parent(“cube”, “ctrl”)
cmds.parent(“ctrl”, “locator”)
cmds.setAttr(‘locator.translateX’, random(-20,20))
cmds.setAttr(‘locator.translateY’, random(-20,20))
cmds.setAttr(‘locator.translateZ’, random(-20,20))
cmds.setAttr(‘ctrl’, “ry”,random(0,360))
myScale = random(0.1,3)

this is what i had


#4

Your issue is where you have lines like this:

Change them to something like this:

When trying to set an attribute value on an object, the syntax is:

cmds.setAttr(“object.attribute”, newValue)


#5

I am glad I have found your post. I was also facing the same issue.
size=5
cube = cmds.polycube()[0] # ‘pCube1’
#cmds.polyCube(name = “cube”, width=size, height=size, depth=size)
cmds.circle(name = “crtl”, nr=[0,1,0], radius= size)
cmds.spaceLocator (name=‘locator’)
cmds.setAttr(“cube”,‘translateX’, 0.5)
cmds.setAttr(“ctrl”,‘translateY’, 0.5)
cmds.parent(“cube”, “ctrl”)
cmds.parent(“ctrl”, “locator”)
cmds.setAttr(‘locator.translateX’, random(-20,20))
cmds.setAttr(‘locator.translateY’, random(-20,20))
cmds.setAttr(‘locator.translateZ’, random(-20,20))
cmds.setAttr(‘ctrl’, “ry”,random(0,360))
myScale = random(0.1,3)
But I am glad I have found your post in which I have found my answer and it is all solved now. I would like to share the https://www.grabmyessay.com/essay-maker link with you in which you will find professional essay writers who will help you in completing your essay assignments. If you are facing problems with writing assignments or you don’t have time to write those assignments then you can take their help.