What i had intended to do was to move a cylinder over a plane and
leave an indentation as the cylinder goes over. I wanted the plane to
be elastic so when the cylinder leaves the immediate region the plane
returns to its initial state. so I have the saveDefaultPos() to save
the vertex information and the check() to displace the vertices of
the plane when the cylinder moves close to it. The first time i run it
the result will be as I expect but when I delete the old pPlane1 and
make a new one with the same name I run the  saveDefaultPos() to save
the vertex info of the new plane. When i run check() now the results
are not what I expect because the vertices move to the positions of
the old pPlane1 instead of the second one I created.

I had thought of using a matrix to store info instead of so many
arrays but i ran into some syntax errors (still having trouble with
the language) so I thought i'd do a small cheat for now. I'll go fix
that now that you've helped me out so my code can be less redundant.

On Jun 12, 1:35 pm, Justin Israel <[email protected]> wrote:
> Hey Serena,
>
> Make sure you use some form of external code view tool for this much code.
> Small snippets are fine, but the messages can't handle the formatting of
> this size and it makes it hard for others to read it. The wrapping breaks
> the lines, and indentations, etc.
>
> I reformatted it into a gist for ya:  https://gist.github.com/2918740
> (pastebin.com works too, but I like gist because of how you can edit and
> version it up, and see a diff)
>
> It was crashing because of a naming error here (line 113):
> dx = verX[ind] - thisPt.x  # self.verX
> dy = verY[ind] - thisPt.y  # self.verY
> dz = verZ[ind] - thisPt.z  # self.verZ
>
> Could you maybe explain a little more specifically how to reproduce the
> problem? This is a lot of code and its hard to understand what is going on
> (a lot of repetition of similar operations).
>
> If you are trying to clear those instance attribute lists (not class
> attributes), you can either just assign them new lists, or if you are
> trying to not break a reference that some other variable may have to that
> same attribute, you can clear the contents with:
>     del self.verX[:]   # or self.verX = [] if you don't care about
> references to the same list
>
> Which specific area is not working as expected?
>
> From the looks of the code, I don't see really why you would need 4
> different lists with so many appends, just to look them up by index again
> later. I don't see the self.verIn even being used... You could probably
> just store them in a dictionary and have much faster access and less
> multi-appends:
>
> self.ver = {}
> self.ver[ind] = (x,y,z)
> x,y,z = self.ver[ind]
> self.ver.clear()On Tue, Jun 12, 2012 at 9:11 AM, Serena Xu 
> <[email protected]> wrote:
> > 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

-- 
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