blender-python-stubs/conformance/test_mathutils_vector.py
Joseph HENRY c2876bc184 Widen mathutils param types, fix getset_descriptor writability, add conformance tests
- Widen bare mathutils type params (Vector, Euler, etc.) to also accept
  Sequence[float], matching Blender's mathutils_array_parse C behavior
- Fix getset_descriptor readonly detection by probing __set__ on the
  descriptor instead of checking fset (which doesn't exist on C descriptors)
- Accept int | slice keys in __getitem__/__setitem__/__delitem__
- Accept Sequence[element_type] values in __setitem__ for slice assignment
- Add mathutils overrides for Matrix.Translation and Matrix.Scale
- Extend apply_overrides to support ClassName.method_name keys
- Add conformance test files from Blender docs examples
- Disable reportUnusedExpression in conformance checks

Remaining known conformance issues:
- draw_handler_add missing from SpaceView3D
- Vector not nominally Sequence[float] (buffer protocol, swizzle setters)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 18:06:11 +01:00

59 lines
1.1 KiB
Python

import mathutils
# Zero length vector.
vec = mathutils.Vector((0.0, 0.0, 1.0))
# Unit length vector.
vec_a = vec.normalized()
vec_b = mathutils.Vector((0.0, 1.0, 2.0))
vec2d = mathutils.Vector((1.0, 2.0))
vec3d = mathutils.Vector((1.0, 0.0, 0.0))
vec4d = vec_a.to_4d()
# Other `mathutils` types.
quat = mathutils.Quaternion()
matrix = mathutils.Matrix()
# Comparison operators can be done on Vector classes:
# (In)equality operators == and != test component values, e.g. 1,2,3 != 3,2,1
vec_a == vec_b
vec_a != vec_b
# Ordering operators >, >=, > and <= test vector length.
vec_a > vec_b
vec_a >= vec_b
vec_a < vec_b
vec_a <= vec_b
# Math can be performed on Vector classes.
vec_a + vec_b
vec_a - vec_b
vec_a @ vec_b
vec_a * 10.0
matrix @ vec_a
quat @ vec_a
-vec_a
# You can access a vector object like a sequence.
x = vec_a[0]
len(vec)
vec_a[:] = vec_b
vec_a[:] = 1.0, 2.0, 3.0
vec2d[:] = vec3d[:2]
# Vectors support 'swizzle' operations.
# See https://en.wikipedia.org/wiki/Swizzling_(computer_graphics)
vec.xyz = vec.zyx
vec.xy = vec4d.zw
vec.xyz = vec4d.wzz
vec4d.wxyz = vec.yxyx
# Direct buffer access is supported.
raw_data = memoryview(vec).tobytes()