Python script to read Material color

Just wondering a quick question.

If I have many Objects with many different Materials, is there a quick and easy script that able to:

  1. Check and grab the Materials color, store that RGB value, convert it into HSB, get just the Brightness.
  2. Pass that Brightness value and extrude that object (if Poly) based on the Brightness/Luminance value.

Thank you.

getting all materials is like this:[:]

(later more)

My Python language is still a bit messy.

#with all objects selected (assume they are all poly mesh with single face and flat)
#go in iteration
for sel in selected:
#get material color
#convert value to just luminance
#extrude face based on luminance value (between 0 and 1)
#keep looping until all faces are extruded based on the color.

If you tell me what your ‘luminance’ is in Blender I could build a loop for you :wink:

missing info: in what direction you want to extrude a face ?!

Sound like you may be able to achieve this with a displace modifier and a texture. Any reason why you are going the scripting route?

Here is some info on RGB to luminance.

Thanks for all the inputs.

Ok, I am not 100% sure where I am going with this.

Basically I imagine if I could bring in some 2D vector Shapes generated procedurally using NodeBox 2 (it could be city blue print map). NodeBox exports out PDF, which can be converted into bunch of vector curve shapes (via Inkscape) into Blender. With those imported 2D Curves Shapes, I was again wondering if I could extrude them using script, just based on different Materials, perhaps their luminance.

@Atom, you are right, that’s also what I had in mind.
@PKHG, I would think Luminance will just just the B in HSB color ring.

It would be a lot nicer of course to do everything inside Blender.

The most basic thing, for example I have 2D image on a plane. Would it be possible via scripting to sample color per UV into grid of cubes? I sort of able to do this in other 3D package (Houdini actually).

My 1st guess originally is: Blender Particles can sample color, but unfortunately the color does not get transferred into the Instance. Not sure if RE:Ticular add-on can sort of does this. Make instance alive and transfer each particle color into the instances.

Before getting too complicated, I think the question is:
If I have bunch of separate shapes with different Materials, I am wondering what kind of script helps to extrude each of those shapes based on the Material properties. Luminance or anything.

Is it tricky to script? It is more of curiosity than anything.

Thanks again.

not tricky, this should extrude first face in its normal direction

import bpy
sce = bpy.context.scene
sel = [o for o in bpy.context.selected_objects if o.type == 'MESH']

for obj in sel:
    # activate object = obj
    if obj == sel[0]:
        # set face selection mode once
        bpy.context.tool_settings.mesh_select_mode = [False, False, True]
    # select some face in object mode
    for f in = False[0].select = True
    # get vector and hsv value
    nor =[0].normal
    v =[0].diffuse_color.hsv[2]
    # go edit mode and extrude / translate first face along normal
    bpy.ops.mesh.extrude_region_move(TRANSFORM_OT_translate={"value":nor * v * 5})

  • now sets mode to face select
  • and it extrudes from hsv ‘value’

I think, I would not have come with such a nice solution!

Thanks so much, for showing the way again ~ :cool:

It definitely works with the extrusion. I will study this script now. I am starting to like Blender scripting :smiley:


Would it be possible to extrude all faces from the selected mesh, instead of just 1st face?

sure, set the select faces line to True like this
for f in = True
and delete:[0].select = True as it is already selected

as svg imports as curves you can do similar script to extrude curves
btw, nodebox2 is big fun…

I see you prepared for that :slight_smile:

This is starting to get cool. Probably very basic, but nice start, I supposed.

OK, I should try to write similar script to extrude curves. At the moment, I will just use script I learned from PKHG to quickly convert all these Curves.

import bpy

Great, you tried NodeBox2! I am glad you also find it fun. I studied it for a while years ago, before I moved to Houdini. NodeBox2 has just been updated too and it has more development going now.

Indeed: big fun!
Something like that direct in Blender and at once 3D ??!!!


Did you create that in Blender or NodeBox? The spectrum color is not from Dynamic Paint, is it?

Hmm, forgotton … but found back: in NodeBox!

I see, thanks :slight_smile: Imagine python script that can bind NodeBox and Blender in realtime.

it is not so easy, nodebox exports pdf, pdf is read in inkscape inkscape exports as svg Blender imports svg …

So if nodebox could export at once svg??

NodeBox2 only exports PDF, yeah, it’s been like this for the last few years. Maybe one day.