stop transformation matrix from scaling mesh

I’m using some code from LINK to create a transformation matrix to align shapes on faces of other shapes. for some reason it also scales the object up, and I have no idea why. I’m not at all familiar with transformation matrices. here is the code:

import bpy
import mathutils
import pprint
import bmesh
from mathutils import *

def make_matrix(v1, v2, v3):
    a = v2-v1
    b = v3-v1

    c = a.cross(b)
    if c.magnitude>0:
        c = c.normalized()
    else:
        raise BaseException("A B C are colinear")

    b2 = c.cross(a).normalized()
    a2 = a.normalized()
    m = Matrix([a2, b2, c]).transposed()
    s = a.magnitude
    m = Matrix.Translation(v1) * Matrix.Scale(s,4) * m.to_4x4()

    return m





bm = bmesh.from_edit_mesh(bpy.context.object.data)

selected_face = [f for f in bm.faces if f.select]
selected_face = selected_face[0]        # list has only one item. this makes it a single object instead of a list of one.

trans_matrix = make_matrix(selected_face.verts[0].co, selected_face.verts[1].co, selected_face.verts[2].co)

vert1position = (1, 1, 2)
vert2position = (-1, 1, 2)
vert3position = (-1, -1, 2)
vert4position = (1, -1, 2)
vert5position = (1, 1, 0)
vert6position = (-1, 1, 0)
vert7position = (-1, -1, 0)
vert8position = (1, -1, 0)



vert = []

# vertices
bm.verts.ensure_lookup_table()
vert.append(bm.verts.new((vert1position)))
vert.append(bm.verts.new((vert2position)))
vert.append(bm.verts.new((vert3position)))
vert.append(bm.verts.new((vert4position)))
vert.append(bm.verts.new((vert5position)))
vert.append(bm.verts.new((vert6position)))
vert.append(bm.verts.new((vert7position)))
vert.append(bm.verts.new((vert8position)))


# edges
bm.edges.ensure_lookup_table()
bm.edges.new((vert[0], vert[1]))
bm.edges.new((vert[1], vert[2]))
bm.edges.new((vert[2], vert[3]))
bm.edges.new((vert[3], vert[0]))
bm.edges.new((vert[4], vert[5]))
bm.edges.new((vert[5], vert[6]))
bm.edges.new((vert[6], vert[7]))
bm.edges.new((vert[7], vert[4]))
bm.edges.new((vert[0], vert[4]))
bm.edges.new((vert[1], vert[5]))
bm.edges.new((vert[2], vert[6]))
bm.edges.new((vert[3], vert[7]))


    
bmesh.ops.transform(bm, matrix= trans_matrix, verts= bm.verts[-8:])

if you want to run this code, you need a triangular face of a mesh selected for it to work properly.


SOLVED: Matrix.Scale(s,4) -> Matrix.Scale(1,4)

Matrix.Scale(1,4) is the identity matrix. You can omit that entirely, you’re essentially multiplying by 1.