How to apply emission shader to an object?

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
marcheenek
Posts: 0
Joined: Sun Dec 30, 2012 11:45 pm
Location: Chicago, IL

How to apply emission shader to an object?

Post by marcheenek » Fri May 31, 2013 10:21 pm

I have just started learning Python scripting for Blender and I'm currently exploring its possibilities for data visualization.
I want to visualize a massive database, with each data point being a cube with a value expressed using a color intensity.

Each data point can have either positive or negative value, I thought I would use Emission shader to visualize the data. I would like to assign green color to a negative value, red to a positive, with the value itself being strength of the emission shader.

While I know how to import CSV data and create a cube for each datapoint, I couldn't find the way to color code the data or even create the shader. All the tutorials I found describe creating materials in BI, but not Cycles using nodes.

Here's some code I'm starting with (main source is Blender Sushi)

Code: Select all

import bpy
import csv
import math
file = csv.reader(open('\\Documents and Settings\user\Desktop\data_for_blender.csv', newline=''), delimiter=',')


numLines = 0


for row in enumerate(file):
    numLines = numLines +1

curRow = []
for idx, row in enumerate(file):
    if idx<numLines:
        curRow=row

#add cube
bpy.ops.mesh.primitive_cube_add(view_align=False, enter_editmode=False, location=(0,0,0), rotation=(0,0,0))

#cursor location
saved_location = bpy.context.scene.cursor_location.copy()
bpy.context.scene.cursor_location = (0.0, 0.0, -1.0)
bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
bpy.context.scene.cursor_location = saved_location

#resize cubes
bpy.ops.transform.resize(value=(0.5, 0.5, 0.5))

#move cubes 
#at this point cubes are just located along a line, but there will be other spatial organization later      
bpy.ops.transform.translate(value=(5, idx*2,0))

#create material/shader here
#read value from the second column:=

myValue= int(curRow[1])

#absolute value for shader strength
myabsValue= math.fabs(myValue)
myColor=''
#PSEUDOCODE
#if myValue < 0:
# myColor = 'green'
#if myValue > 0:
# myColor = 'red'

#[... assign material with emission shader with myColor for color and myabsValue for emission strength ...]

marcheenek
Posts: 0
Joined: Sun Dec 30, 2012 11:45 pm
Location: Chicago, IL

Post by marcheenek » Mon Jun 03, 2013 4:21 pm

So, I just realized that for what I'm attempting I would have to create a separate material for each object (~31 000 total objects), which is ridiculous.

I would give up continuous scale and approach this by creating let's say 10 materials for each color, with varying values of emission strength (1 through 10) and assign a particular material to an object based on a value from CSV file, normalized to 1-10 scale.

So, what if I just simply create those materials manually? Can I apply them in the script?

Or, maybe create 10 cubes, with materials applied already and duplicate them using the script?

So many ideas, so little skills :(

Post Reply