problem with city generator

still having problems with my city generator script:





#Cityscape v.01
#based extensively on the Demo Script in the Blender 2.3x Manual
##########################################
import Blender
from Blender import NMesh
from Blender.BGL import *
from Blender.Draw import *

import math
from math import *
import whrandom

#building parameters
T_height = Create(10)
T_width  = Create(1)
T_depth  = Create(1)
T_buildnum = Create(10)

#Events
EVENT_NOEVENT = 1
EVENT_DRAW    = 2
EVENT_EXIT    = 3
EVENT_CITY    = 4
############################################
#Drawing GUI
############################################
def draw() :
	global T_height
	global T_width
	global T_depth
	global T_buildnum
	global EVENT_NOEVENT, EVENT_DRAW, EVENT_EXIT

	##############title
	glClear(GL_COLOR_BUFFER_BIT)
	glRasterPos2d(8, 173)
	Text("Cityscape")	
	
	############# GUI buttons
	glRasterPos2d(8, 153)
	Text("Settings")
	T_height = Number("Height: ", EVENT_NOEVENT,
						10, 125, 210, 18,
				T_height.val, 1, 60, "Height of building") ;
	T_width = Number("Width: ", EVENT_NOEVENT,
						10, 105, 210, 18,
				T_width.val, 1, 10, "Width of building") ;
	T_depth = Number("Depth: ", EVENT_NOEVENT,
						10, 85, 210, 18,
				T_depth.val, 1, 10, "Depth of building") ;
	
	T_buildnum = Number("Number of buildings: ", EVENT_NOEVENT,
							250, 125, 210, 18,
					T_buildnum.val, 2, 1000, "Number of buildings") ; 	

	T_citygen = Button("Generate City", EVENT_CITY,
							250, 100, 
				100, 25, "generate city") ;



	############Draw and Exit Buttons
	Button("Draw", EVENT_DRAW, 10, 20, 80, 18)
	Button("Exit", EVENT_EXIT, 140, 20, 80, 18)

##################################################
#Handling Events
###################################################
def event(evt, val):	
	if (evt == QKEY and not val): 
		Exit()

def bevent(evt):
	global T_height
	global T_width
	global T_depth
	global T_citygen
	global T_buildnum
	global EVENT_NOEVENT,EVENT_DRAW,EVENT_EXIT

	######### Manages GUI events
	if (evt == EVENT_EXIT): 
		Exit()
	elif (evt== EVENT_DRAW):
		Building(T_height.val, T_width.val, T_depth.val)
		
	if (evt == EVENT_CITY):
		city()			
	Blender.Redraw()

Register(draw, event, bevent)

########################################					
#Creating the building
########################################
def Building(height, width, depth):

##############Create the mesh
	poly = NMesh.GetRaw()
	
############## Add vertices
	v = NMesh.Vert(0.0,0.0,0.0)
	poly.verts.append(v)
	v = NMesh.Vert(width,0.0,0.0)
	poly.verts.append(v)
	v = NMesh.Vert(0.0,depth,0.0)
	poly.verts.append(v)		
	v = NMesh.Vert(width,depth,0.0)
	poly.verts.append(v)
	v = NMesh.Vert(0.0,0.0,height)
	poly.verts.append(v)
	v = NMesh.Vert(width,0.0,height)
	poly.verts.append(v)
	v = NMesh.Vert(width,depth,height)
	poly.verts.append(v)
	v = NMesh.Vert(0.0,depth,height)
	poly.verts.append(v)	

	f = NMesh.Face()
	f.v.append(poly.verts[0 ])
	f.v.append(poly.verts[1 ])
	f.v.append(poly.verts[3 ])
	f.v.append(poly.verts[2 ])
	poly.faces.append(f)

	g = NMesh.Face()
	g.v.append(poly.verts[4])
	g.v.append(poly.verts[5])
	g.v.append(poly.verts[6])
	g.v.append(poly.verts[7])
	poly.faces.append(g)

	h = NMesh.Face()
	h.v.append(poly.verts[0])
	h.v.append(poly.verts[2])
	h.v.append(poly.verts[7])
	h.v.append(poly.verts[4])
	poly.faces.append(h)

	i = NMesh.Face()
	i.v.append(poly.verts[7])
	i.v.append(poly.verts[6])
	i.v.append(poly.verts[3])
	i.v.append(poly.verts[2])
	poly.faces.append(i)

	j = NMesh.Face()
	j.v.append(poly.verts[0])
	j.v.append(poly.verts[1])
	j.v.append(poly.verts[5])
	j.v.append(poly.verts[4])
	poly.faces.append(j)

	k = NMesh.Face()
	k.v.append(poly.verts[5])
	k.v.append(poly.verts[6])
	k.v.append(poly.verts[3])
	k.v.append(poly.verts[1])
	poly.faces.append(k)

