Last active
January 15, 2018 16:33
-
-
Save danielSanchezQ/dc75104f8ec1c2043dfa4fff6fd9239a to your computer and use it in GitHub Desktop.
Short code to apply matrix transfomation over a cube in maya
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
import maya.cmds as cmds | |
import itertools as it | |
def transpose(m, size=4): | |
return list(it.chain.from_iterable(map( | |
lambda x: m[x:len(m):size], | |
xrange(size) | |
))) | |
class TransformationsPlayground(object): | |
def __init__(self): | |
self.pc = cmds.polyCube()[0] | |
def getTransformMatrix(self): | |
m = transpose(cmds.xform(self.pc, q=True, ws=True, m=True)) | |
for i in xrange(0, 16, 4): | |
print m[i:i+4] | |
return m | |
def setTransformMatrix(self, tMatrix): | |
cmds.xform(self.pc, ws=True, m=transpose(tMatrix)) | |
self.getTransformMatrix() | |
######################################################### | |
############# MAIN EXAMPLES HERE ######################## | |
tp = TransformationsPlayground() | |
tp.getTransformMatrix() | |
#sccale and translate the cube | |
m = [ | |
3.0, 0.0, 0.0, 0.0, | |
0.0, 5.0, 0.0, 2.0, | |
0.0, 0.0, 1.0, 0.0, | |
0.0, 0.0, 0.0, 1.0 | |
] | |
tp.setTransformMatrix(m) | |
#Rotate the cube 45º over the Y-axis | |
#calculate matrix in wolframalpha -> {{cos(45),0,sin(45),0},{0,1,0,0},{-sin(45), 0, cos(45),0},{0,0,0,1}}*{{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}} | |
#1/sqrt(2) = 0.7071 | |
my = [ | |
0.7071, 0.0, 0.7071, 0.0, | |
0.0, 1.0, 0.0, 0.0, | |
- 0.7071, 0.0, 0.7071, 0.0, | |
0.0, 0.0, 0.0, 1.0 | |
] | |
tp.setTransformMatrix(my) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment