script 24/04/23


import maya.cmds #import maya

import random

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)

    floor_set = cmds.sets(n='floor_set')

def make_building():

    

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

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

    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()

    

    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]) #makes list of the created buildings coords if needed in future

        

    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) #2d array

coord_range = [-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,2,3,4,5,6,7,8,9,10,11] #used by later added function so was moved outside its original fn. still works fine  

block_order = [[0,1], [0,-1], [0,2], [0,-2], [1,0], [-1,0], [2,0], [-2,0], [1,1], [1,-1], [-1,1], [-1,-1], [1,2], [1,-2], [-1,2], [-1,-2], [2,1], [2,-1], [-2,1], [-2,-1], [2,2], [2,-2], [-2,2], [-2,-2]]    

def blocks():

    

    if cmds.objExists('block_set'): #only 1 block set allowed

        cmds.select('block_set')

        cmds.delete()

    

    floor()

    block_list = []

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

    for i in range (block_amount):

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

        cmds.move((block_order[i][0])*24,0,(block_order[i][1])*24)

        block_list.append(block[0])

    cmds.select(block_list)  

    block_set = cmds.sets(n='block_set')

           

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(len(coord_range)):

        cmds.instance( "car", leaf=True )   ###importing too many(?) cars currently crashes Maya, unsure if leaf as True changes this

        cmds.select("car")

        cmds.move(0,0,(coord_range[i])+(random.uniform(0,0.5)))

    cmds.delete("car")

def create_sunrise():

    

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

    

    if cmds.objExists('nurbsCircle1'): #as function this is within parents the light + sunSphere

        cmds.select('nurbsCircle1')    #to this circle,

        cmds.delete()                  #only the circle needs to checked/deleted 

        

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

    cmds.setAttr ('sunSphereShape.castsShadows', 0) #stops the sun sphere a 'light source' from casting shadow

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

    

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

    light = cmds.directionalLight(i = 10) #i intensity, not index

    

    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.keyTangent('directionalLight1', itt='linear', ott='linear') #itt ott, linear tangents for constant speed

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

                

    cmds.playbackOptions( minTime='0', maxTime='199' ) #0 to 199 to get rid of duplicate frame 200

    cmds.parent('sunSphere','directionalLight1','nurbsCircle1') #parents all created to nurbs circle

    cmds.select('nurbsCircle1')

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

    cmds.setAttr ('directionalLightShape1.color', 1, 0.928921, 0.538, type='double3') #sets colour

   

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()

    

#################################################### -- Main Menu

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)) #button with command

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

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. (1-24)", 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.