############## Creates new object
	polyObj = NMesh.PutRaw(poly)


############################################
#CITY STUFF
############################################




###########city
#generates buildings along x axis
def city():

	global height
	global T_width
	global depth
	global T_buildnum
	global EVENT_NOEVENT,EVENT_DRAW,EVENT_EXIT, EVENT_CITY
	
	
	for n in range(T_buildnum.val):
		Building(T_height.val, T_width.val, T_depth.val)
	
		sel = Blender.Object.GetSelected()

		for obj in sel:
			objlocx = obj.LocX
			objlocx = (n * T_width.val) + (T_width.val * n)
			obj.LocX = objlocx			
			City2()

			#adds randomness
			r = whrandom.random()
			if r < 0.7:
				r = r + 1			


			objsizex = obj.SizeX
			objsizex = objsizex * r
			obj.SizeX = objsizex
		
			
			#adds randomness
			r_2 = whrandom.random()
			if r_2 < 0.7:
				r_2 = r_2 + 1
		
			objsizez = obj.SizeZ
			objsizez = objsizez * r_2
			obj.SizeZ = objsizez
		

			#adds randomness
			r_3 = whrandom.random()
			if r_3 < 0.7:
				r_3 = r_3 + 1
		
			objsizey = obj.SizeY
			objsizey = objsizey * r_3
			obj.SizeY = objsizey
		
		City2()
		
###############################################################
def City2():
	global height
	global T_width
	global depth
	global T_buildnum
	global EVENT_NOEVENT,EVENT_DRAW,EVENT_EXIT, EVENT_CITY
	global objlocx

	
	for n in range(T_buildnum.val):
		Building(T_height.val, T_width.val, T_depth.val)
		
		#adds randomness
		r = whrandom.random()
		if r < 0.7:
			r = r + 1

		sel = Blender.Object.GetSelected()
		print(sel)
			
		for obj in sel:
			objlocy = obj.LocY
			objlocy = (n * T_depth.val) + (T_depth.val * n)
			obj.LocY = objlocy			

			objsizex = obj.SizeX
			objsizex = objsizex * r
			obj.SizeX = objsizex
			print(r)
			
			#adds randomness
			r_2 = whrandom.random()
			if r_2 < 0.7:
				r_2 = r_2 + 1
		
			objsizez = obj.SizeZ
			objsizez = objsizez * r_2
			obj.SizeZ = objsizez
			print(r_2)

			#adds randomness
			r_3 = whrandom.random()
			if r_3 < 0.7:
				r_3 = r_3 + 1
		
			objsizey = obj.SizeY
			objsizey = objsizey * r_3
			obj.SizeY = objsizey
		
		

		
		
		
			

		

its supposed to generate 1 building along y axis, then n on x axis, then one more on y axis, then n more on x axis, but i dont think that the y axis function is calling the x axis function properly.
anyone see the problem?

for a function to call another function the second function must be above the first

switch city1 and city2 around.

like this?



#Cityscape v.01
#based extensively on the Demo Script in the Blender 2.3x Manual
##########################################
import Blender
from Blender import NMesh
from Blender.BGL import *
from Blender.Draw import *

import math
from math import *
import whrandom

#building parameters
T_height = Create(10)
T_width  = Create(1)
T_depth  = Create(1)
T_buildnum = Create(10)

