3
$\begingroup$

Is there a way to assign a distinct "random" color to a mesh based on the string-value of one of its "Custom Properties"? I am most interested in having this color visible in the 3D viewport to distinguish objects based on their properties while editing a scene.

I'm not sure whether the solution should be based on shading or not. The viewport "solid mode" view with "random" color assignment is close to what I want except that it chooses a separate color for each object rather than for a group with the same property.

For example, supposing that I would have a large number of meshes each with a property "country" and I wanted each mesh with value "australia" to be one color, each with value "usa" to be another color, and so on.

$\endgroup$
2
  • $\begingroup$ Hello, would it be OK if you represented each country with a number instead of a string of characters ? $\endgroup$
    – Gorgious
    Jan 29, 2021 at 13:51
  • $\begingroup$ That would be workable yep. I'd probably then have two attributes, "country" and "country_num", that correspond with one for human consumption and one for internal use. $\endgroup$ Jan 29, 2021 at 14:08

1 Answer 1

6
$\begingroup$

You can do it with the latest Blender Version (2.92+) and custom properties :

  1. Download the latest version (At least 2.92+, which is currently in Beta but should be officially released pretty soon).
  2. Select your object, and create your custom property, named "country".
  3. In the value field, you can input a string of characters. Don't forget to press OK to validate your change.

enter image description here

  1. Create another property, let's call it hash. Keep the min and max between 0 and 1.

enter image description here

  1. Right click in the field and choose Add Driver. Delete the target variable, check Self and type hash(self["country"])%1000/1000. This will generate a (kinda) random number between 0 and 1 depending on the string inside the country property. The hash provides the same output if 2 inputs are the same.

enter image description here

  1. Important : You have to enable python scripts globally for this to work. Go to Edit > Preferences > Save & Load and enable Auto run python scripts. You can add your Downloads folder for security, that way downloaded files won't execute custom scripts automatically.

enter image description here

  1. Add a material to your object. Use this layout :

enter image description here

Add an Attribute node set to Object type, and input the hash custom property name. Link it to a Combine HSV node to create a random hue.

  1. Result :

enter image description here

$\endgroup$
9
  • $\begingroup$ Thank you for the fantastic answer! This seems ideal. The hash presumably gives me consistent color choices for the same string which seems like a big plus (even though it may not choose the most visually distinct colours for the data I happen to use.) Good trade-off. $\endgroup$ Jan 29, 2021 at 16:37
  • $\begingroup$ You could use a different method than hash I guess :) You can also add another property and use it in the shader as an offset to the hash value to fine-tune it $\endgroup$
    – Gorgious
    Jan 29, 2021 at 17:36
  • $\begingroup$ I suppose that one could imagine a future version of Blender where there is a node that takes a string property directly and outputs its hash value, so that it would not be necessary to manually create the hash properties with scripts (e.g. if there would be many different properties to hash.) That's assuming nodes can convert from strings to numbers? $\endgroup$ Jan 30, 2021 at 9:50
  • $\begingroup$ Maybe an elegant solution would even be for the Converter->Math node to have a "Conversion" function called Hash that hashes a string input? $\endgroup$ Jan 30, 2021 at 11:41
  • $\begingroup$ I looked for something like that at first, but internally I don't think you can even feed string data into the shader pipeline. It only works with float Vectors of size 4 as far as I know. Not a developer though :) $\endgroup$
    – Gorgious
    Jan 30, 2021 at 11:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .