Skip to content

Instantly share code, notes, and snippets.

@kohyuk91
Last active October 14, 2018 08:56
Show Gist options
  • Select an option

  • Save kohyuk91/2d3da03e58e2fe095158a289be8d9aa3 to your computer and use it in GitHub Desktop.

Select an option

Save kohyuk91/2d3da03e58e2fe095158a289be8d9aa3 to your computer and use it in GitHub Desktop.
# Script Name: align_to_grid
#
# Author: Hyuk Ko | [email protected] | 2018.10.14
#
# Usage: 1.Select at least 3 vertices(or locators) which resembles the ground plane.
# 2.Run Script
# 3.Click on the 'N-gon' mesh (You should see a blue arrow)
# 4.Click on the 'ground plane' mesh (You should see a blue arrow)
# 5.Hit 'Enter'
import maya.cmds as mc
import pymel.core as pm
import maya.OpenMaya as om
def cleanUp():
mc.select(clear=True)
mc.isolateSelect(panelWithFocus, state=False)
mc.delete(pc, newMeshTrans, tempPlaneTrans)
def getDagPath(nodeName):
selList = om.MSelectionList()
selList.add(nodeName)
dagPath = om.MDagPath()
selList.getDagPath(0, dagPath)
return dagPath
# Turn on trackSelectionOrder
if mc.selectPref(q=True, trackSelectionOrder=True) is False:
mc.selectPref(trackSelectionOrder=True)
selList = mc.ls(orderedSelection=True, flatten=True, long=True)
selListSize = len(selList)
if selListSize > 2:
# Get Top Level Dag Object of selected vertex(or locator)
try:
rootPN = pm.PyNode(selList[0].split('.')[0])
except:
rootPN = pm.PyNode(selList[0])
rootTrans = rootPN.root().longName()
numPolygons = 1
numVertices = selListSize
selPosList = []
for sel in selList:
pos = mc.pointPosition(sel)
selPosList.append(pos)
vertexArray= om.MFloatPointArray()
for selPos in selPosList:
vertexArray.append(om.MFloatPoint(selPos[0], selPos[1], selPos[2]))
polygonCounts = om.MIntArray()
polygonCounts.append(numVertices)
polygonConnects = om.MIntArray()
for sel in selList:
polygonConnects.append(selList.index(sel))
parentM = om.MObject()
newMesh = om.MFnMesh()
newTransformOrShape = newMesh.create(numVertices, numPolygons, vertexArray, polygonCounts, polygonConnects, parentM)
newMesh.updateSurface()
dagpath = om.MDagPath()
om.MDagPath.getAPathTo(newTransformOrShape, dagpath)
newMeshPN = pm.PyNode(dagpath.fullPathName())
newMeshTrans = newMeshPN.longName()
mc.sets(newMeshTrans, edit=True, forceElement="initialShadingGroup")
# If the normal is facing Y-up do Reverse
if newMeshPN.faces[0].getNormal('world').y > 0.0:
mc.polyNormal(newMeshTrans, normalMode=0, userNormalMode=0, ch=True)
# Create Temp Plane
tempPlaneTrans = mc.polyPlane(name='tempPlane#', w=1000, h=1000, sx=1, sy=1, ax=[0,1,0], ch=True)[0]
# Parent Constraint 'newMesh(Parent)' to 'rootTrans(Child)'
pc = mc.parentConstraint(newMeshTrans, rootTrans, mo=True)
# Isolate 'newMesh' and 'tempPlane'
panelWithFocus = 'modelPanel4' #mc.getPanel(withFocus=True)
mc.select(newMeshTrans, tempPlaneTrans, replace=True)
mc.editor(panelWithFocus, edit=True, lockMainConnection=True, mainListConnection='activeList')
mc.isolateSelect(panelWithFocus, state=True)
# Activate Snap Together Tool
mc.setToolTo('snapTogetherToolCtx')
# If Enter is Pressed, Disable Isolate Mode / Delete tempPlane / Delete newMesh
job = mc.scriptJob(e=('PostToolChanged', cleanUp), runOnce=True)
else:
mc.warning('Select at least 3 vertices(or locators)!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment