'Python: how to color certain parts of Maya object based on user input
First-time poster. I have a project where I need to color the faces of a Rubix cube made within Maya. I want to color each face of each cube segment based on user input, but I really have no clue where to start. I know in theory what I should do, but don't know how to go about doing it.
Included is the code that was written in class. I'd appreciate any help on this since I'd really like to learn Python without just copying whatever the professor does.
color_list=[(0,0,1),(1,1,1),(0,1,0),(1,1,0),(1,0.4,0),(1,0,0)]
color_keys=["B","W","G","Y","O","R"]
#cube=cmds.polyCube()
cmds.scale(10,10,10)
#cmds.select(cube[0]+".f[0]")
def create_shaders(lst_of_color):
shaders=[]
for rgb_color in lst_of_color:
shader=cmds.shadingNode('lambert',asShader=True)
cmds.setAttr(shader+'.color',rgb_color[0],rgb_color[1],rgb_color[2],type='double3')
shaders.append(shader)
return shaders
shaders=create_shaders(color_list)
def color_one_cube(cube_name,shaders):
for i in range(6):
cmds.select(cube_name+".f["+str(i)+"]")
shader=shaders[i]
cmds.hyperShade(assign=shader)
def repeat_cubes(shader,cube_size,gap):
x_ct=3
z_ct=3
y_ct=3
for i in range(x_ct):
x=i*(cube_size+gap)
for j in range(z_ct):
z=j*(cube_size+gap)
for k in range(y_ct):
y=k*(cube_size+gap)
cube=cmds.polyCube()
cmds.scale(cube_size,cube_size,cube_size)
cmds.move(x,y,z)
color_one_cube(cube[0],shader)
#repeat_cubes(shaders,1,0.1)
def color_cube_cmnd(cube_name,shaders,color_commands):
for i in range(6):
cmds.select(cube_name+".f["+str(i)+"]")
if color_commands[i]:
shader=shaders[i]
cmds.hyperShade(assign=shader)
def acc_cubes(cube_size,shaders,gap):
x_ct=3
z_ct=3
y_ct=3
color_commands=[]
for i in range(6):
color_commands.append(False)
for i in range(x_ct):
x=i*(cube_size+gap)
color_commands[5] = i==0
color_commands[4]=i==2
for j in range(z_ct):
z=j*(cube_size+gap)
#if j==0:
# color_commands[2]=True
#else:
# color_commands[2]=False
color_commands[2]=j==0
color_commands[0]=j==2
for k in range(y_ct):
color_commands[3]=k==0
color_commands[1]=k==2
y=k*(cube_size+gap)
cube=cmds.polyCube()
cmds.scale(cube_size,cube_size,cube_size)
cmds.move(x,y,z)
color_cube_cmnd(cube[0],shaders,color_commands)
acc_cubes(1,shaders,0.1)```
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
