Last active
March 11, 2023 09:52
-
-
Save jm991/0179c432a7f31d85e48cc2a12947013d to your computer and use it in GitHub Desktop.
You can align the pivot along a selected normal which can be defined by vertices, edges or a face.
This file contains 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
//--------------------------------------------------------------------------------- | |
// Source: https://www.highend3d.com/maya/script/align-pivot-to-selection-mel-for-maya | |
// alignPivotToSelection Tool for Maya 2010, 2011 & 2012 | |
// Author: Daniel Brkovic | |
// Thanks to: Tom Ferstl | |
// Date: 10-12-2011 | |
// | |
// Description: | |
// You can align the pivot along a selected normal which can be defined by vertices, edges or a face. | |
// Commands: | |
// alignPivotToSelection(0),alignPivotToSelection(1) | |
// Installation: | |
// 1. Copy alignPivotToSelection.mel to C:\Users\[USER]\Documents\maya\[MAYAVERSION]\prefs\scripts\jm991 | |
// 2. Restart maya | |
// Instructions: | |
// 1. Select at least 3 vertices(best), 2 edges or 1 face. | |
// 2. Run alignPivotToSelection(0) to affect rotations only and alignPivotToSelection(1) for rotation and translation. | |
// Note: Best results with 3 vertices, triangle or quad face selected. | |
global proc alignPivotToSelection( int $option ) | |
{ | |
if ($option == 0) | |
{ | |
ConvertSelectionToVertices; | |
string $vtxSel[] = `ls -sl -fl`; | |
string $selectedObjectStore[] = `ls -sl -o`; | |
$objectSelectionStore = `listRelatives -p $selectedObjectStore[0]`; | |
if (size($vtxSel) < 3) | |
error "Please select at least 3 Vertices, 2 Edges or 1 Face"; | |
// create and align helperPlane | |
string $plane[] = `polyPlane -w 1 -h 1 -sx 1 -sy 1 -ax 0 1 0 -cuv 2 -ch 1 -n rotationPlane`; | |
select ( $plane[0] + ".vtx[0:2]") $vtxSel[0] $vtxSel[1] $vtxSel[2]; | |
snap3PointsTo3Points(0); | |
// parent object to helperPlane | |
parent $objectSelectionStore $plane[0]; | |
// freeze transformations on object | |
makeIdentity -apply true -t 0 -r 1 -s 0 -n 0 $objectSelectionStore; | |
// unparent object | |
parent -world $objectSelectionStore; | |
// cleanup | |
delete $plane; | |
} | |
else if ($option == 1) | |
{ | |
setToolTo Move; | |
float $getPivotPos[] = `manipMoveContext -q -p Move`; | |
ConvertSelectionToVertices; | |
string $vtxSel[] = `ls -sl -fl`; | |
string $selectedObjectStore[] = `ls -sl -o`; | |
$objectSelectionStore = `listRelatives -p $selectedObjectStore[0]`; | |
if (size($vtxSel) < 3) | |
error "Please select at least 3 Vertices, 2 Edges or 1 Face"; | |
// create and align helperPlane | |
string $plane[] = `polyPlane -w 1 -h 1 -sx 1 -sy 1 -ax 0 1 0 -cuv 2 -ch 1 -n rotationPlane`; | |
select ( $plane[0] + ".vtx[0:2]") $vtxSel[0] $vtxSel[1] $vtxSel[2]; | |
snap3PointsTo3Points(0); | |
// parent object to helperPlane | |
parent $objectSelectionStore $plane[0]; | |
// freeze transformations on object | |
makeIdentity -apply true -t 0 -r 1 -s 0 -n 0 $objectSelectionStore; | |
// move pivot | |
xform -ws -piv $getPivotPos[0] $getPivotPos[1] $getPivotPos[2]; | |
// unparent object | |
parent -world $objectSelectionStore; | |
// cleanup | |
delete $plane; | |
} | |
} |
This was a great help for what I was trying to achieve; create locators and align them to faces.
Thanks for the upload!
Here is my script in Python, if you are curious:
`
import maya.mel as mel
def alignPivotToSelection(sl=None, translate=True):
if not sl:
print("Nothing selected")
return
face_pos = cmds.xform(sl, q=1, translation=True, worldSpace=True)
# Find the average of all our x,y,z points. That's our center
get_pivot_pos = ([sum(face_pos[0::3]) / len(face_pos[0::3]), sum(face_pos[1::3]) / len(face_pos[1::3]), sum(face_pos[2::3]) / len(face_pos[2::3])])
sl_vertices = cmds.ls(cmds.polyListComponentConversion(sl, toVertex=True), flatten=True)
if len(sl_vertices) < 3:
print("Please select at least 3 Vertices, 2 Edges or 1 Face")
return
#sl_object = cmds.listRelatives(cmds.ls(sl=1, objectsOnly=True)[0], parent=True)[0]
# Create and align helper plane
plane = cmds.polyPlane(name="rotationPlane", width=1, height=1, subdivisionsX=1, subdivisionsY=1, axis=[0, 1, 0], createUVs=2, constructionHistory=True)[0]
cmds.select("%s.vtx[0:2]" % plane, sl_vertices[0:3])
mel.eval("snap3PointsTo3Points(0);") ### TODO Transpose this
# Create Locator
new_loc = cmds.spaceLocator()
cmds.xform(new_loc, rotation=cmds.xform(plane, q=True, rotation=True, worldSpace=True), worldSpace=True)
if translate:
# Move pivot
cmds.xform(new_loc, translation=get_pivot_pos, worldSpace=True)
cmds.delete(plane)
#sl = cmds.ls(sl=1)[0]
#alignPivotToSelection(sl=sl, translate=True)
sl = cmds.ls(sl=1)[0]
sl_faces = cmds.ls("%s.f[:]" % sl, flatten=True)
for i in sl_faces:
alignPivotToSelection(sl=i, translate=True)
`
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Remove the ".c" from the filename when you download this (git doesn't support .mel syntax highlighting)