PyDoc: include buffer access in examples, cleanup

Note that buffer access is possible, also minor mathutils test cleanup.
This commit is contained in:
Campbell Barton
2025-08-16 17:38:20 +10:00
committed by Thamsanqa Dreem
parent 09ca53d3c2
commit 2237424e07
6 changed files with 22 additions and 6 deletions

View File

@@ -28,3 +28,6 @@ print("Color: {:d}, {:d}, {:d}".format(*(int(c) for c in (col * 255.0))))
# This example prints the color as hexadecimal.
print("Hexadecimal: {:02x}{:02x}{:02x}".format(int(col.r * 255), int(col.g * 255), int(col.b * 255)))
# Direct buffer access is supported.
print(memoryview(col).tobytes())

View File

@@ -30,3 +30,6 @@ vec.rotate(eul)
mat_rot = eul.to_matrix()
mat_loc = mathutils.Matrix.Translation((2.0, 3.0, 4.0))
mat = mat_loc @ mat_rot.to_4x4()
# Direct buffer access is supported.
print(memoryview(eul).tobytes())

View File

@@ -30,3 +30,6 @@ mat[0][0:3] = 0.0, 1.0, 2.0
# Each item in a matrix is a vector so vector utility functions can be used.
mat[0].xyz = 0.0, 1.0, 2.0
# Direct buffer access is supported.
print(memoryview(mat).tobytes())

View File

@@ -29,3 +29,6 @@ exp_avg = (quat_a.to_exponential_map() +
quat_avg = mathutils.Quaternion(exp_avg)
print("Average rotation:")
print(quat_avg)
# Direct buffer access is supported.
print(memoryview(quat_avg).tobytes())

View File

@@ -53,3 +53,6 @@ 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()

View File

@@ -64,12 +64,18 @@ def _test_flat_buffer_protocol(self, ty, n):
vec = ty(expected)
vec.freeze()
view = memoryview(vec)
with self.assertRaises(TypeError):
view = memoryview(vec)
view[0] = 1
class MatrixTesting(unittest.TestCase):
def assertAlmostEqualMatrix(self, first, second, size, *, places=6, msg=None, delta=None):
for i in range(size):
for j in range(size):
self.assertAlmostEqual(first[i][j], second[i][j], places=places, msg=msg, delta=delta)
def test_matrix_column_access(self):
# mat =
# [ 1 2 3 4 ]
@@ -302,11 +308,6 @@ class MatrixTesting(unittest.TestCase):
self.assertEqual(view.format, "f")
self.assertEqual(view.tolist(), expected)
def assertAlmostEqualMatrix(self, first, second, size, *, places=6, msg=None, delta=None):
for i in range(size):
for j in range(size):
self.assertAlmostEqual(first[i][j], second[i][j], places=places, msg=msg, delta=delta)
class VectorTesting(unittest.TestCase):