Skip to content

Instantly share code, notes, and snippets.

@jm991
Created October 30, 2017 04:51
Show Gist options
  • Save jm991/3f4b5a07cf9cb2281acaa1ab75d17241 to your computer and use it in GitHub Desktop.
Save jm991/3f4b5a07cf9cb2281acaa1ab75d17241 to your computer and use it in GitHub Desktop.
Aligns a construction plane to the normal vector and position of the currently selected polygon and creates an orthographic camera pointed at the plane
//---------------------------------------------------------------------------------
// Source: https://gist.github.com/jm991/
// alignWorkplaneToSelection Tool for Maya 2017
// Author: John Mac
// Date: 10-29-2017
//
// Description:
// Aligns a construction plane to the normal vector and position of the currently selected polygon and creates an orthographic camera pointed at the plane
// Commands:
// alignWorkplaneToSelection(),toggleWorkplaneAndPerspCam()
// Installation:
// 1. Copy alignWorkplaneToSelection.mel to C:\Users\[USER]\Documents\maya\[MAYAVERSION]\prefs\scripts\jm991
// 2. Restart maya
// Instructions:
// 1. In component mode, select the polygon you want to align a construction plane to, then execute alignWorkplaneToSelection()
// 2. If you want to switch back and forth between the orthographic workplane camera and the normal perspective camera, execute toggleWorkplaneAndPerspCam()
global proc alignWorkplaneToSelection()
{
//Lists the transform nodes of all selected objects
string $nodes[] = `ls -objectsOnly -selection`;
for ($node in $nodes)
{
println ($node);
}
// Get first object
$result = substituteAllString($nodes[0], "Shape", "");
println ($result);
// Save reference to original parent (since the selection was a poly need to first access the transform)
$val = objectType( $node );
println $val;
$xform = `listRelatives -parent -type transform $node`;
printarray ($xform);
$parentOrig = `listRelatives -parent -path -shapes $xform`;
printarray ($parentOrig);
// Extract face
polyChipOff -ch 1 -dup 1;
polyPerformAction polySeparate o 0;
delete -ch;
string $polySurfaces[] = `ls -objectsOnly -selection`;
// Get reference to the new group created by the face extraction (for deletion/cleanup)
$parentToDel = `listRelatives -parent -path $polySurfaces[0]`;
$parentToDelName = `rename $parentToDel temp`;
// Rename the originally selected object back to it's original Outliner name (face extraction renamed the object)
rename $polySurfaces[0] $result;
rename $polySurfaces[1] constructionPoly;
// Reparent geo back to original parent
parent $result $parentOrig;
// Unparent constructionPoly
parent -w constructionPoly;
// Delete the group that was made by the face extraction
println($parentToDelName);
select -r $parentToDelName;
doDelete;
// Align pivot to constructionPoly's normal
select constructionPoly;
ConvertSelectionToFaces;
alignPivotToSelection(1);
// Create construction plane, if one doesn't already exist
if (`objExists workplane`)
{
// do nothing
}
else
{
plane -s 24 -n workplane;
}
// Select the objects in the order necessary for contraining the workplane to the poly
select -r constructionPoly;
select -add workplane;
// Point constrain the workplane to the poly
doCreatePointConstraintArgList 1 { "0","0","0","0","0","0","0","1","","1" };
pointConstraint -offset 0 0 0 -weight 1;
// Normal constrain workplane's Z against the normal of the poly
normalConstraint -weight 1 -aimVector 0 0 1 -upVector 0 0 1 -worldUpType "object" -worldUpVector 0 1 0;
// Make the construction plane Live
select -r workplane;
MakeLive;
// Select and align view to polygon, then delete it
select -r constructionPoly;
select -d constructionPoly.vtx[0:3];
select -add constructionPoly.f[0];
AlignCameraToPolygon;
select -r constructionPoly;
doDelete;
// Focus/fit zoom to workplane
select -r workplane;
FrameSelectedWithoutChildren;
// Create a new workplane camera from this view and turn it ortho
MTcreateCameraFromView;
string $cameraName = getCurrentCamera();
print $cameraName;
float $orthoVal = `camera -q -orthographic $cameraName`;
if ($orthoVal != 1)
{
CameraModeToggle;
}
// Clean up any existing workplaneCam
if (`objExists workplaneCam`)
{
select workplaneCam;
doDelete;
}
// Rename this to workplaneCam
rename $cameraName workplaneCam;
lookThru workplaneCam;
}
global proc toggleWorkplaneAndPerspCam()
{
string $cameraName = getCurrentCamera();
if ($cameraName == "persp")
{
lookThru workplaneCam;
}
else
{
lookThru persp;
}
}
@Aurontwist
Copy link

HI there... Awesome script though I cannot utilize it.
Could you tell me where the println function comes from?

@XhnPLayer
Copy link

Support MAYA2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment