random cubes arrange by width


import maya.cmds

import random

    

def clamp( num, min_value, max_value):

    return max(min(num, max_value), min_value)

    

def add_cube(custom_range):

    cube_width=random.uniform(1,custom_range)

    cmds.polyCube(w=cube_width, d=cube_width, h=0.5)

    print ("Cube width:", cube_width)

    cmds.move((custom_range-cube_width), y=True)

    

def cube_loop(loop_num):

    loop_num = clamp(cmds.intField(cube_field, query=True, value=True),1,200)

    custom_range = clamp(cmds.intField(range_field, query=True, value=True),1,50)

    for i in range(loop_num):

        add_cube(custom_range)

        

def view():

    cmds.viewFit(all=True, an=True)

    

if cmds.window("new_window", exists=True):

    cmds.deleteUI("new_window", window=True)

window = cmds.window( "new_window", title="cubes", widthHeight=(400, 700), bgc=(0.7,0.8,0.2)) #defining window

cmds.columnLayout( adjustableColumn=True )

cmds.text(label="Enter the number of cubes. (1-200)", h=50)

cube_field = cmds.intField( minValue=1, maxValue=200, v=1, bgc=(1,1,1))#integer input field

cmds.text(label="Enter the maximum value. (1-50)", h=50)

range_field = cmds.intField( minValue=1, maxValue=50, v=1, bgc=(1,1,1))#integer input field

cmds.button( label='Create Cubes', command=('cube_loop(cube_field)'), height=40, bgc=(0.4,0.7,0.6))

cmds.button( label='Fit to view', command=('view()'), height=40, bgc=(0.2,0.7,0.5))

cmds.setParent( '..' )

cmds.showWindow( "new_window" )

Leave a comment

Log in with itch.io to leave a comment.