Hello,
I’m trying to gradient a mesh with python script
I Found few scripts like this:
import bpy
from mathutils import Vector
flip = False
clearColors = True
obj = bpy.context.object
me = obj.data
#change this to change color, 0 = red, 1 = green, 2 = blue
value = 0
bbox = [Vector(point) for point in obj.bound_box]
max_z = max(v.z for v in bbox)
min_z = min(v.z for v in bbox)
diff = max_z - min_z
mesh_loops = me.loops
color_loops = me.vertex_colors.active.data
if clearColors:
for m_loop2, c_loop2 in zip(mesh_loops, color_loops):
z = me.vertices[m_loop2.vertex.index].co.z
c_loop2.color[0] = 0
c_loop2.color[1] = 0
c_loop2.color[2] = 0
for m_loop, c_loop in zip(mesh_loops, color_loops):
z= me.vetrices[m_loop.vertex_index].co.z
color = (z - min_z) / diff
if flip:
color = 1 - color
c_loop.color[value] = color
#c_loop.color[(value+1)%3] = color
The problem is I always get
“AttributeError: ‘NoneType’ object has no attribute ‘data’”
in “color_loops = me.vertex_colors.active.data”
It’s always me.vertex_colors.active.data
I don’t know what should I do.
Should I add something to the object for it to work?
I tried adding something to color_attributes or attributes but I don’t know what I should put there for the script to work…
Edit::
I added to Color Attributes: Attribute [Face corner->Byte Color]
And now the script goes
AttributeError: ‘MeshLoop’ object has no attribute ‘vertex’
The MeshLoopColorLayer holds the vertex_loop list inside its data attribute…
you can use (you may need to correct other uses of this variable):
color_loops = me.vertex_colors.active.data
or in the zip:
for m_loop2, c_loop2 in zip(mesh_loops, color_loops.data):
A good way to find your way how the blender data is structured, is to open the Python Console, and check the objects and their attributes directly there.
AttributeError: ‘MeshLoop’ object has no attribute ‘vertex’
Unfortunately I have zero knowledge about python… and it is probably first and last script I’ve gonna need from Python, so I would rather not spend year of my time to learn it for it
I saw the script working on 5:44
Also found few posts about gradient where similar scripts worked without problems.
In the code you posted, there are some errors… in your last AttributeError, you’re trying to access me.vertices[m_loop2.vertex.index].co.z but it really should be me.vertices[m_loop2.vertex_index].co.z.
I took all your code and fixed it, so this below should work now:
import bpy
from mathutils import Vector
flip = False
clearColors = True
obj = bpy.context.object
me = obj.data
#change this to change color, 0 = red, 1 = green, 2 = blue
value = 0
bbox = [Vector(point) for point in obj.bound_box]
max_z = max(v.z for v in bbox)
min_z = min(v.z for v in bbox)
diff = max_z - min_z
mesh_loops = me.loops
if not me.vertex_colors.active:
me.vertex_colors.new(name="Color")
color_loops = me.vertex_colors.active.data
if clearColors:
for m_loop2, c_loop2 in zip(mesh_loops, color_loops):
z = me.vertices[m_loop2.vertex_index].co.z
c_loop2.color[0] = 0
c_loop2.color[1] = 0
c_loop2.color[2] = 0
for m_loop, c_loop in zip(mesh_loops, color_loops):
z= me.vertices[m_loop.vertex_index].co.z
color = (z - min_z) / diff
if flip:
color = 1 - color
c_loop.color[value] = color