Page MenuHome

Swizzle properties for Mathutils.Vector
Closed, ArchivedPublicPATCH

Description

Allows access to elements of vectors in any order. The getter functions all return a Vector object with the new arrangement of axes. For example:

vec = Mathutils.Vector([1,2,3])
print vec.zxy
# prints "[3.000000, 1.000000, 2.000000](vector)"

Setters can take a vector, list or scalar as the value. If the value is scalar, it is copied to all axes listed in the swizzle. If an axis appears more than once in the swizzle, the final occurrance is the one that determines its value.


vec = Blender.Mathutils.Vector([1, 2, 3, 4])


vec.xxxx = 10

print vec

# prints "[10.000000, 2.000000, 3.000000, 4.000000](vector)"


If value is a vector or list this operates like an array copy, except that the destination is effectively re-ordered as defined by the swizzle. At most min(len(source), len(dest)) values will be copied.

vec.zxy = [1, 2, 3]

print vec

# prints "[2.000000, 3.000000, 1.000000, 4.000000](vector)"


vec.zxy = Blender.Mathutils.Vector([1, 2, 3, 5000])

print vec

# prints "[2.000000, 3.000000, 1.000000, 4.000000](vector)"


This can be used to rotate axes in wonderful ways!

vec = Blender.Mathutils.Vector([1, 2, 3, 4])

vec.wxzy = vec.yzxw

print vec
# prints "3.000000, 4.000000, 1.000000, 2.000000](vector)"

More information on swizzling is available here:
http://en.wikipedia.org/wiki/Swizzling_(computer_graphics)

See attached TestVector.blend for a set of unit tests. Tested so far on an Intel Core 2 Duo.

Event Timeline

Attached an update that works better with python initialization

Made the interface a little cleaner: got rid of the Vector_MixedGetSeter structure; using global arrays instead. Now Vector_AppendSwizzleGetseters and Vector_DelGetseters access these globals directly and take no arguments.

applied in svn with some changes,
didnt get your Swizzle_v3.patch, funnily enough I also removed the Vector_MixedGetSeter structure
- fools never differ :)

Campbell Barton (campbellbarton) changed the task status from Unknown Status to Unknown Status.May 3 2009, 7:53 PM