Feature Request: Implement __hash__ for mathutils.Vector

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

robodonut
Posts: 2
Joined: Tue Jun 05, 2012 9:55 pm

Feature Request: Implement __hash__ for mathutils.Vector

Postby robodonut » Tue Jun 05, 2012 10:04 pm

Python 'sets' are ideal for operations on unordered point cloud data, but Blender's vector class does not support hashing. This means that you either have to use lists (which may be slower or less convenient) or write an entirely new vector class instead of reusing the existing one.

stiv
Posts: 3646
Joined: Tue Aug 05, 2003 7:58 am
Location: 45N 86W

Postby stiv » Wed Jun 06, 2012 1:52 am

To be hashable, the object must be immutable. That seems like a stiff requirement for something used in calculations.

From the Python docs:
If a class defines mutable objects and implements a __cmp__() or __eq__() method, it should not implement __hash__(), since hashable collection implementations require that a object’s hash value is immutable (if the object’s hash value changes, it will be in the wrong hash bucket).

robodonut
Posts: 2
Joined: Tue Jun 05, 2012 9:55 pm

Postby robodonut » Wed Jun 06, 2012 2:48 am

That's a very good point.
It seems a shame, though, that I can't reuse the existing vector class at all, even though it's almost exactly what I need.

Maybe someone more versed in Python than I might have a suggestion?

stiv
Posts: 3646
Joined: Tue Aug 05, 2003 7:58 am
Location: 45N 86W

Postby stiv » Wed Jun 06, 2012 3:19 am

random thought: convert the Vector to a tuple or a tuple-ish class? Tuples are immutable and therefore hashable. If you are just using them as data...


Return to “Python”

Who is online

Users browsing this forum: No registered users and 0 guests