How to Randomly replace Blocks?

So here is my problem:
I have a 290 blocks (facade panels) named Block A already present at their final positions in the model.

Now I would like to replace a certain percentage or fixed number of them with other Blocks called B and C (E.g. Block A: 30%, Block B 50%, Block C20%)

The selection should be randomized, ideally with a seed number so I would be able to generate different solutions easily.

I searched the archive, but I did not find anything there…

Can someone give me a hand?

Thanks in Advance

Andreas

Are you working with Rhino 8?

yes I do :wink:

Get the total number of block planes.

In Grasshopper, duplicate every block as many times as you have planes.

Random reduce in each set of blocks

place the blocks

If you can get it to work, post your files.

1 Like

Here is a simple python script to do this for you.

import rhinoscriptsyntax as rs
import random

def RunScript():
    # Get the objects
    instances=rs.GetObjects("Select blocks to replace", rs.filter.instance, preselect=True)
    if not instances: return
    
    block_A = rs.GetObject("Select first replacement block", rs.filter.instance)
    if not block_A: return
    
    block_B = rs.GetObject("Select second replacement block", rs.filter.instance)
    if not block_B: return
    
    # Turn off redraw to speed up the process a lot
    rs.EnableRedraw(False)
    # Find block names
    block_A_name = rs.BlockInstanceName(block_A)
    block_B_name = rs.BlockInstanceName(block_B)
    
    # Define variables for percentage of each replacement (0.2 = 20%)
    var_A = 0.2
    var_B = 0.3
    
    # Check random number against variables 
    for instance in instances:
        rnd = random.random()
        
        if rnd < var_A:
            xform = rs.BlockInstanceXform(instance)
            rs.InsertBlock2(block_A_name, xform)
            rs.DeleteObject(instance)
        
        elif rnd > 1-var_B:
            xform = rs.BlockInstanceXform(instance)
            rs.InsertBlock2(block_B_name, xform)
            rs.DeleteObject(instance)
    
    # Turn on redraw again
    rs.EnableRedraw(True)

RunScript()

And the test file I used:
Block replace test file.3dm (569.3 KB)

1 Like

Thank you so much both Martin and Holo,

You guys rock!

I really wish I could offer something in return…

Do you think something like this might work?

Thanks again

Andreas

1 Like

Using the random component to select one two or 3 options can be done this way also:

2 Likes

Thanks Scott,

I think I have to hone my GH skills, I don´t understand the icons (yet)
My knowledge is very basic.

Back to school I supose :wink:

1 Like

Run random into a greater then component to see if the random number was higher or lower than .5 or whatever the percentage of one option vs another is. That will create a true/false list that can be used in a pattern component that switches between the two options above.

1 Like

than

:zipper_mouth_face:

1 Like

My pleasure and thank you for wanting to buy us a coffee :smiley:
I liked the idea and made an account: https://buymeacoffee.com/jorgholo
So fun!

enjoy :wink:

1 Like

Wohoo! You made my day :smiley:
image