Random (rhino/python)

hello everyone,

we have a problem. we want to place objects randomly on a city we imported from open street map (osm). the thing is, we have no idea how to do this. we already have extruded our buildings and created a cylinder which should be placed on the objects (buildings). we thought we could place the cylinders on top of the buildings, which have the hight of 40 (our buildings extrude randomly between the hight of 30 to 50) any suggestions??? :slight_smile:

here’s what we have for now
import rhinoscriptsyntax as rs
import random as ran
import math


#Gebaeude extrudieren, cappen und Pfade loeschen
buildings = rs.ObjectsByLayer(“buildings”, True)
for i in range (0,len(buildings)):
hgt = ran.uniform(30,50)
points = [(0,0,0), (0,0,hgt)]
mypath = rs.AddCurve(points,1)
obj = rs.ExtrudeCurve(buildings[i],mypath)

rs.UnselectAllObjects ()

#layer ausschalten bis auf…
if 1:
layer_names = rs.LayerNames ()
rs.CurrentLayer (“Default”)
for i in range (0,len(layer_names)):
if layer_names[i] != “buildings”:
rs.LayerVisible (layer_names [i], False)

First, if you want your code to appear formatted correctly in a Discourse post, do the following

Enclose the code in two sets of three ‘backticks’, you can put in ‘python’ after the first 3 for python-specific formatting

<paste your entire code in here>

Now as to your script -

There’s nothing there yet for the creation/placement of your “cylinders”. In a loop, you would need to do something like get the bounding box of each object, get the top face of the bounding box and some point on that top face, then place or create the cylinder there. The tricky part of this is making sure the cylinder actually is inside the top building face border, as using something like the bounding box center cannot guarantee this.

As you have the building footprint curve already, perhaps first get it’s area centroid, raise that point to the building height and use that to place your cylinder. It still won’t guarantee the cylinder will lie 100% within the building roof border, but it might be closer…

thanks! i didn’t know that, it was my first post…
thank you for your fast answere! we try to work on your points!
best wishes