How do I generate random cylinders, with random height on a predefined area?Generate random ellipsoidsAlgorithm for random distribution of circles in a squareLink one object's property to another object's different propertyGenerate random ellipsoidsFinding the python command for an addon (and using it for “Sapling: Add Tree”)Union partially overlapping cubes without boolean modifierHow can I effectively import and texture a large height mapped terrain mesh?Calculate a surface by checking if meshes intersectAlign an array of vertices to vectors with pythonRandom placement of cylinders with random orientationsWhat is the correct python code for pressing “I” and selecting LocRotScale?How do I create new objects from a mesh created from animation nodes?
Is it possible to map the firing of neurons in the human brain so as to stimulate artificial memories in someone else?
When handwriting 黄 (huáng; yellow) is it incorrect to have a disconnected 草 (cǎo; grass) radical on top?
How can a day be of 24 hours?
How could sorcerers who are able to produce/manipulate almost all forms of energy communicate over large distances?
My ex-girlfriend uses my Apple ID to login to her iPad, do I have to give her my Apple ID password to reset it?
Why is it a bad idea to hire a hitman to eliminate most corrupt politicians?
Pact of Blade Warlock with Dancing Blade
Notepad++ delete until colon for every line with replace all
In the UK, is it possible to get a referendum by a court decision?
Is this draw by repetition?
Obtaining database information and values in extended properties
How to prevent "they're falling in love" trope
In Bayesian inference, why are some terms dropped from the posterior predictive?
Salesman text me from his personal phone
Spam email "via" my domain, but SPF record exists
how do we prove that a sum of two periods is still a period?
Car headlights in a world without electricity
How could indestructible materials be used in power generation?
What's the meaning of "Sollensaussagen"?
Why were 5.25" floppy drives cheaper than 8"?
files created then deleted at every second in tmp directory
How can I deal with my CEO asking me to hire someone with a higher salary than me, a co-founder?
Why was Sir Cadogan fired?
Do creatures with a speed 0ft., fly 30ft. (hover) ever touch the ground?
How do I generate random cylinders, with random height on a predefined area?
Generate random ellipsoidsAlgorithm for random distribution of circles in a squareLink one object's property to another object's different propertyGenerate random ellipsoidsFinding the python command for an addon (and using it for “Sapling: Add Tree”)Union partially overlapping cubes without boolean modifierHow can I effectively import and texture a large height mapped terrain mesh?Calculate a surface by checking if meshes intersectAlign an array of vertices to vectors with pythonRandom placement of cylinders with random orientationsWhat is the correct python code for pressing “I” and selecting LocRotScale?How do I create new objects from a mesh created from animation nodes?
$begingroup$
I am using the following python script to generate an amount of random cylinders that do not overlap with each other:
import bpy
from random import random
from mathutils import Vector
cylinderRadius = 1
maxIterations = 500 # Max iterations to prevent while loop from running
forever
# min and max values for each axis for the random numbers
ranges =
'x' : 'min' : 7, 'max' : 10 ,
'y' : 'min' : 7, 'max' : 10 ,
'z' : 'min' : 0, 'max' : 0
# Generates a random number within the axis minmax range
randLocInRange = lambda axis: ranges[axis]['min'] + random() * (
ranges[axis]['max'] - ranges[axis]['min'] )
size = 50 # Number of cylinders
cylinders = [] # Cylinders coordinates list
loopIterations = 0
while len( cylinders ) < size and loopIterations < maxIterations:
loopIterations += 1
# Generate a random 3D coordinate
loc = Vector([ randLocInRange( axis ) for axis in ranges.keys() ])
if len( cylinders ) > 0:
# Search for overlapping points (within the cylinder radius radius)
overlappingPoints = [ p for p in cylinders if ( p - loc ).length < cylinderRadius * 2 ]
# if any found, skip this location
if overlappingPoints: continue
# Add coordinate to cylinder list
cylinders.append( loc )
# Add the first cylinder (others will be duplicated from it)
bpy.ops.mesh.primitive_cylinder_add( radius = cylinderRadius, location =
cylinders[0] )
cylinder = bpy.context.scene.objects['Cylinder']
# Add all other cylinders
for c in cylinders[1:]:
dupliCylinder = cylinder.copy()
dupliCylinder.location = c
bpy.context.scene.objects.link( dupliCylinder )
However, I do not get the 50 cylinders I am asking for when compiling. Does anyone have an idea on how to actually avoid setting a number of cylinders to be generated, but to fill in a pre-defined area (e.g. a square) with randomly placed cylinders, that do not overlap with each other, on it ? 
Thanks!
modeling python scripting
New contributor
Katja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
add a comment |
$begingroup$
I am using the following python script to generate an amount of random cylinders that do not overlap with each other:
import bpy
from random import random
from mathutils import Vector
cylinderRadius = 1
maxIterations = 500 # Max iterations to prevent while loop from running
forever
# min and max values for each axis for the random numbers
ranges =
'x' : 'min' : 7, 'max' : 10 ,
'y' : 'min' : 7, 'max' : 10 ,
'z' : 'min' : 0, 'max' : 0
# Generates a random number within the axis minmax range
randLocInRange = lambda axis: ranges[axis]['min'] + random() * (
ranges[axis]['max'] - ranges[axis]['min'] )
size = 50 # Number of cylinders
cylinders = [] # Cylinders coordinates list
loopIterations = 0
while len( cylinders ) < size and loopIterations < maxIterations:
loopIterations += 1
# Generate a random 3D coordinate
loc = Vector([ randLocInRange( axis ) for axis in ranges.keys() ])
if len( cylinders ) > 0:
# Search for overlapping points (within the cylinder radius radius)
overlappingPoints = [ p for p in cylinders if ( p - loc ).length < cylinderRadius * 2 ]
# if any found, skip this location
if overlappingPoints: continue
# Add coordinate to cylinder list
cylinders.append( loc )
# Add the first cylinder (others will be duplicated from it)
bpy.ops.mesh.primitive_cylinder_add( radius = cylinderRadius, location =
cylinders[0] )
cylinder = bpy.context.scene.objects['Cylinder']
# Add all other cylinders
for c in cylinders[1:]:
dupliCylinder = cylinder.copy()
dupliCylinder.location = c
bpy.context.scene.objects.link( dupliCylinder )
However, I do not get the 50 cylinders I am asking for when compiling. Does anyone have an idea on how to actually avoid setting a number of cylinders to be generated, but to fill in a pre-defined area (e.g. a square) with randomly placed cylinders, that do not overlap with each other, on it ? 
Thanks!
modeling python scripting
New contributor
Katja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
$begingroup$
Kind of a 2d version of this blender.stackexchange.com/a/42232/15543
$endgroup$
– batFINGER
2 days ago
$begingroup$
Do you want it in a triangle form as in your image or is it ok if it was in square form with a small gap in the middle. If so, try doing a loop for the y axis which has a loop for the x axis that adds 2 times the radius to that axis. To get 50 you would input 5 and 10 on the x and y values, giving you 5x10=50. Just giving a simpler approach...
$endgroup$
– Bigfoot Blondy
2 days ago
add a comment |
$begingroup$
I am using the following python script to generate an amount of random cylinders that do not overlap with each other:
import bpy
from random import random
from mathutils import Vector
cylinderRadius = 1
maxIterations = 500 # Max iterations to prevent while loop from running
forever
# min and max values for each axis for the random numbers
ranges =
'x' : 'min' : 7, 'max' : 10 ,
'y' : 'min' : 7, 'max' : 10 ,
'z' : 'min' : 0, 'max' : 0
# Generates a random number within the axis minmax range
randLocInRange = lambda axis: ranges[axis]['min'] + random() * (
ranges[axis]['max'] - ranges[axis]['min'] )
size = 50 # Number of cylinders
cylinders = [] # Cylinders coordinates list
loopIterations = 0
while len( cylinders ) < size and loopIterations < maxIterations:
loopIterations += 1
# Generate a random 3D coordinate
loc = Vector([ randLocInRange( axis ) for axis in ranges.keys() ])
if len( cylinders ) > 0:
# Search for overlapping points (within the cylinder radius radius)
overlappingPoints = [ p for p in cylinders if ( p - loc ).length < cylinderRadius * 2 ]
# if any found, skip this location
if overlappingPoints: continue
# Add coordinate to cylinder list
cylinders.append( loc )
# Add the first cylinder (others will be duplicated from it)
bpy.ops.mesh.primitive_cylinder_add( radius = cylinderRadius, location =
cylinders[0] )
cylinder = bpy.context.scene.objects['Cylinder']
# Add all other cylinders
for c in cylinders[1:]:
dupliCylinder = cylinder.copy()
dupliCylinder.location = c
bpy.context.scene.objects.link( dupliCylinder )
However, I do not get the 50 cylinders I am asking for when compiling. Does anyone have an idea on how to actually avoid setting a number of cylinders to be generated, but to fill in a pre-defined area (e.g. a square) with randomly placed cylinders, that do not overlap with each other, on it ? 
Thanks!
modeling python scripting
New contributor
Katja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
I am using the following python script to generate an amount of random cylinders that do not overlap with each other:
import bpy
from random import random
from mathutils import Vector
cylinderRadius = 1
maxIterations = 500 # Max iterations to prevent while loop from running
forever
# min and max values for each axis for the random numbers
ranges =
'x' : 'min' : 7, 'max' : 10 ,
'y' : 'min' : 7, 'max' : 10 ,
'z' : 'min' : 0, 'max' : 0
# Generates a random number within the axis minmax range
randLocInRange = lambda axis: ranges[axis]['min'] + random() * (
ranges[axis]['max'] - ranges[axis]['min'] )
size = 50 # Number of cylinders
cylinders = [] # Cylinders coordinates list
loopIterations = 0
while len( cylinders ) < size and loopIterations < maxIterations:
loopIterations += 1
# Generate a random 3D coordinate
loc = Vector([ randLocInRange( axis ) for axis in ranges.keys() ])
if len( cylinders ) > 0:
# Search for overlapping points (within the cylinder radius radius)
overlappingPoints = [ p for p in cylinders if ( p - loc ).length < cylinderRadius * 2 ]
# if any found, skip this location
if overlappingPoints: continue
# Add coordinate to cylinder list
cylinders.append( loc )
# Add the first cylinder (others will be duplicated from it)
bpy.ops.mesh.primitive_cylinder_add( radius = cylinderRadius, location =
cylinders[0] )
cylinder = bpy.context.scene.objects['Cylinder']
# Add all other cylinders
for c in cylinders[1:]:
dupliCylinder = cylinder.copy()
dupliCylinder.location = c
bpy.context.scene.objects.link( dupliCylinder )
However, I do not get the 50 cylinders I am asking for when compiling. Does anyone have an idea on how to actually avoid setting a number of cylinders to be generated, but to fill in a pre-defined area (e.g. a square) with randomly placed cylinders, that do not overlap with each other, on it ? 
Thanks!
modeling python scripting
modeling python scripting
New contributor
Katja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Katja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 2 days ago
Katja
New contributor
Katja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 2 days ago
KatjaKatja
62
62
New contributor
Katja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Katja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Katja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$begingroup$
Kind of a 2d version of this blender.stackexchange.com/a/42232/15543
$endgroup$
– batFINGER
2 days ago
$begingroup$
Do you want it in a triangle form as in your image or is it ok if it was in square form with a small gap in the middle. If so, try doing a loop for the y axis which has a loop for the x axis that adds 2 times the radius to that axis. To get 50 you would input 5 and 10 on the x and y values, giving you 5x10=50. Just giving a simpler approach...
$endgroup$
– Bigfoot Blondy
2 days ago
add a comment |
$begingroup$
Kind of a 2d version of this blender.stackexchange.com/a/42232/15543
$endgroup$
– batFINGER
2 days ago
$begingroup$
Do you want it in a triangle form as in your image or is it ok if it was in square form with a small gap in the middle. If so, try doing a loop for the y axis which has a loop for the x axis that adds 2 times the radius to that axis. To get 50 you would input 5 and 10 on the x and y values, giving you 5x10=50. Just giving a simpler approach...
$endgroup$
– Bigfoot Blondy
2 days ago
$begingroup$
Kind of a 2d version of this blender.stackexchange.com/a/42232/15543
$endgroup$
– batFINGER
2 days ago
$begingroup$
Kind of a 2d version of this blender.stackexchange.com/a/42232/15543
$endgroup$
– batFINGER
2 days ago
$begingroup$
Do you want it in a triangle form as in your image or is it ok if it was in square form with a small gap in the middle. If so, try doing a loop for the y axis which has a loop for the x axis that adds 2 times the radius to that axis. To get 50 you would input 5 and 10 on the x and y values, giving you 5x10=50. Just giving a simpler approach...
$endgroup$
– Bigfoot Blondy
2 days ago
$begingroup$
Do you want it in a triangle form as in your image or is it ok if it was in square form with a small gap in the middle. If so, try doing a loop for the y axis which has a loop for the x axis that adds 2 times the radius to that axis. To get 50 you would input 5 and 10 on the x and y values, giving you 5x10=50. Just giving a simpler approach...
$endgroup$
– Bigfoot Blondy
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
Test run, settings as posted Placed 66 of 68
Algorithm for random distribution of circles in a square
This simple script to place a number of
num_circlescircles with
radiusradiusin a refined rectangle (aligned to global axes) speficied
byxmin, xmax, yminandymaxby trying random locations until it not
colliding with an existing circle. This is certainly not the most
efficient way of doing it, but it should be fine for smaller problems
and if the relative area covered is not too high.
Test code after making a couple of edits to https://blender.stackexchange.com/a/89658/15543 to make it add cylinders of random height to a rectangle.
from mathutils import Vector, Matrix
from mathutils.noise import random, seed_set
import bpy
# Specify parameters
seed = 0
xmin = -10
xmax = 10
ymin = -10
ymax = 10
radius = 1
num_circles = 68
max_tries = 10000
max_height = 4
# Init
seed_set(seed)
sx = xmax - xmin - 2 * radius
sy = ymax - ymin - 2 * radius
xminm = xmin + radius
yminm = ymin + radius
existing_locations = []
sce = bpy.context.scene
bpy.ops.mesh.primitive_cylinder_add(
location=(0, 0, 0),
depth=1,
radius=radius)
ref_circle = bpy.context.object
ref_circle.data.transform(Matrix.Translation((0, 0, 0.5)))
# Loop
for i in range(num_circles):
j = 0
searchOn = True
while searchOn:
if j > max_tries:
print("Placed", i, "of", num_circles)
bpy.ops.object.select_all(action='DESELECT')
ref_circle.select_set(True)
bpy.ops.object.delete()
raise ValueError('Found no more room for another circle')
break
j += 1
new_location = (xminm + random() * sx,
yminm + random() * sy,
0)
for existing_location in existing_locations:
if (Vector(existing_location) - Vector(new_location)).length < 2 * radius:
break
else:
new_circle = ref_circle.copy()
new_circle.dimensions.z = max_height * random()
new_circle.location = new_location
sce.collection.objects.link(new_circle)
existing_locations.append(new_location)
searchOn = False
ref_circle.select_set(True)
bpy.ops.object.delete()
sce.update()
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
);
);
, "mathjax-editing");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "502"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Katja is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fblender.stackexchange.com%2fquestions%2f135808%2fhow-do-i-generate-random-cylinders-with-random-height-on-a-predefined-area%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Test run, settings as posted Placed 66 of 68
Algorithm for random distribution of circles in a square
This simple script to place a number of
num_circlescircles with
radiusradiusin a refined rectangle (aligned to global axes) speficied
byxmin, xmax, yminandymaxby trying random locations until it not
colliding with an existing circle. This is certainly not the most
efficient way of doing it, but it should be fine for smaller problems
and if the relative area covered is not too high.
Test code after making a couple of edits to https://blender.stackexchange.com/a/89658/15543 to make it add cylinders of random height to a rectangle.
from mathutils import Vector, Matrix
from mathutils.noise import random, seed_set
import bpy
# Specify parameters
seed = 0
xmin = -10
xmax = 10
ymin = -10
ymax = 10
radius = 1
num_circles = 68
max_tries = 10000
max_height = 4
# Init
seed_set(seed)
sx = xmax - xmin - 2 * radius
sy = ymax - ymin - 2 * radius
xminm = xmin + radius
yminm = ymin + radius
existing_locations = []
sce = bpy.context.scene
bpy.ops.mesh.primitive_cylinder_add(
location=(0, 0, 0),
depth=1,
radius=radius)
ref_circle = bpy.context.object
ref_circle.data.transform(Matrix.Translation((0, 0, 0.5)))
# Loop
for i in range(num_circles):
j = 0
searchOn = True
while searchOn:
if j > max_tries:
print("Placed", i, "of", num_circles)
bpy.ops.object.select_all(action='DESELECT')
ref_circle.select_set(True)
bpy.ops.object.delete()
raise ValueError('Found no more room for another circle')
break
j += 1
new_location = (xminm + random() * sx,
yminm + random() * sy,
0)
for existing_location in existing_locations:
if (Vector(existing_location) - Vector(new_location)).length < 2 * radius:
break
else:
new_circle = ref_circle.copy()
new_circle.dimensions.z = max_height * random()
new_circle.location = new_location
sce.collection.objects.link(new_circle)
existing_locations.append(new_location)
searchOn = False
ref_circle.select_set(True)
bpy.ops.object.delete()
sce.update()
$endgroup$
add a comment |
$begingroup$
Test run, settings as posted Placed 66 of 68
Algorithm for random distribution of circles in a square
This simple script to place a number of
num_circlescircles with
radiusradiusin a refined rectangle (aligned to global axes) speficied
byxmin, xmax, yminandymaxby trying random locations until it not
colliding with an existing circle. This is certainly not the most
efficient way of doing it, but it should be fine for smaller problems
and if the relative area covered is not too high.
Test code after making a couple of edits to https://blender.stackexchange.com/a/89658/15543 to make it add cylinders of random height to a rectangle.
from mathutils import Vector, Matrix
from mathutils.noise import random, seed_set
import bpy
# Specify parameters
seed = 0
xmin = -10
xmax = 10
ymin = -10
ymax = 10
radius = 1
num_circles = 68
max_tries = 10000
max_height = 4
# Init
seed_set(seed)
sx = xmax - xmin - 2 * radius
sy = ymax - ymin - 2 * radius
xminm = xmin + radius
yminm = ymin + radius
existing_locations = []
sce = bpy.context.scene
bpy.ops.mesh.primitive_cylinder_add(
location=(0, 0, 0),
depth=1,
radius=radius)
ref_circle = bpy.context.object
ref_circle.data.transform(Matrix.Translation((0, 0, 0.5)))
# Loop
for i in range(num_circles):
j = 0
searchOn = True
while searchOn:
if j > max_tries:
print("Placed", i, "of", num_circles)
bpy.ops.object.select_all(action='DESELECT')
ref_circle.select_set(True)
bpy.ops.object.delete()
raise ValueError('Found no more room for another circle')
break
j += 1
new_location = (xminm + random() * sx,
yminm + random() * sy,
0)
for existing_location in existing_locations:
if (Vector(existing_location) - Vector(new_location)).length < 2 * radius:
break
else:
new_circle = ref_circle.copy()
new_circle.dimensions.z = max_height * random()
new_circle.location = new_location
sce.collection.objects.link(new_circle)
existing_locations.append(new_location)
searchOn = False
ref_circle.select_set(True)
bpy.ops.object.delete()
sce.update()
$endgroup$
add a comment |
$begingroup$
Test run, settings as posted Placed 66 of 68
Algorithm for random distribution of circles in a square
This simple script to place a number of
num_circlescircles with
radiusradiusin a refined rectangle (aligned to global axes) speficied
byxmin, xmax, yminandymaxby trying random locations until it not
colliding with an existing circle. This is certainly not the most
efficient way of doing it, but it should be fine for smaller problems
and if the relative area covered is not too high.
Test code after making a couple of edits to https://blender.stackexchange.com/a/89658/15543 to make it add cylinders of random height to a rectangle.
from mathutils import Vector, Matrix
from mathutils.noise import random, seed_set
import bpy
# Specify parameters
seed = 0
xmin = -10
xmax = 10
ymin = -10
ymax = 10
radius = 1
num_circles = 68
max_tries = 10000
max_height = 4
# Init
seed_set(seed)
sx = xmax - xmin - 2 * radius
sy = ymax - ymin - 2 * radius
xminm = xmin + radius
yminm = ymin + radius
existing_locations = []
sce = bpy.context.scene
bpy.ops.mesh.primitive_cylinder_add(
location=(0, 0, 0),
depth=1,
radius=radius)
ref_circle = bpy.context.object
ref_circle.data.transform(Matrix.Translation((0, 0, 0.5)))
# Loop
for i in range(num_circles):
j = 0
searchOn = True
while searchOn:
if j > max_tries:
print("Placed", i, "of", num_circles)
bpy.ops.object.select_all(action='DESELECT')
ref_circle.select_set(True)
bpy.ops.object.delete()
raise ValueError('Found no more room for another circle')
break
j += 1
new_location = (xminm + random() * sx,
yminm + random() * sy,
0)
for existing_location in existing_locations:
if (Vector(existing_location) - Vector(new_location)).length < 2 * radius:
break
else:
new_circle = ref_circle.copy()
new_circle.dimensions.z = max_height * random()
new_circle.location = new_location
sce.collection.objects.link(new_circle)
existing_locations.append(new_location)
searchOn = False
ref_circle.select_set(True)
bpy.ops.object.delete()
sce.update()
$endgroup$
Test run, settings as posted Placed 66 of 68
Algorithm for random distribution of circles in a square
This simple script to place a number of
num_circlescircles with
radiusradiusin a refined rectangle (aligned to global axes) speficied
byxmin, xmax, yminandymaxby trying random locations until it not
colliding with an existing circle. This is certainly not the most
efficient way of doing it, but it should be fine for smaller problems
and if the relative area covered is not too high.
Test code after making a couple of edits to https://blender.stackexchange.com/a/89658/15543 to make it add cylinders of random height to a rectangle.
from mathutils import Vector, Matrix
from mathutils.noise import random, seed_set
import bpy
# Specify parameters
seed = 0
xmin = -10
xmax = 10
ymin = -10
ymax = 10
radius = 1
num_circles = 68
max_tries = 10000
max_height = 4
# Init
seed_set(seed)
sx = xmax - xmin - 2 * radius
sy = ymax - ymin - 2 * radius
xminm = xmin + radius
yminm = ymin + radius
existing_locations = []
sce = bpy.context.scene
bpy.ops.mesh.primitive_cylinder_add(
location=(0, 0, 0),
depth=1,
radius=radius)
ref_circle = bpy.context.object
ref_circle.data.transform(Matrix.Translation((0, 0, 0.5)))
# Loop
for i in range(num_circles):
j = 0
searchOn = True
while searchOn:
if j > max_tries:
print("Placed", i, "of", num_circles)
bpy.ops.object.select_all(action='DESELECT')
ref_circle.select_set(True)
bpy.ops.object.delete()
raise ValueError('Found no more room for another circle')
break
j += 1
new_location = (xminm + random() * sx,
yminm + random() * sy,
0)
for existing_location in existing_locations:
if (Vector(existing_location) - Vector(new_location)).length < 2 * radius:
break
else:
new_circle = ref_circle.copy()
new_circle.dimensions.z = max_height * random()
new_circle.location = new_location
sce.collection.objects.link(new_circle)
existing_locations.append(new_location)
searchOn = False
ref_circle.select_set(True)
bpy.ops.object.delete()
sce.update()
edited 2 days ago
answered 2 days ago
batFINGERbatFINGER
26.6k52876
26.6k52876
add a comment |
add a comment |
Katja is a new contributor. Be nice, and check out our Code of Conduct.
Katja is a new contributor. Be nice, and check out our Code of Conduct.
Katja is a new contributor. Be nice, and check out our Code of Conduct.
Katja is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Blender Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fblender.stackexchange.com%2fquestions%2f135808%2fhow-do-i-generate-random-cylinders-with-random-height-on-a-predefined-area%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
Kind of a 2d version of this blender.stackexchange.com/a/42232/15543
$endgroup$
– batFINGER
2 days ago
$begingroup$
Do you want it in a triangle form as in your image or is it ok if it was in square form with a small gap in the middle. If so, try doing a loop for the y axis which has a loop for the x axis that adds 2 times the radius to that axis. To get 50 you would input 5 and 10 on the x and y values, giving you 5x10=50. Just giving a simpler approach...
$endgroup$
– Bigfoot Blondy
2 days ago