# Material color randomizer

**URL:** <https://forum.dynamobim.com/t/material-color-randomizer/23013>\
**Category:** DesignScript\
**Created:** [June 21, 2018, 7:12pm UTC](https://forum.dynamobim.com/t/material-color-randomizer/23013 "2018-06-21T19:12:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Glenn\_Mutz](https://avatars.discourse-cdn.com/v4/letter/g/7ea924/32.png) [@Glenn\_Mutz](https://forum.dynamobim.com/u/Glenn_Mutz)\
**Post date:** [June 21, 2018, 7:12pm UTC](https://forum.dynamobim.com/t/material-color-randomizer/23013/1 "2018-06-21T19:12:54Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/7/5/7512ddf1681f37bde128d2f8ddcd42086dcbce21.png)

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/0/f/0f926f5a33c2febdd5d02449d512ed367c2745ea.png)

---

<div class="post-metadata">

**Author:** ![Jonathan.Olesen](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jonathan.olesen/32/44242_2.png) [@Jonathan.Olesen](https://forum.dynamobim.com/u/Jonathan.Olesen)\
**Post date:** [June 21, 2018, 7:20pm UTC](https://forum.dynamobim.com/t/material-color-randomizer/23013/2 "2018-06-21T19:20:03Z")

</div>

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

[Random.list and adjacent families](https://forum.dynamobim.com/t/random-list-and-adjacent-families/21345?u=jonathan.olesen)

---

<div class="post-metadata">

**Author:** ![Glenn\_Mutz](https://avatars.discourse-cdn.com/v4/letter/g/7ea924/32.png) [@Glenn\_Mutz](https://forum.dynamobim.com/u/Glenn_Mutz)\
**Post date:** [June 22, 2018, 11:29am UTC](https://forum.dynamobim.com/t/material-color-randomizer/23013/3 "2018-06-22T11:29:39Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [June 22, 2018, 1:25pm UTC](https://forum.dynamobim.com/t/material-color-randomizer/23013/4 "2018-06-22T13:25:17Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Glenn\_Mutz](https://avatars.discourse-cdn.com/v4/letter/g/7ea924/32.png) [@Glenn\_Mutz](https://forum.dynamobim.com/u/Glenn_Mutz)\
**Post date:** [June 22, 2018, 3:19pm UTC](https://forum.dynamobim.com/t/material-color-randomizer/23013/5 "2018-06-22T15:19:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [June 22, 2018, 3:32pm UTC](https://forum.dynamobim.com/t/material-color-randomizer/23013/6 "2018-06-22T15:32:20Z")

</div>

Show us what you’re doing.

---

<div class="post-metadata">

**Author:** ![Glenn\_Mutz](https://avatars.discourse-cdn.com/v4/letter/g/7ea924/32.png) [@Glenn\_Mutz](https://forum.dynamobim.com/u/Glenn_Mutz)\
**Post date:** [June 22, 2018, 3:48pm UTC](https://forum.dynamobim.com/t/material-color-randomizer/23013/7 "2018-06-22T15:48:59Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/0/c/0c16cb35b608f3349b6f96a38b448cbe5eb2a758.png)

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [June 22, 2018, 3:50pm UTC](https://forum.dynamobim.com/t/material-color-randomizer/23013/8 "2018-06-22T15:50:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Glenn\_Mutz](https://avatars.discourse-cdn.com/v4/letter/g/7ea924/32.png) [@Glenn\_Mutz](https://forum.dynamobim.com/u/Glenn_Mutz)\
**Post date:** [June 26, 2018, 11:17am UTC](https://forum.dynamobim.com/t/material-color-randomizer/23013/9 "2018-06-26T11:17:30Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [June 26, 2018, 12:04pm UTC](https://forum.dynamobim.com/t/material-color-randomizer/23013/10 "2018-06-26T12:04:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Glenn\_Mutz](https://avatars.discourse-cdn.com/v4/letter/g/7ea924/32.png) [@Glenn\_Mutz](https://forum.dynamobim.com/u/Glenn_Mutz)\
**Post date:** [June 26, 2018, 12:49pm UTC](https://forum.dynamobim.com/t/material-color-randomizer/23013/11 "2018-06-26T12:49:32Z")

</div>

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.

 ![20180626_084634](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/0/1/01e2bcdba123e412c144ecebddd23d56869e2224.jpg)

 ![20180626_084650](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/9/a/9a649d37905d4e020eb14061b35183983b9f2391.jpg)

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [June 26, 2018, 3:12pm UTC](https://forum.dynamobim.com/t/material-color-randomizer/23013/12 "2018-06-26T15:12:14Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Glenn\_Mutz](https://avatars.discourse-cdn.com/v4/letter/g/7ea924/32.png) [@Glenn\_Mutz](https://forum.dynamobim.com/u/Glenn_Mutz)\
**Post date:** [June 26, 2018, 6:03pm UTC](https://forum.dynamobim.com/t/material-color-randomizer/23013/13 "2018-06-26T18:03:16Z")

</div>

@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):  
types.append(i)

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]:
                chosen.append(elem)
        else:
            chosen.append(elem)
final.append(chosen)

```

OUT = final

>

---

<div class="post-metadata">

**Author:** ![Glenn\_Mutz](https://avatars.discourse-cdn.com/v4/letter/g/7ea924/32.png) [@Glenn\_Mutz](https://forum.dynamobim.com/u/Glenn_Mutz)\
**Post date:** [June 26, 2018, 6:04pm UTC](https://forum.dynamobim.com/t/material-color-randomizer/23013/14 "2018-06-26T18:04:07Z")

</div>

Here is the Revit and Dynamo files.

[MATERIAL RANDOMIZATION.rvt](https://forum.dynamobim.com/uploads/short-url/qllLBicWuzS3CjLPPNESm64hMUV.rvt) (728 KB)  
[BUILDING FACADE COLOR RANDOMIZER.dyn](https://forum.dynamobim.com/uploads/short-url/tjlgOIpf2pwwui5lYfBqgvhSzXW.dyn) (14.1 KB)

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [June 26, 2018, 6:30pm UTC](https://forum.dynamobim.com/t/material-color-randomizer/23013/15 "2018-06-26T18:30:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Glenn\_Mutz](https://avatars.discourse-cdn.com/v4/letter/g/7ea924/32.png) [@Glenn\_Mutz](https://forum.dynamobim.com/u/Glenn_Mutz)\
**Post date:** [June 26, 2018, 6:35pm UTC](https://forum.dynamobim.com/t/material-color-randomizer/23013/16 "2018-06-26T18:35:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [June 26, 2018, 6:40pm UTC](https://forum.dynamobim.com/t/material-color-randomizer/23013/17 "2018-06-26T18:40:21Z")

</div>

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

```
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
clr.AddReference('DSCoreNodes')
from DSCore import *

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

panelColors = []
rowBelow = panels[0]

for row in panels:
	rowColors = []
	rowInt = []
	panelLeft = []
	p=0
	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]
		rowInt.append(i)
		rowColors.append(colors[i])
		panelLeft = i
		p=p+1
	panelColors.append(rowColors)
	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.

---

<div class="post-metadata">

**Author:** ![Glenn\_Mutz](https://avatars.discourse-cdn.com/v4/letter/g/7ea924/32.png) [@Glenn\_Mutz](https://forum.dynamobim.com/u/Glenn_Mutz)\
**Post date:** [June 26, 2018, 7:00pm UTC](https://forum.dynamobim.com/t/material-color-randomizer/23013/18 "2018-06-26T19:00:32Z")

</div>

Still running into a roadblock.

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/f/2/f231294bfe7ab60946d302d06e2bfb80111e61cf.png)

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [June 26, 2018, 7:02pm UTC](https://forum.dynamobim.com/t/material-color-randomizer/23013/19 "2018-06-26T19:02:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Glenn\_Mutz](https://avatars.discourse-cdn.com/v4/letter/g/7ea924/32.png) [@Glenn\_Mutz](https://forum.dynamobim.com/u/Glenn_Mutz)\
**Post date:** [June 26, 2018, 7:08pm UTC](https://forum.dynamobim.com/t/material-color-randomizer/23013/20 "2018-06-26T19:08:53Z")

</div>

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

[Next page](https://forum.dynamobim.com/t/material-color-randomizer/23013.md?page=2)
