So far I have been able to export vertices fine however I have been unable to correctly get the assigned vertex colors. I export a model of 8 vertices and it had 8 vertex positions however only 3 vertex colors.
objdata = bpy.data.meshes['Mesh']
for v in objdata.vertices:
vec = v.co*obj.matrix_world
f.write('%f,%f,%f
' %(vec.x, vec.y, vec.z))
for d in objdata.vertex_colors:
for c in d.data:
col = c.color1
f.write(("%d, %d, %d
" % (col[0],col[1],col[2])))
I think its because the color returned is for the faces not the verts, just not sure if there is a way to access the individual vertex colors. Or how to convert face colors to vertex colors etc. Thanks been stuck on this for a while any help would be greatly appreciated.
By the way: Pay attention to matrix * vector and vector * matrix mathematically different and too in my Blender 2.61!
Here a script vertexcolourGreyZcomponent which may be helpful! (Author sorry, not recorded)
# modify vertex painted 'Vertex Colors' palette
import bpy
import random
from mathutils import Color, Vector
def remap(current, lower_old, upper_old, lower_new, upper_new):
# error spreads must return difference between the values
old_tup = lower_old, upper_old
new_tup = lower_new, upper_new
# reusing a nicely coded Vector math utility :)
old_min = Vector((0.0, 0.0, lower_old))
old_max = Vector((0.0, 0.0, upper_old))
new_min = Vector((0.0, 0.0, lower_new))
new_max = Vector((0.0, 0.0, upper_new))
# fast and saves room!
spread_old = (old_max-old_min).length
spread_new = (new_max-new_min).length
factor_remap = spread_new / spread_old
current_vector = Vector((0.0, 0.0, current))
remap_temp =(current_vector-old_min).length
remapped = (remap_temp * factor_remap) + new_min[2]
# i think color values are clamped by blender, but it is
# good practice to not take this for granted in all cases.
if remapped < lower_new: return lower_new
if remapped > upper_new: return upper_new
# value seems alright!
return remapped
# Find upper and lower z values.
# there are many ways to skin this cat, but i'm going to be lazy
z_list = []
for i in bpy.context.active_object.data.vertices:
z_list.append(i.co.z)
z_list = sorted(z_list)
print('lower z',z_list[0])
print('upper z',z_list[len(z_list)-1])
lower_old = z_list[0]
upper_old = z_list[len(z_list)-1]
lower_new = 0.0
upper_new = 1.0
vertlist = bpy.context.active_object.data.vertices
facelist = bpy.context.active_object.data.faces
colorlist = bpy.context.active_object.data.vertex_colors[0].data
for i in range(len(colorlist)):
remapped = []
for element in range(4):
vertnum1 = facelist[i].vertices[element]
remap_1 = vertlist[vertnum1].co.z
remap_1 = remap(remap_1, lower_old, upper_old, lower_new, upper_new)
remapped.append(Color((remap_1,remap_1,remap_1)))
colorlist[i].color1 = remapped[0]
colorlist[i].color2 = remapped[1]
colorlist[i].color3 = remapped[2]
colorlist[i].color4 = remapped[3]
from bpy import *
from random import *
# select the currently selected mesh (be sure its a mesh object thought :P)
mesh = context.active_object.data
# mesh = data.objects["Mesh"].data # for using a predefined name
# vertex colour data
vertexColour = mesh.vertex_colors[0].data
faces = mesh.faces
# create random vertex colours
vColour = [(random(),random(),random()) for i in mesh.vertices]
#vertexColour = mesh.vertex_colors[0].data
faces = mesh.faces
# asign colours to verts
for i in range(len(faces)):
v = vertexColour[i]
f = faces[i].vertices_raw
v.color1 = vColour[f[0]]
v.color2 = vColour[f[1]]
v.color3 = vColour[f[2]]
v.color4 = vColour[f[3]]