# Create Rotation Matrix With Given Vectors

I have a set of 3 vectors defined by vertices:

1. Vector1 - (x0, y0, z0), (x1, y1, z1)
2. Vector2 - (x0, y0, z0), (x2, y2, z2)
3. Vector3 - (x0, y0, z0), (x3, y3, z3)

I want to create a rotation matrix from these vectors, which when applied on any other vector V, would align V with the new matrix world.

Assuming that you actually have the 4 vectors in your diagram:

• V0 - (x0, y0, z0) being the new pivot/world center of your matrix.
• V1 - (x1, y1, z1) being the X axis.
• V2 - (x2, y2, z2) being the Y axis.
• V3 - (x3, y3, z3) being the Z axis.

And that they're all in the parents space of the given vector, your direction vectors (V1, V2, V3) should be realigned to your new world center:

V1 -= V0
V2 -= V0
V3 -= V0


And your new matrix can be constructed with the following function:

Matrix([(x1, y1, z1, x0),
(x2, y2, z2, y0),
(x3, y3, z3, z0),
( 0,  0,  0,  1)]


Here's what worked for me:

# First create the matrix having Z axis aligned to V3
rotmat = V3.to_track_quat().to_matrix()

# Find the rotation diff of X axis of this and V1 (the new X axis)
matX = rotmat @ Vector((1, 0, 0))
rotDiff = matX.rotation_difference(V1)

# rotate the matrix with this difference
rotmat = rotDiff.to_matrix() @ rotmat

# We don't need V2