I'm having an issue updating the information in a class array
variable. I have a few variables storing point info on a plane. when I
delete that plane and make a new one the same name it still has info
from the first plane even after i run the method to get the info from
the second plane. here's my script

#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[0:len(self.verX)]
        del self.verY[0:len(self.verY)]
        del self.verZ[0:len(self.verZ)]
        del self.verIn[0:len(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 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 = verX[ind] - thisPt.x
                        dy = verY[ind] - thisPt.y
                        dz = 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 = verX[ind] - thisPt.x
                        dy = verY[ind] - thisPt.y
                        dz = 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))


Please help! Thanks!!

-- 
view archives: http://groups.google.com/group/python_inside_maya
change your subscription settings: 
http://groups.google.com/group/python_inside_maya/subscribe

Reply via email to