How to apply one-sided (directional) scaling to a matrix?

I’m using matrix.setScaleComponent (directly used in below reference url) to scale a matrix along a specific axis (e.g., X, Y, or Z). However, the scaling occurs symmetrically in both directions (e.g., both +X and -X). I want to apply the scale only in one direction — for instance, only along the +X axis. Is there a way to achieve directional scaling (only +X, +Y, or +Z) using this method or another approach?

Please refer - Scale Handles Operator
using this I am trying to scale in one direction.

To scale in X+ only:

  1. Take your initial matrix:
    const mat = model.getNodeMatrix(id)

  2. Create a translation where you want to scale from (if your part’s origin is in the middle, then translate half the width)
    const transMat = new Communicator.Matrix().setTranslationComponent(size_x / 2, 0, 0)

  3. Create your scaling:
    const scaleMat = new Communicator.Matrix().setScaleComponent(scale_x, 1, 1)

  4. Then undo the translation:
    const transInvMat = Communicator.Matrix.inverse(transMat)

  5. Then it’s just a matter of compounding all the transformations together:

mat = Communicator.Matrix.multiply(mat, transMat)
mat = Communicator.Matrix.multiply(mat, scaleMat)
mat = Communicator.Matrix.multiply(mat, transInvMat)
model.setNodeMatrix(id, mat)
1 Like

What if I want to scale in -X direction now? Means I am working on resize functionality of polygon using its six faces to resize using handles.

It’s always the same pattern, define what your anchor point is, translate your part there before scaling or rotating and then undo your translation.

So for X-, again, this is assuming your part’s origin is in the center:
transMat = new Communicator.Matrix().setTranslationComponent(-size_x / 2, 0, 0)

so on so forth:
Y+ setTranslationComponent(0, size_y / 2, 0)
Y- setTranslationComponent(0, -size_y / 2, 0)
Z+ setTranslationComponent(0, 0, size_z / 2)
Z- setTranslationComponent(0, 0, -size_z / 2)