#Events
EVENT_NOEVENT = 1
EVENT_DRAW    = 2
EVENT_EXIT    = 3
EVENT_CITY    = 4
############################################
#Drawing GUI
############################################
def draw() :
	global T_height
	global T_width
	global T_depth
	global T_buildnum
	global EVENT_NOEVENT, EVENT_DRAW, EVENT_EXIT

	##############title
	glClear(GL_COLOR_BUFFER_BIT)
	glRasterPos2d(8, 173)
	Text("Cityscape")	
	
	############# GUI buttons
	glRasterPos2d(8, 153)
	Text("Settings")
	T_height = Number("Height: ", EVENT_NOEVENT,
						10, 125, 210, 18,
				T_height.val, 1, 60, "Height of building") ;
	T_width = Number("Width: ", EVENT_NOEVENT,
						10, 105, 210, 18,
				T_width.val, 1, 10, "Width of building") ;
	T_depth = Number("Depth: ", EVENT_NOEVENT,
						10, 85, 210, 18,
				T_depth.val, 1, 10, "Depth of building") ;
	
	T_buildnum = Number("Number of buildings: ", EVENT_NOEVENT,
							250, 125, 210, 18,
					T_buildnum.val, 2, 1000, "Number of buildings") ; 	

	T_citygen = Button("Generate City", EVENT_CITY,
							250, 100, 
				100, 25, "generate city") ;



	############Draw and Exit Buttons
	Button("Draw", EVENT_DRAW, 10, 20, 80, 18)
	Button("Exit", EVENT_EXIT, 140, 20, 80, 18)

##################################################
#Handling Events
###################################################
def event(evt, val):	
	if (evt == QKEY and not val): 
		Exit()

def bevent(evt):
	global T_height
	global T_width
	global T_depth
	global T_citygen
	global T_buildnum
	global EVENT_NOEVENT,EVENT_DRAW,EVENT_EXIT

	######### Manages GUI events
	if (evt == EVENT_EXIT): 
		Exit()
	elif (evt== EVENT_DRAW):
		Building(T_height.val, T_width.val, T_depth.val)
		
	if (evt == EVENT_CITY):
		city()			
	Blender.Redraw()

Register(draw, event, bevent)

########################################					
#Creating the building
########################################
def Building(height, width, depth):

##############Create the mesh
	poly = NMesh.GetRaw()
	
############## Add vertices
	v = NMesh.Vert(0.0,0.0,0.0)
	poly.verts.append(v)
	v = NMesh.Vert(width,0.0,0.0)
	poly.verts.append(v)
	v = NMesh.Vert(0.0,depth,0.0)
	poly.verts.append(v)		
	v = NMesh.Vert(width,depth,0.0)
	poly.verts.append(v)
	v = NMesh.Vert(0.0,0.0,height)
	poly.verts.append(v)
	v = NMesh.Vert(width,0.0,height)
	poly.verts.append(v)
	v = NMesh.Vert(width,depth,height)
	poly.verts.append(v)
	v = NMesh.Vert(0.0,depth,height)
	poly.verts.append(v)	

	f = NMesh.Face()
	f.v.append(poly.verts[0 ])
	f.v.append(poly.verts[1 ])
	f.v.append(poly.verts[3 ])
	f.v.append(poly.verts[2 ])
	poly.faces.append(f)

	g = NMesh.Face()
	g.v.append(poly.verts[4])
	g.v.append(poly.verts[5])
	g.v.append(poly.verts[6])
	g.v.append(poly.verts[7])
	poly.faces.append(g)

	h = NMesh.Face()
	h.v.append(poly.verts[0])
	h.v.append(poly.verts[2])
	h.v.append(poly.verts[7])
	h.v.append(poly.verts[4])
	poly.faces.append(h)

	i = NMesh.Face()
	i.v.append(poly.verts[7])
	i.v.append(poly.verts[6])
	i.v.append(poly.verts[3])
	i.v.append(poly.verts[2])
	poly.faces.append(i)

	j = NMesh.Face()
	j.v.append(poly.verts[0])
	j.v.append(poly.verts[1])
	j.v.append(poly.verts[5])
	j.v.append(poly.verts[4])
	poly.faces.append(j)

	k = NMesh.Face()
	k.v.append(poly.verts[5])
	k.v.append(poly.verts[6])
	k.v.append(poly.verts[3])
	k.v.append(poly.verts[1])
	poly.faces.append(k)

############## Creates new object
	polyObj = NMesh.PutRaw(poly)


############################################
#CITY STUFF
############################################

def City2():
	global height
	global T_width
	global depth
	global T_buildnum
	global EVENT_NOEVENT,EVENT_DRAW,EVENT_EXIT, EVENT_CITY
	global objlocx

	print('hello')

	for n in range(T_buildnum.val):
		Building(T_height.val, T_width.val, T_depth.val)
		
		#adds randomness
		r = whrandom.random()
		if r < 0.7:
			r = r + 1

		sel = Blender.Object.GetSelected()
		print(sel)
			
		for obj in sel:
			objlocy = obj.LocY
			objlocy = (n * T_depth.val) + (T_depth.val * n)
			obj.LocY = objlocy			

			objsizex = obj.SizeX
			objsizex = objsizex * r
			obj.SizeX = objsizex
			print(r)
			
			#adds randomness
			r_2 = whrandom.random()
			if r_2 < 0.7:
				r_2 = r_2 + 1
		
			objsizez = obj.SizeZ
			objsizez = objsizez * r_2
			obj.SizeZ = objsizez
			print(r_2)

			#adds randomness
			r_3 = whrandom.random()
			if r_3 < 0.7:
				r_3 = r_3 + 1
		
			objsizey = obj.SizeY
			objsizey = objsizey * r_3
			obj.SizeY = objsizey
		


