Created
December 15, 2017 18:08
-
-
Save eliemichel/db86f7a3ebd9f871628af4baf4dc2675 to your computer and use it in GitHub Desktop.
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
""" | |
autoRotate | |
========== | |
Align the selected edge to the world X axis of the scene by rotating the whole mesh. | |
Usage | |
----- | |
Select exactly one edge, then run the script. | |
License | |
------- | |
Copyright (c) 2017 Elie Michel -- ATI (Universite Paris 8) | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
""" | |
from maya import cmds | |
import maya.api.OpenMaya as om | |
def objectFromEdge(edge): | |
"""Convert edge identifier into its matching shape identifier. | |
I'm not sure whether this is always correct, but could not find | |
a proper way to do so using maya's API""" | |
return '.'.join(edge.split('.')[:-1]) | |
def alignEdgeToXAxis(): | |
"""Align the selected edge to the world X axis of the scene""" | |
edgeList = cmds.ls(sl=True) | |
if len(edgeList) != 1: | |
cmds.error("[alignEdgeToXAxis] You must select exactly one edge") | |
return | |
# Get the position of the vertices of the selected edge | |
edge = edgeList[0] | |
verts = cmds.ls(cmds.polyListComponentConversion(edge, tv=True), flatten=True) | |
assert(len(verts) == 2) | |
p = [cmds.xform(v, query=True, translation=True, worldSpace=True) for v in verts] | |
assert(len(p) == 2 and len(p[0]) == 3) | |
p = [om.MVector(u) for u in p] | |
# Reconstruct rotation matrix from the target X vector | |
y = om.MVector(0, 1, 0) | |
x = (p[1] - p[0]).normalize() | |
z = (x ^ y).normalize() | |
y = (z ^ x).normalize() | |
c = -(p[0] + p[1]) / 2.0 | |
# Transpose the matrix to get a transformation back to the aligned object | |
mat = om.MMatrix([ | |
[x[0], y[0], z[0], 0], | |
[x[1], y[1], z[1], 0], | |
[x[2], y[2], z[2], 0], | |
[ 0, 0, 0, 1] | |
]) | |
# Translation matrices, in order to rotate around the center of the edge instead of the origin point | |
t1 = om.MMatrix([ | |
[1, 0, 0, 0], | |
[0, 1, 0, 0], | |
[0, 0, 1, 0], | |
[c.x, c.y, c.z, 1] | |
]) | |
t2 = om.MMatrix([ | |
[1, 0, 0, 0], | |
[0, 1, 0, 0], | |
[0, 0, 1, 0], | |
[-c.x, -c.y, -c.z, 1] | |
]) | |
# Apply the rotation matrix | |
currentMat = om.MMatrix(cmds.xform(objectFromEdge(edge), q=True, m=True)) | |
currentMat = cmds.xform(objectFromEdge(edge), m=currentMat*t1*mat*t2) | |
alignEdgeToXAxis() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment