Last active
May 21, 2025 22:21
-
-
Save tbttfox/e2099c32b143db4e79e648d9d92756ae to your computer and use it in GitHub Desktop.
howMatrixWorks.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Build a matrix from translation values | |
This should give you a feel for how transformation matrices actually WORK in Maya. | |
Transformation matrices can be thought of as 3 vectors, and a point. | |
So I made 4 objects. One representing each of the X Y and Z axis vectors | |
And a fourth representing the translation point. | |
Then I connected the translation values of each object to a fourByFourMatrix node which | |
builds a matrix from 16 values. Then I connected that matrix to the model. | |
All together that builds a matrix that looks like this | |
X.tx X.ty X.tz 0 | |
Y.tx Y.ty Y.tz 0 | |
Z.tx Z.ty Z.tz 0 | |
T.tx T.ty T.tz 1 | |
To test your understanding: | |
Move the x, y and z axes around and see if you can make different scales. | |
Move the x, y and z axes around and see if you can make a rotation. | |
Figure out the formula to get the x, y, and z scale values | |
Figure out the formula for a matrix that rotates around one of the axes. | |
Hint: Remember the unit circle from Trigonometry class | |
""" | |
from maya import cmds | |
def hideTfms(obj, srt="sr", cmp="xyz"): | |
for v in srt: | |
for c in cmp: | |
cmds.setAttr(f"{obj}.{v}{c}", lock=True, keyable=False, channelBox=False) | |
def create_shader(name, rgb, target, node_type="lambert"): | |
material = cmds.shadingNode(node_type, name=name, asShader=True) | |
sg = cmds.sets(name=f"{name}SG", empty=True, renderable=True, noSurfaceShader=True) | |
cmds.connectAttr(f"{material}.outColor", f"{sg}.surfaceShader") | |
cmds.sets(target, edit=True, forceElement=sg) | |
cmds.setAttr(f"{material}.color", *rgb, type="double3") | |
def createLoc(name, size): | |
loc = cmds.spaceLocator(name=name)[0] | |
shape = cmds.listRelatives(loc, children=True, shapes=True)[0] | |
cmds.setAttr(f"{shape}.localScale", size, size, size) | |
hideTfms(loc) | |
return loc | |
def connectRow(src, mat, row): | |
for col, axis in enumerate("xyz"): | |
cmds.connectAttr(f"{src}.t{axis}", f"{mat}.in{row}{col}") | |
def makeArrow(): | |
cyl = cmds.polyCylinder(radius=0.02, height=1.85, constructionHistory=False, subdivisionsAxis=6)[0] | |
cmds.xform(f"{cyl}.vtx[0:5]", relative=True, translation=(0, 1.85 / 2, 0)) | |
cone = cmds.polyCone(radius=0.08, height=0.2, constructionHistory=False, subdivisionsAxis=6)[0] | |
cmds.setAttr(cone + ".ty", 0.9) | |
unite = cmds.polyUnite(cyl, cone)[0] | |
cmds.delete(unite, constructionHistory=True) | |
return unite | |
def makeModel(): | |
sphT = cmds.polySphere(radius=0.3)[0] | |
xax = makeArrow() | |
create_shader("red", (1, 0, 0), xax) | |
cmds.setAttr(xax + ".rz", -90) | |
yax = makeArrow() | |
create_shader("green", (0, 1, 0), yax) | |
zax = makeArrow() | |
create_shader("blue", (0, 0, 1), zax) | |
cmds.setAttr(zax + ".rx", 90) | |
unite = cmds.polyUnite(sphT, xax, yax, zax)[0] | |
cmds.delete(unite, constructionHistory=True) | |
return unite | |
unite = makeModel() | |
hideTfms(unite, srt="srt") | |
elayer = cmds.createDisplayLayer(unite, name="extLayer", noRecurse=True) | |
cmds.setAttr(f"{elayer}.displayType", 2) | |
mat = cmds.createNode("fourByFourMatrix") | |
tloc = createLoc("Translation", 0.75) | |
connectRow(tloc, mat, 3) | |
for i, axis in enumerate("xyz"): | |
loc = createLoc(f"{axis}_Axis", 0.2) | |
cmds.parent(loc, tloc) | |
cmds.setAttr(f"{loc}.t{axis}", 1) | |
connectRow(loc, mat, i) | |
cmds.connectAttr(f"{mat}.output", f"{unite}.offsetParentMatrix") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment