script_final
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 del_floor():
if cmds.objExists('floor_set'):
cmds.select('floor_set')
cmds.delete()
if cmds.objExists('block_set'):
cmds.select('block_set')
cmds.delete()
else:
print("Currently no floors to delete!")
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(buildings_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 changes with location of car file, 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("car1")
def del_sunrise():
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
def create_sunrise():
del_sunrise()
radius=clamp(cmds.intField(cycle_radius_field, query=True, value=True),40, 300)
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 play_anim():
cmds.play( forward=True )
def stop_anim():
cmds.play( state=False )
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, 752), bgc=(0.27,0.27,0.27)) #defining window
cmds.columnLayout( adjustableColumn=True )
cmds.button( label='Create Floor',c='floor()', height=30, bgc=(1,0.26,0.26)) #button with command
cmds.button( label='Delete Floor(s)',c='del_floor()', height=30, bgc=(1,0.26,0.26))
cmds.text(label="Enter the number of buildings in the field below. (10-400)", h=40) #text field
buildings_density_field = cmds.intField(min=10, max=400, bgc=(0.36,0.36,0.36))
cmds.button( label='Create Buildings',c='buildings_create()', height=30, bgc=(0.86,0.58,0.34))
cmds.button( label='Delete Buildings',c='buildings_delete()', height=30, bgc=(0.86,0.58,0.34))
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=24, bgc=(0.36,0.36,0.36))
cmds.button( label='Make City Blocks', c='blocks()', height=30, bgc=(0.29,0.67,0.71))
cmds.button( label='Create Cars',c='car_spawn()', height=50, bgc=(0.29,0.67,0.71))
cmds.text(label="Sun cycle radius below. (40-300)", h=40)
cycle_radius_field = cmds.intField(min=40, max=300, bgc=(0.36,0.36,0.36))
cmds.button( label='Create Sun Cycle',c='create_sunrise()', height=50, bgc=(0.68,0.61,0.86))
cmds.button( label='Delete Sun Cycle',c='del_sunrise()', height=50, bgc=(0.68,0.61,0.86))
cmds.button( label='Fit All to View',c='view()', height=50, bgc=(0.35,0.68,0.49))
cmds.text(label="Use the buttons below to control playback.", h=40)
cmds.button( label='Play',c='play_anim()', height=40, bgc=(0.1,0.95,0.1))
cmds.button( label='Stop', command=('stop_anim()'), height=40, bgc=(0.95,0.1,0.1))
cmds.button( label='Delete All',c='del_all()', height=50, bgc=(0.05,0.05,0.05))
cmds.button( label='Close Menu and Delete All',c='close_delete_all()', height=50, bgc=(0.05,0.05,0.05))#buttons
cmds.setParent( '..' )
cmds.showWindow('new_window')
UP2115068 - TB2 SCRIPTS
TB2 SCRIPTS
More posts
- script 24/04/23Apr 24, 2023
- py city sun cycleMar 20, 2023
- python city 20/03/23Mar 20, 2023
- random cubes arrange by widthJan 30, 2023
- city button testJan 23, 2023
Leave a comment
Log in with itch.io to leave a comment.