Page MenuHome

Support mathutils.Vector.rotate for 2D vectors
Closed, ResolvedPublicTO DO

Description

System Information
Operating system: Windows-10-10.0.18362 64 Bits
Graphics card: GeForce GTX 970/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 441.08

Blender Version
Broken: version: 2.81 (sub 16), branch: master, commit date: 2019-12-04 11:32, hash: rBf1aa4d18d49d
Worked: (optional)

Short description of error
Yet it is possible to rotate a 2D vector by a Matrix.Rotation
it cannot be done through mathutils.Vector.rotate

Exact steps for others to reproduce the error
// this works
v = Vector((1.0, 0.5))
m = Matrix.Rotation(radians(90), 2, 'X')
result = v @ m

// this will raise a ValueError: matrix must have minimum 3x3 dimensions
v = Vector((1.0, 0.5))
result = v.rotate(Matrix.Rotation(radians(90), 2, 'X'))

Event Timeline

lob nico (lobnico) updated the task description. (Show Details)
lob nico (lobnico) renamed this task from mathutils.Vector.rotate callback TypeCheck fails on 2D vectors to python mathutils.Vector.rotate callback TypeCheck fails on 2D vectors.Jan 21 2020, 9:34 PM
lob nico (lobnico) edited projects, added Python; removed BF Blender.
Campbell Barton (campbellbarton) renamed this task from python mathutils.Vector.rotate callback TypeCheck fails on 2D vectors to Support mathutils.Vector.rotate for 2D vectors.Jan 22 2020, 8:17 AM
Campbell Barton (campbellbarton) changed the task status from Needs Triage to Confirmed.
Campbell Barton (campbellbarton) changed the subtype of this task from "Report" to "To Do".

Marked this as a TODO for the Python module.

Hey @Campbell Barton (campbellbarton) ,

I've just compiled Blender yesterday and started looking into this issue since it seemed like a good first contribution.

I noticed that on Vector_rotate (at source\blender\python\mathutils\mathutils_Vector.c:1283) there's a check for only allowing vectors with size 3 or 4.
However, the error that's raised comes from an earlier call to mathutils_any_to_rotmat, done right before the vector size check.

I added a "special case" check for when the vector has size = 2 and the matrix is 2x2 right before it.

I thought this would be better than changing mathutils_any_to_rotmat, as it'd need to resize the matrix to be 3x3 (or we'd have to create a new function to return 2x2 matrices).
Also, we'd also have to deal with Quaternions and Eulers, and imo using them to rotate 2D vectors should result in an error, since it's not an operation as "well defined" as with a 2x2 matrix.

If you think there's a better approach I'm happy to make the changes. Otherwise, can I create a Diff?

Cheers,
Tiago.

p.s.:
I noticed that v.rotate(m) is equivalent to m @ v, so when testing @lob nico (lobnico) 's example v.rotate(Matrix.Rotation(radians(90), 2, 'X')) will be different from v @ m.
Btw, this would be equivalent to a rotation of v along the Z axis, if we had made it a 3D vector by adding a Z component equal to 0.