###########city
#generates buildings along x axis
def city():

	global height
	global T_width
	global depth
	global T_buildnum
	global EVENT_NOEVENT,EVENT_DRAW,EVENT_EXIT, EVENT_CITY
	
	
	for n in range(T_buildnum.val):
		Building(T_height.val, T_width.val, T_depth.val)
	
		sel = Blender.Object.GetSelected()

		for obj in sel:
			objlocx = obj.LocX
			objlocx = (n * T_width.val) + (T_width.val * n)
			obj.LocX = objlocx			
			City2()

			#adds randomness
			r = whrandom.random()
			if r < 0.7:
				r = r + 1			


			objsizex = obj.SizeX
			objsizex = objsizex * r
			obj.SizeX = objsizex
		
			
			#adds randomness
			r_2 = whrandom.random()
			if r_2 < 0.7:
				r_2 = r_2 + 1
		
			objsizez = obj.SizeZ
			objsizez = objsizez * r_2
			obj.SizeZ = objsizez
		

			#adds randomness
			r_3 = whrandom.random()
			if r_3 < 0.7:
				r_3 = r_3 + 1
		
			objsizey = obj.SizeY
			objsizey = objsizey * r_3
			obj.SizeY = objsizey
		City2()

because that still dont work


#CITYSCAPE V.01
##########################################
import Blender
from Blender import NMesh, Object, Scene
from Blender.Draw import*
from Blender.BGL import*
from whrandom import random

#building parameters
T_height = Create(10)
T_width = Create(1)
T_depth = Create(1)
T_buildnum = Create(10)
#events
EVENT_NOEVENT = 1
EVENT_DRAW = 2
EVENT_RAND = 3
EVENT_CITY = 4
EVENT_EXIT = 5
scene = Scene.GetCurrent()

########################################
#creating the building
########################################
def Building (h, w, d, x, y):
	#create the mesh
	mesh = NMesh.New('Block')
	#create vertices
	verts = [(0.0, 0.0, 0.0), (w, 0.0, 0.0), (0.0, d, 0.0), (w, d, 0.0), \
				 (0.0, 0.0, h), (w, 0.0, h), (w, d, h), (0.0, d, h)]

	for vco in verts:
		mesh.verts.append(NMesh.Vert (vco[0], vco[1], vco[2]))

	#mesh the vertices
	m = [[0, 1, 3, 2], [4, 5, 6, 7], [0, 2, 7, 4], \
			 [7, 6, 3, 2], [0, 1, 5, 4], [5, 6, 3, 1]]

	for i in m:
		f = NMesh.Face()
		[f.v.append(mesh.verts[j]) for j in i]
		mesh.faces.append(f)

	#creates new object
	block = Object.New ('Mesh', 'Block')
	scene.link(block)
	block.link(mesh)
	block.setLocation (x, y, 0)

############################################
#city stuff
############################################
#generate buildings along xy axis
def City1 ():
	#generate buildings
	for n in range(T_buildnum.val):
		for k in range(T_buildnum.val):
			#calculate and pass xy location
			lx = 2 * n * T_width.val
			ly = 2 * k * T_width.val
			Building (T_height.val, T_width.val, T_depth.val, lx, ly)

def City2 ():
	#get all blocks in list
	city = []
	[city.append(o) for o in Object.Get() if o.getName()[:5] == 'Block']

	#traverse city blocks
	for cb in city:
		#randomize
		r = [random(), random(), random()]

		for i in [0, 1, 2]:
			if r[i] < 0.7: r[i] += 1

		cb.setSize(r)

