Blender.Mathutils Class types

Can anyone tell me how to test if something is a Blender Vector (from Mathutils?)

I’m writing a custom sequence type which accepts Blender Vectors as slice indices.

Unfortunately, I can’t get type testing to recognize Blender.Mathutils.Vector as a class:


import Blender
import bpy
from Blender.Mathutils import *

x = Vector(0, 0, 1)
if isinstance(x, type(Vector)):
    print 'true'

The above raises an error:

Traceback (most recent call last):
  File "Text", line 6, in <module>
NameError: name 'vector' is not defined
Traceback (most recent call last):
  File "Text", line 6, in <module>
TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and typ
es

This is the second time in two days I’ve become completely lost as to what I’m doing wrong. I’m starting to lose it.

Try:


import Blender
import bpy
from Blender.Mathutils import *

VECTORTYPE = Vector(0, 0, 0)
x = Vector(0, 0, 1)
if type(x) is type(VECTORTYPE):
    print 'true'
else:
    print 'false'

In python the first argument of type() should be an object or instance of a class, not the class itself.

Since isinstance expects a class for argument 2, in python you should be able to do something like this:


isinstance(x, Vector)

But this doesn’t work. The documentation suggests that Vector is a class but doing this:


Vector.__class__
>>> <type 'builtin_function_or_method'>

shows it to be a function. But the function does return a class object or instance of type Vector:


Vector().__class__
>>> <type 'Blender Vector'>

so you can do this:


 isinstance(x, type(Vector()))
 

+1, thanks sornen