Reduce number of colors and bake with material

Hello everyone,

I’ve created a script that takes an image, pixelates it, and assigns a corresponding color to a mesh cube. After that, I run a Python command to generate and apply a material with the color of each block.

The problem I’m facing is that this process creates around 7,000 materials, which makes it extremely slow to adjust properties like reflection or transparency, even when batch-editing (e.g., 300 at a time).

One workaround I thought of, though my Grasshopper skills are quite limited, is:

  1. Reducing the number of colors by replacing very similar ones while still keeping some variation.

  2. Generating materials only for this reduced color set and assigning them to the corresponding cubes directly in Grasshopper.

Does this approach make sense? Or is there a better way to streamline this workflow?

Thanks in advance,
Oscar

Replace vertex colors with per-object materials.py (279 Bytes)

Facade 2.gh (19.3 MB)

You could ‘clump’ colors together based on distance. Here is a potential implementation of that:
Replace vertex colors with per-object materials - clumping - by jesterKing.py (2.9 KB)

You’ll find that I am using RhinoCommon API directly. Also I made this for use with Python 3 in Rhino 8. It should be relatively simple to adjust for Python 2, but I haven’t tested. Just run _ScriptEditor, load the script and run it.

Steps the script takes:

  1. find all mesh objects
  2. build a list of RGB triplets from all mesh objects vertex colors (the first, as you did)
  3. find the color clumps
  4. create materials for each clump, picking the first color in the clump
  5. assign materials to mesh objects based on

With distance 5 the script generates 268 colors. Since the script doesn’t remove the vertex colors I have a screenshot with Raytraced and Rendered modes side by side. It allows you to see the original colors against the new color set:

However, there is a trick with a hidden material that you can use if your goal is to render with Raytraced / Rhino Render in Rhino 7 or later. That will need only one material assigned to all objects, which then will render the vertex colors.

Run TestShowPrivateContent, the command-line history will tell you “Private content will be displayed”. Then in the materials panel add a new material, search for Cycles Vertex Color. Select all your objects that have vertex colors and assign this material. Then switch to Raytraced. It will look like this: