Assigning colors to elements which are of the same type

I have made an attempt to assign random colors to different types of structural framing;

This worked for assigning each structural framing element a unique color, as you can see here;

Unfortunately, this was not my aim. My aim was to override the graphics in view for each element of a specific type with a unique color. I think I am almost there, but I am a bit stuck on which direction to take.

Thanks in advance.

1 Like

Try to group your elements by type with a GroupByKey node where the keys input comes out of the Element.Type node (if needed, you will find indications about how to do that in this discussion, or try the search field)

I never heard about GroupByKey, I guess it somewhat the same principle as a Python Dictionary?

For now I seem to have solved my problem, but I still have the feeling I made it unnecessary complicated.

Here is what I did, I made sure I only assigned a color to unique types.

Then I used a Python script to check if the elements are equal to that type;


After some tweaking I could write them to the Element.OverridesColorsInView Node,

It doesn’t seem to work properly for Structural Framing in Structural Beam Systems

Here is what it looks like before I run the script;

And here is after (Remember, the aim was to assign a unique color to each element of a type of structural framing);

As you can see the Structural Framing in the Structural Beam Systems only assigned a color to one beam. I will do some more testing. :slightly_smiling_face:

edit: I ran my script several times and what I came across is the following;

  • colors are not uniqe to the types, because I generate random colors and assign them to the type it can occur that the same color is being used twice
  • I have no idea why some beams won’t change their color, it seems completely random. I added and removed some beams. Used structural framing or beam system, but I can’t seem to find a pattern.

I will revise my script entirely. :man_technologist:

I managed to make it work properly, :smile: there were some little errors in my script.

Here are the results. My script now colors the the types instead of every instance;

the revised script;

edit: The only thing now is I don’t know how to store or save the unique colors, there are new colors created everytime the script is run. Anybody any idea?:slightly_smiling_face:

edit #2: Is there also some way to prevent the projection lines from being overwritten by a color?

1 Like

You’re using a Math.Random node to generate the list of colors. Since those colors will randomly be selected on each run, you’re going to get random results on each run.

To get a more consistent approach try making a range from 1-755, spacing the values by the total count of beam types. A code block like this should do:


Then Chop the list into three parts - one for the all red group, one for the all green group, and one for the all blue group.
Next use an if statement statement to convert the numbers into assignable color values (less than 256). Something like this in a code block will get you started:

A>510? A/3 : A>255? A/2 : A

You can now wire the numbers into the color creation node accordingly. After that your colors will change uniformly based on the number of types in your job. It won’t have any yellows//purples/cyan colors, but those can be added by working with a 1530 deep list.

You can also use some list manipulation to shuffle the nodes in a seemingly random -reproducible way. tricks (list.remove and list.getitematindex nodes with a mid range number, feeding those into a list.create, and repeat the process a half dozen times), but I don’t know that there is any real value to that.


Thanks for your reply, however I don’t understand this part.

Next use an if statement statement to convert the numbers into assignable color values (less than 256). Something like this in a code block will get you started:

A>510? A/3 : A>255? A/2 : A

I understand that I can’t exceed the RGB values, but how can I know with which integer to divide my values in the list?
I have no experience with Code Blocks. So I made the first part in Python.

import clr
from Autodesk.DesignScript.Geometry import *
#The inputs to this node will be stored as a list in the IN variables.
number_of_types = IN[0]

output = []

color_range = range(1,755, int(number_of_types))

def chunks(l,n):
	for i in xrange(0, len(l), n):
		yield l[i:i + n]
output.append(chunks(color_range, (len(color_range)/3) ))

#Assign your output to the OUT variable.
OUT = output

This gives me three seperate lists like so;

So I am stuck on the part to convert this to RGB values. Thanks in advance. :slight_smile:

I can also foresee other problems with using the script as it is. :rofl:

Can I get a Copy of the DYN file please as this is somthing i need to investigate