python city 20/03/23


import maya.cmds #import maya

import random

#width - x, height - y, depth - z

### city size is on intfield

def clamp( num, min_value, max_value):

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

def floor():

    if cmds.objExists('floor'): #only 1 floor allowed

        cmds.select('floor')

        cmds.delete()

    floor = cmds.polyPlane(n="floor", w=24, h=24, sy=24, sx=24)

def make_building():

    

    x_spread=(int(random.uniform(0,20)))

    z_spread=(int(random.uniform(0,20)))

    cube_height=random.uniform(1,12)   

    return (x_spread,cube_height,z_spread)

    

    

def buildings_delete():

    

    if cmds.objExists('cube_set1'):

        cmds.select('cube_set1')

        cmds.delete() #keeps buildings created with buildings_create() to one set

    else:

        print("There are no buildings to delete!")

          

            

def buildings_create():

    

    buildings_delete()

    

    coord_range = [-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,2,3,4,5,6,7,8,9,10,11]

    added_coords = []

    building_list = []

    loop_range=clamp(cmds.intField(build_density_field, query=True, value=True),10, 400)

    

    for i in range(loop_range):

        

        make_building()

        build_x, height, build_z  = make_building() #'turns' (x_spread,cube_height,z_spread) into (build_x, height, build_z)

        building=cmds.polyCube(h=height)

        cmds.move((coord_range[build_x]),(height/2),(coord_range[build_z]))

        current_coords=[coord_range[build_x],coord_range[build_z]]

        added_coords.append(current_coords)

        building_list.append(building[0])

        print(height)

        

    cmds.select(building_list)  

    cube_set = cmds.sets(n='cube_set1') #adds created cube(s) to their own set

    cmds.select('cube_set1')

    print(added_coords)

    

    

def blocks():

    block_coords=[-4,-3,-2,-1,0,1,2,3,4]

    block_amount=clamp(cmds.intField(block_number_field, query=True, value=True),1, 35)

           

def car_spawn():

    file_path = "N:\maya\projects\default\scenes\TB2 - Y1\small_car.obj" #ideal if file path is dynamic, but in this case it doesnt need to be

    cmds.file(file_path, i=True)

    cmds.move(100,100,100, "car")

    

    for i in range(0,2):

        cmds.instance( "car" )   ###IMPORTING too many(?) cars currently crashes Maya

    cmds.delete("car")

def create_sunrise():

    

    radius=clamp(cmds.intField(cycle_radius_field, query=True, value=True),40, 500) 

    

    if cmds.objExists('nurbsCircle1'):

        cmds.select('nurbsCircle1')

        cmds.delete()

        

    cmds.polySphere(n='sunSphere', r=(radius/10))

    cmds.move(0,0,(radius))

    circle1 = cmds.circle(nr=(0,1,0),r=radius)

    

    for i in range(2):

        cmds.currentTime((i)*200, edit=True)

        cmds.setKeyframe(circle1,v=(i*-360), at='rotateX')

        cmds.keyTangent('nurbsCircle1', itt='linear', ott='linear')

        cmds.setInfinity( pri='cycle', poi='cycle')

        

    cmds.playbackOptions( minTime='0', maxTime='199' )

    cmds.parent('sunSphere','nurbsCircle1')

    cmds.select('nurbsCircle1')

    cmds.setAttr ('nurbsCircle1.rotateY', -25)

   

def view():

    

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

         

         

def del_all():

    

    cmds.select( all=True )

    cmds.delete()

  

  

def close_delete_all():

    

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

    del_all()

    

####################################################

if cmds.window("new_window", exists=True): #if exists when script is run, delete

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

    

the_window = cmds.window( "new_window", title="Cit(.p)y", widthHeight=(350, 360), bgc=(1,0.8,0.7)) #defining window

cmds.columnLayout( adjustableColumn=True )

cmds.button( label='Create Floor',c='floor()', height=30, bgc=(1,0.2,0.2))

cmds.text(label="Enter the number of buildings in the field below. (10-400)", h=40)

build_density_field = cmds.intField(min=10, max=400)

cmds.button( label='Create Buildings',c='buildings_create()', height=30, bgc=(1,0.85,0))

cmds.button( label='Delete Buildings',c='buildings_delete()', height=30, bgc=(1,0.85,0))

cmds.text(label="Enter the number of city blocks in the field below. (10-400)", h=40)

block_number_field = cmds.intField(min=1, max=35)

cmds.button( label='Make City Blocks', c='blocks()', height=30, bgc=(1,0.7,0))

cmds.button( label='Create Cars',c='car_spawn()', height=50, bgc=(0.1,0.35,1))

cmds.text(label="Sun cycle radius below. (40-500)", h=40)

cycle_radius_field = cmds.intField(min=40, max=500)

cmds.button( label='Create Sun Cycle',c='create_sunrise()', height=50, bgc=(1,0.85,0))

cmds.button( label='Fit all to View',c='view()', height=50, bgc=(0.4,0.4,0.4))

cmds.button( label='Delete All',c='del_all()', height=50, bgc=(0.2,0.2,0.2))

cmds.button( label='Close Menu and Delete',c='close_delete_all()', height=50, bgc=(0,0,0))#buttons

cmds.setParent( '..' )

cmds.showWindow('new_window')

Leave a comment

Log in with itch.io to leave a comment.