############################################
#drawing gui
############################################
def draw ():
	global T_height
	global T_width
	global T_depth
	global T_buildnum
	global EVENT_NOEVENT, EVENT_DRAW, EVENT_RAND, EVENT_EXIT
	#title
	glClearColor (0.7, 0.7, 0.7, 1.0)
	glClear(GL_COLOR_BUFFER_BIT)
	glColor3f (0.1, 0.1, 0.1)
	glRasterPos2d (10, 170)
	Text("TA-ZANY'S :", "small")
	#gui buttons
	glRasterPos2d (10, 150)
	Text("*** CONSTRUCTION BEESKNEES ***")
	T_height = Number ("Height: ", EVENT_NOEVENT, 10, 125, 210, 18, T_height.val, 1, 60, "Height of building");
	T_width = Number ("Width: ", EVENT_NOEVENT, 10, 105, 210, 18, T_width.val, 1, 10, "w of building");
	T_depth = Number ("Depth: ", EVENT_NOEVENT, 10, 85, 210, 18, T_depth.val, 1, 10, "Depth of building");
	T_buildnum = Number ("Number of buildings: ", EVENT_NOEVENT, 10, 65, 210, 18, T_buildnum.val, 2, 1000, "Number of buildings");
	T_citygen = Button ("Generate City", EVENT_CITY, 10, 40, 210, 22, "generate city");
	#draw and exit buttons
	Button ("Draw", EVENT_DRAW, 10, 20, 70, 18)
	Button ("Rand", EVENT_RAND, 80, 20, 70, 18)
	Button ("Exit", EVENT_EXIT, 150, 20, 70, 18)

##################################################
#handling events
##################################################
def event (evt, val):
	if (evt == ESCKEY or evt == QKEY and not val):
		Exit()

def bevent (evt):
	global T_height
	global T_width
	global T_depth
	global T_citygen
	global T_buildnum
	global EVENT_NOEVENT, EVENT_DRAW, EVENT_EXIT, EVENT_RAND, EVENT_CITY

	#gui events
	if (evt == EVENT_EXIT):
		Exit()

	elif (evt == EVENT_RAND):
		City2()

	elif (evt == EVENT_DRAW):
		Building (T_height.val, T_width.val, T_depth.val, 0, 0)

	if (evt == EVENT_CITY):
		City1()

	Blender.Redraw()

Register (draw, event, bevent)

have fun.

ya some lines got wrapped … unwrap them manually and strip trailing whitespace. anyway blender will complain till you do it right and tell which line it is.

wow, thanks a lot…im really new to python tho, do u think u could explain the changes u made for future reference?

well basically I split process in 3 parts:

  • making actual mesh(so you could edit every one later), making building object and linking these two, placing object
  • calculating city grid and passing parameters to above
  • separate randomizing function(reads ‘Block’ objects), which you can use to very simply get the cityscape you want, even if actual mesh creation process has been done already.

Hey tedi an TaZany.

I got the script to work after I removed trailing spaces from the cut and paste(Only two lines, not bad considering with some script cut and pasted from here there are 50 to 60 lines you have to do that on).

I have two questions.

#1 What does the Draw button do. When I press it nothing seems to happen.

#2 It seems that the Random button only works AFTER you create a city? Then it puts random blocks ON the blocks already drawn or does it ad new blocks at random places. I could not tell on my machine.

1 ) draws a single block (mesh+object) at 0,0,0 so you can a) admire how brilliant it’s working b) add some more blocks to your city if you really feel to

2 ) it randomizes parameters after creation (says so in my description already) so you can come back and just re-run the randomizer after meshes have been created, maybe textured, added detail etc.

as this is just cough educational cough thingie there’s a room in it to add your own stuff, actually, the most useful would be to hardcode some more building types in it, and to code randomizing citygrid function or use something along lines of [Building(params) for v in gridmesh.verts] to spawn buildings in duplivert fashion and/or reduce number of generated meshes so it’ll render half a second faster.

to be honest, I might need something to work this way, to split process in creation/randomization parts and maybe add some auto-texturing. I was checking some city-creation scripts here and this two-foldedness of process is what they lacked to be production-friendly imo.

ya to add this - draw button works as expected (here) even with script copied from this page so there’s a probability you screwed something up. again I recommend the metapad, prefered text editor of litestep users, to get scripts from the web behaving as expected.

k, thanks tedi for all ur help. ya, everyone, feel free to add/change any parts of it. im thinking of adding some other building shapes. thanks tedi for showing me much more effective ways of creating scripts. as soon as i get home, i’ll post the final v.1 of citygen (lines unwrapped any fully functioning)

make a good one so I can steal some codes from you.

https://blenderartists.org/forum/viewtopic.php?p=274994#274994
final script