1
$\begingroup$

I have some scientific data that I am importing into blender for rendering. I'm trying to make a Surface plot plot with a color code.

Scavenging around I found a python script to produce the mesh. I just cannot figure out how to give every surface a different color depending on its height, or even a material.

Here is the code. Any help would be appreciated:

import bpy
import math
import numpy as np

x,y=np.meshgrid(np.arange(-100,100),np.arange(-100,100))
r=np.sqrt(x*x+y*y)
values=np.sin(r/10.)
# clear mesh and object
for item in bpy.context.scene.objects:
    if item.type == 'MESH':
        bpy.context.scene.objects.unlink(item)
for item in bpy.data.objects:
    if item.type == 'MESH':
        bpy.data.objects.remove(item)
for item in bpy.data.meshes:
    bpy.data.meshes.remove(item)
for item in bpy.data.materials:
    bpy.data.materials.remove(item)

# mesh arrays
verts = []
faces = []

# mesh variables
numX = np.shape(values)[0]
numY = np.shape(values)[1]
scale=2. #how large you want to make the field in x y
# fill verts array
for i in range (0, numX):
    for j in range(0,numY):
        # nomalize range
        x = 2*(i/numX-1/2)
        y = 2*(j/numY-1/2)


        z = values[i,j]

        vert = (x*scale,y*scale,z)
        verts.append(vert)

# fill faces array
count = 0
for i in range (0, numY *(numX-1)):
    if count < numY-1:
        A = i
        B = i+1
        C = (i+numY)+1
        D = (i+numY)
        face = (A,B,C,D)
        faces.append(face)
        count = count + 1
    else:
        count = 0

# create mesh and object
mesh = bpy.data.meshes.new("wave")
object = bpy.data.objects.new("wave",mesh)

# set mesh location
object.location = (0,0,0)
bpy.context.scene.objects.link(object)

# create mesh from python data
mesh.from_pydata(verts,[],faces)
mesh.update(calc_edges=True)
$\endgroup$
2
  • $\begingroup$ Thank you. What did you use to render ? In cycles I do not see any change for the colors $\endgroup$
    – Mathusalem
    Oct 27 '16 at 10:34
  • $\begingroup$ Use a material if you can. I read this too fast and thought you wanted to set a Vertex Color layer. $\endgroup$
    – zeffii
    Oct 27 '16 at 11:56
2
$\begingroup$

I usually do it with the following node setup: enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.