Setting Vertex Colors from Python

On March 26, 2008, in Tutorials, by Admin

Here is the complete script. It shows how to change a few of the vertices in a face of a cube.

import Blender
from Blender import *

These first few lines import the Blender Module.

I started with a basic Blender cube. Space Bar – Add – Cube. I left the default name for the mesh as

mesh = Blender.Mesh.Get('Cube')

This line assigns our mesh to the variable ‘mesh’. Make sure you replace ‘Cube’ with the name of your Blender

The name of your mesh will be located here. With your object selected, switch to ‘Editing (F9)‘.
You’ll see it listed under the Links and Materials tab.

colnames = mesh.getColorLayerNames()

The next line adds a new vertex color layer and names it ‘layer1′. The name can be anything. You’ll see this name
updated under your Mesh tab while you are in Editing mode with your object once the script completes.

The next line gets the list of all of the vertex color layers and assigns them to the variable colnames.
This line is not necessary to run the script, since we are only working with the newly created layer and
there are no other layers at this time.

print 'vertexcolors =',
print mesh.vertexColors

These next two lines simply print out the current value of the vertexColors variable. This is a boolean value which
shows whether vertex colors are active or not for this mesh. It will equal 0 or 1. 1 meaning vertex colors are
active. When we created our new Color Layer, this value is automatically changed from 0 to 1.

These lines are not needed for this script, but may be useful if you need to know if a mesh has
vertex colors active before you begin to work with it.

faces = mesh.faces

This next line assigns all of the faces in our mesh to the variable faces.

for d in  faces[0].verts:
	print d
for d in  faces[0].col:
	print d

This next few lines, again, are not necessary for the script to run, but they show you
a sample of what you have to work with in your mesh. All of the faces in a mesh are
numbered starting with 0. A mesh with 4 faces will be numbered from 0 – 3. The lines
above print out a sample from face # 0 only.

for d in  faces[0].verts:
	print d

These lines print out a list in the console of all of the vertices in face #0.

for d in  faces[0].col:
	print d

These lines will print out all of the color values of each vertex in face #0.
Values are from 0 – 255. (0 = 0% and 255 = 100%). The values will be for r, g, b and a.

r = red
b = blue
g = green
a = alpha

The default color values are 255 which means each vertex will be white.

faces[0].col[0].r = 0
faces[0].col[1].g = 0
faces[0].col[2].b = 0

These lines alter the default values for 3 vertices from 255 to 0.

col[0].r represents the red color value from vertex # 0.
col[1].g represents the green color value from vertex # 1.
col[2].b represents the blue color value from vertex # 2.

The result on face #0 looks like this. You must be in the Textured view mode (ALT-Z)
in order to see the vertex coloring.

In order to see vertex coloring in your rendered Image, assign a new material to your object.

Under the Material tab, click on the button labeled VColPaint. Your vertex coloring will now
show up in your rendered images.

Setting Vertex Colors in Python

Good luck with your modeling.



Comments are closed.

muzica noua
filme Online
filme online gratis
seriale online
filme noi
muzica noua
desene animate online
jocuri noi online
jocuri online
filme bune
filme online
filme online hd
filme online subtitrate
filme noi