Material color randomizer

Good afternoon. I am working on a script to randomize the color of facade elements. I was able to get the script to work. The next level of detail I am trying to add to the script is the ability for same color material to not be perpendicular or parallel to each other. In the example below I am trying to have similar colors be next to each other. Has anyone ever done this before? Thanks for the help.

You should have a look here, it’s right up your alley :slight_smile:

Random.list and adjacent families


@Jonathan.Olesen thanks for the reference post. The screenshot above is actually a wall that I broke down into parts and then selected model elements. I looked at the script you referred me to which seems to apply to curtain wall panels. I am not sure how to integrate the code from that script into mine to make the randomization sucessfull.

The category is irrelevant. The randomization is the important part. You just need to have the parts sorted and grouped correctly so that the randomization looks at adjacent pieces when “selecting” colors (or materials).

1 Like

@Nick_Boyts thats where I am running into the issue. When I input the python script it stops working.

Show us what you’re doing.

1 Like

I input the python script after the list cycle and keep getting an error message.

The python script takes two inputs, you’re only providing one.

Hi guys. I feel like I hit a wall here and am stuck. Can anyone give me a hand or some direction on the python script? Thank you all!

Show us what you currently have so we know where to help you.

1 Like

I was able to input the python script but I am still having same colors touching one another. See snips below. My apologies for low resolution my office just lost internet. I will be uploading the Revit and dynamo file one we are back online.

Please post your code as preformatted text </> so it can be used by other people.

Also, I don’t think your count_elem is what you’re expecting. Count @L1 will just return 1 count for every item…

@Nick_Boyts here is the python script

<import sys
sys.path.append(‘C:\Program Files (x86)\IronPython 2.7\Lib’)
from random import choice

count_elem = IN[0]
count_max = IN[1]
divisor = IN[2]

types = []
for i in range(0, count_max):

final = []
for panel in count_elem:
step = panel/divisor
if step < count_max:
step = panel

chosen = [choice(types)]
while len(chosen) < panel:
    elem = choice(types)
    if elem != chosen[-1]:
        if len(chosen) > step:
            if elem != chosen[-step]:

OUT = final

Here is the Revit and Dynamo files.


I don’t understand why you’re writing new code. The random color code will work as is, you just have to supply the elements in the correct order/structure and the materials to choose from.

I also don’t understand what your code is doing. Are you assigning the material then shuffling the elements around so there are no matching materials adjacent? That won’t work.

I did not re-write the python script. I am able to generate random colors for the selected elements no problem. Where I am running into issues is trying to make sure similar materials are not next to or above the same material.

Erik’s code is for a slightly more complex setup. I was assuming you’d use the one I posted above it.

import clr
from Autodesk.DesignScript.Geometry import *
from DSCore import *

dataEnteringNode = IN
panels = IN[0]
colors = IN[1]

panelColors = []
rowBelow = panels[0]

for row in panels:
	rowColors = []
	rowInt = []
	panelLeft = []
	for panel in row:
		panelBelow = rowBelow[p]
		adjacent = [panelLeft,panelBelow]
		ints = [x for x in range(0,len(colors)) if x not in adjacent]
		rInt = int(Math.Round(Math.Random(-0.5,len(ints)-0.5)))
		i = ints[rInt]
		panelLeft = i
	rowBelow = rowInt

OUT = panelColors

It will work with colors or materials (should work with anything really) as long as the elements are sorted in rows. You may have some issues with the empty spaces (that might be your issue with your current code as well). You will have to fill the empty spaces with null values so that the elements line up correctly.

Still running into a roadblock.

The elements (I assume they’re panels) need to be grouped by row. Otherwise there is no structure to tell how they’re ordered.

The elements being selected are parts of a wall not individual panels.