why wont my if statment work?

if vertex.color == [0.0,1.0,0.0,1.0]: #wont enter the loop

if i print vertex i get
(Vector(0.0000,1.0000,0.0000,1.0000))

and to set the color in the documentation its
vertex.color == [0.0,1.0,0.0,1.0]

i hate to start a thread on something so simple but i have not had much luck with google on this one lol

What do you get when you print vertex.color?

I think it should work with :

from mathutils import Vector

if vertex.color == Vector((0,1,0,1)):

You’re comparing a vector to a list. This isn’t a supported comparison, so Python uses the default cmp method which simply compares the two objects’ memory addresses. Of course these will always be different in this case since you’re comparing two different objects.

There are two relatively easy solutions.

  1. Convert the list to a vector before comparing:
myColor = mathutils.Vector([0, 1, 0, 1])
if vertex.color == myColor:
    ...
  1. Manually compare each element of the vector and list:
for i, j in zip(vertex.color, [0, 1, 0, 1]):
    if i != j:
        break
else: # they're equal
   ...

Bear in mind that computers can’t represent decimal numbers exactly, so:


a = 0.1
b = a + a + a

if b == 0.3:
    '''This won't ever run'''

Instead of comparing them exactly, maybe you can compare something else. What exactly are you trying to accomplish?

With floating point vectors I usually test to see if the points are “close” to each other. Because as sdfgeoff says, not all numbers can be stored in a float variable accurately.


if (vertex.color - Vector([0.0,1.0,0.0,1.0])).magnitude < 0.001:
  # do something
  pass

pohatu23 your solution worked for me
Vector((0,1,0,1))
i did Vector(0,1,0,1) before and it didnt work for some reason dont really get why lol but thank all of you for your help :slight_smile:
i aslo tryed Vector([0, 1, 0, 1]) last night but it didnt work but now it does may i had some sort of type :\ thanks again.

Actually that will work in later python releases. It recognises that they are approximations of the same number

Sent from my Nexus 7 using Tapatalk 4

ok i lied lol this is what i had to do to get it to work
if [vertex.color[0],vertex.color[1],vertex.color[2],vertex.color[3]] == [0.0, 0.0, 1.0, 1.0]