Skip to content

Instantly share code, notes, and snippets.

@justinfx
Created June 12, 2012 17:09
Show Gist options
  • Select an option

  • Save justinfx/2918740 to your computer and use it in GitHub Desktop.

Select an option

Save justinfx/2918740 to your computer and use it in GitHub Desktop.
#move vertices as rod goes over it
import maya.OpenMaya as om
import maya.cmds as cmds
import math as m
class vertexInfo(object):
def __init__(self):
self.verX = []
self.verY = []
self.verZ = []
self.verIn = []
def saveDefaultPos(self):
del self.verX[:]
del self.verY[:]
del self.verZ[:]
del self.verIn[:]
om.MGlobal.clearSelectionList()
om.MGlobal.selectByName('pPlane1')
sList = om.MSelectionList()
om.MGlobal.getActiveSelectionList(sList)
iter1 = om.MItSelectionList ( sList, om.MFn.kGeometric )
while not iter1.isDone():
dagPath = om.MDagPath()
components = om.MObject()
sList.getDagPath( 0 ,dagPath, components)
iter2 = om.MItMeshVertex(dagPath, components)
while not iter2.isDone():
pt = iter2.position()
ind = iter2.index()
self.verX.append(pt.x)
self.verY.append(pt.y)
self.verZ.append(pt.z)
self.verIn.append(ind)
iter2.next()
iter1.next()
def check(self):
'''if #variable are not empty check to match
self.match(yesNo)
if yesNo == 0: #no match then run clear
self.clear()
self.saveDefaultPos()
if yesNo == 1: #yes match then keep going
'''
doubleArray = om.MScriptUtil()
spc = om.MSpace.kWorld
#check current position
##save cylinder as a selection
om.MGlobal.clearSelectionList()
om.MGlobal.selectByName('cyl')
sCyl = om.MSelectionList()
om.MGlobal.getActiveSelectionList(sCyl)
item = om.MDagPath()
sCyl.getDagPath(0, item)
item.extendToShape()
fnMesh = om.MFnMesh(item)
dagPath = om.MDagPath()
components = om.MObject()
sCyl.getDagPath( 0 ,dagPath, components)
cylPts = om.MItMeshVertex(dagPath, components)
##save plane as selection
om.MGlobal.clearSelectionList()
om.MGlobal.selectByName('pPlane1')
sList = om.MSelectionList()
om.MGlobal.getActiveSelectionList(sList)
iter1 = om.MItSelectionList ( sList, om.MFn.kGeometric )
while not iter1.isDone():
dagPath = om.MDagPath()
components = om.MObject()
sList.getDagPath( 0 ,dagPath, components)
iter2 = om.MItMeshVertex(dagPath, components)
while not iter2.isDone():
thisPt = iter2.position()
ind = iter2.index()
inPoint = om.MPoint(thisPt.x, thisPt.y, thisPt.z)
outPoint = om.MPoint(0.0, 0.0, 0.0)
fnMesh.getClosestPoint(inPoint, outPoint, spc)
x = (outPoint[0] - thisPt.x)
y = (outPoint[1] - thisPt.y)
z = (outPoint[2] - thisPt.z)
dist = m.sqrt(m.pow(x, 2) + m.pow(y, 2) + m.pow(z, 2))
# if current position is default position then runcheck to move down
if thisPt.x == self.verX[ind] and thisPt.y == self.verY[ind] and thisPt.z == self.verZ[ind]:
#find the normal of the closest point on the cylinder
doubleArray.createFromList([1, 1, 1], 1)
cylNor = om.MVector(doubleArray.asDoublePtr())
cylPts.getNormal(cylNor, spc)
#print cylNor.x
dist = m.sqrt(m.pow(x, 2) + m.pow(y, 2) + m.pow(z, 2))
if dist < 2:
#find that remaining distance cylNor is magnitude 1
nX = -cylNor.x * (2 - dist)
nY = -cylNor.y * (2 - dist)
nZ = -cylNor.z * (2 - dist)
doubleArray.createFromList([nX, nY, nZ], 1)
moveVec = om.MVector(doubleArray.asDoublePtr())
#move down the remaining distance
iter2.translateBy(moveVec, spc)
iter2.next()
#if current position is not default then run check to move it back in place
else:
if dist > 2:
dx = self.verX[ind] - thisPt.x
dy = self.verY[ind] - thisPt.y
dz = self.verZ[ind] - thisPt.z
doubleArray.createFromList([dx, dy, dz], 1)
vec = om.MVector(doubleArray.asDoublePtr())
iter2.translateBy(vec, spc)
iter2.next()
iter1.next()
##save cylinder as a selection
om.MGlobal.clearSelectionList()
om.MGlobal.selectByName('cyl')
sCyl = om.MSelectionList()
om.MGlobal.getActiveSelectionList(sCyl)
item = om.MDagPath()
sCyl.getDagPath(0, item)
item.extendToShape()
fnMesh = om.MFnMesh(item)
dagPath = om.MDagPath()
components = om.MObject()
sCyl.getDagPath( 0 ,dagPath, components)
cylPts = om.MItMeshVertex(dagPath, components)
##save plane as selection
om.MGlobal.clearSelectionList()
om.MGlobal.selectByName('pPlane1')
sList = om.MSelectionList()
om.MGlobal.getActiveSelectionList(sList)
iter1 = om.MItSelectionList ( sList, om.MFn.kGeometric )
while not iter1.isDone():
dagPath = om.MDagPath()
components = om.MObject()
sList.getDagPath( 0 ,dagPath, components)
iter2 = om.MItMeshVertex(dagPath, components)
while not iter2.isDone():
thisPt = iter2.position()
ind = iter2.index()
inPoint = om.MPoint(thisPt.x, thisPt.y, thisPt.z)
outPoint = om.MPoint(0.0, 0.0, 0.0)
fnMesh.getClosestPoint(inPoint, outPoint, spc)
x = (outPoint[0] - thisPt.x)
y = (outPoint[1] - thisPt.y)
z = (outPoint[2] - thisPt.z)
dist = m.sqrt(m.pow(x, 2) + m.pow(y, 2) + m.pow(z, 2))
# if current position is default position then run check to move down
if thisPt.x == self.verX[ind] and thisPt.y == self.verY[ind] and thisPt.z == self.verZ[ind]:
#find the normal of the closest point on the cylinder
doubleArray.createFromList([1, 1, 1], 1)
cylNor = om.MVector(doubleArray.asDoublePtr())
cylPts.getNormal(cylNor, spc)
#print cylNor.x
dist = m.sqrt(m.pow(x, 2) + m.pow(y, 2) + m.pow(z, 2))
if dist < 2:
#find that remaining distance cylNor is magnitude 1
nX = -cylNor.x * (2 - dist)
nY = -cylNor.y * (2 - dist)
nZ = -cylNor.z * (2 - dist)
doubleArray.createFromList([nX, nY, nZ], 1)
moveVec = om.MVector(doubleArray.asDoublePtr())
#move down the remaining distance
iter2.translateBy(moveVec, spc)
iter2.next()
#if current position is not default then run check to move it back in place
else:
if dist > 2:
dx = self.verX[ind] - thisPt.x
dy = self.verY[ind] - thisPt.y
dz = self.verZ[ind] - thisPt.z
doubleArray.createFromList([dx, dy, dz], 1)
vec = om.MVector(doubleArray.asDoublePtr())
iter2.translateBy(vec, spc)
iter2.next()
iter1.next()
# i'm using this to run the methods
v = vertexInfo()
v.saveDefaultPos()
jobNum = cmds.scriptJob(event=('timeChanged', v.check))
@justinfx
Copy link
Copy Markdown
Author

Fixed global name errors when referring to verX, verY, ...

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