simple script global problem

I have created the below script to simply retain the actual vertexes when editing a mesh rather than the encompassing method used by vertex groups.
(more detail about what it does after the code)


# WARNING very crude code, just a proof-of-concept at this point
import bpy
import Blender
from Blender.BGL import *
from Blender.Draw import *
from Blender import *
listv = [] 
# harcoded name for test purposes
sphere = Blender.Object.Get("Sphere") 
sMesh = sphere.getData(False,True) # false for name only and true to get mesh data

def initSelection():
    # clear listv
    del listv[:]
    for v in sMesh.verts: 
        if v.sel: listv.append(v.co) 
    # at this point the initialisation is complete, we have a copy of the coordinates of the unaltered vertex.
    # now we wait until the user hits the select only original button

def SelectOnlyOriginal():
    print "SelectOnlyOriginal():"
    for v in sMesh.verts: 
        # if coords match entry in list select else deselect
        v.sel=IsSelection(v.co)

def IsSelection(coord):
    print "IsSelection",coord
    returning=False
    for co in listv:
        if co==coord: returning=True    
        print "listv",co,"==",coord,returning
    return(returning)

def draw():
    global Button3, Button2, Button1
    Button('Exit', 1, 106, 25, 39, 15, 'Exit script')
    Button('init', 2, 58, 25, 39, 15, 'Initialise')
    Button('SOO', 3, 0, 25, 49, 15, 'Select only original')

def event(evt, val):
    if (evt== QKEY and not val): Exit()

def bevent(evt):
    # must exit edit mode to continue
    Window.EditMode(0)
    if evt == 1: #Button1
        Exit()
    elif evt == 2: #Button2
        initSelection()
    elif evt == 3: #Button3
        SelectOnlyOriginal()
    # return to edit mode
    Window.EditMode(1)

Register(draw, event, bevent)

Sometimes it works and the SOO button only selects the vertexes that I had recorded earlier but most of the time it selects nothing and I find all the saved vertex data is either random numbers or zero.
If it does work then clicking SOO again shows the listv variable is fubar.

What is affecting the listv variable ?

The reason for the script needs an example, create a Cube, select all the vertexes and add then to a vertex group, subdivide the mesh and select the original vertexes via the vertex group, oh, wait you can’t.
Now while I understand why that functionality would be useful it is not the functionality I want, hence the script.

i open another thread but

can you tell me is there is a way to select a vertex group in an object and how ?

Thanks

Anywhere you modify a global variable (not just read it), make sure you put that you’re modifying the global copy, i.e.:


myGlobalVar = 10
 
def someModifyFunction():
     global myGlobalVar
     myGlobalVar = 5
 
def someReadFunction():
     myLocalVar = myGlobalVar

See if that works in your code…

using the global key word did not help :frowning:
Though it seems to now run at least once consistently.
I have updated the script with lots of debug messages reporting the state of the global list (now called ListOfBananas to ensure it does not conflict with anything else).
The only time the list data gets corrupted is after the bevent() method has ended and before it is started again.

# vertex selector
# force selection to actual vertexes even if new ones are created.
# standard vertex groups fail this task, for obvious reasons.
# rednuht 04/07/2009
import bpy
import Blender
from Blender.BGL import *
from Blender.Draw import *
from Blender import *
ListOfBananas = [] 
# harcoded name for test purposes
sphere = Blender.Object.Get("Cube") 
sMesh = sphere.getData(False,True) # false for name only and true to get mesh data



def initSelection():
    print "initSelection():"
    global ListOfBananas
    # clear ListOfBananas
    del ListOfBananas[:]
    for v in sMesh.verts: 
        if v.sel: ListOfBananas.append(v.co) 
    # at this point the initialisation is complete, we have a copy of the coordinates of the unaltered vertex.
    # now we wait until the user hits the select only original button

def SelectOnlyOriginal():
    print "SelectOnlyOriginal():"
    for v in sMesh.verts: 
        # if coords match entry in list select else deselect
        v.sel=IsSelection(v.co)

def IsSelection(coord):
    print "IsSelection",coord
    returning=False
    for co in ListOfBananas:
        if co==coord: returning=True    
        print "ListOfBananas",co,"==",coord,returning
    return(returning)

def draw():
    global Button3, Button2, Button1
    Button('Exit', 1, 106, 25, 39, 15, 'Exit script')
    Button('init', 2, 58, 25, 39, 15, 'Initialise')
    Button('SOO', 3, 0, 25, 49, 15, 'Select only original')

def event(evt, val):
    if (evt== QKEY and not val): Exit()

def bevent(evt):
    ListList("pre editmode(0) ",ListOfBananas)
    # must exit edit mode to continue
    Window.EditMode(0)
    ListList("post editmode(0) ",ListOfBananas)
    if evt == 1: #Button1
        Exit()
    elif evt == 2: #Button2
        ListList("pre init",ListOfBananas)
        initSelection()
        ListList("post init",ListOfBananas)
    elif evt == 3: #Button3
        ListList("pre SOO",ListOfBananas)
        SelectOnlyOriginal()
        ListList("post SOO",ListOfBananas)
    # return to edit mode
    ListList("pre editmode(1) ",ListOfBananas)
    Window.EditMode(1)
    ListList("post editmode(1)",ListOfBananas)

def ListList(ref,list):
    print ref,"list len = ",len(list)
    for lco in list:
        print ref,lco

print "========="
print "New Session"
print "========="
Register(draw, event, bevent)

Try this code:


# WARNING very crude code, just a proof-of-concept at this point
import bpy
import Blender
from Blender.BGL import *
from Blender.Draw import *
from Blender import *
listv = []
# harcoded name for test purposes
sphere = Blender.Object.Get("Cube") 
sMesh = sphere.getData(mesh = True) # false for name only and true to get mesh data

def initSelection():
    global listv
    listv = [Mathutils.Vector(sMesh.verts[v].co[0], sMesh.verts[v].co[1], sMesh.verts[v].co[2])  for v in sMesh.verts.selected()]

def SelectOnlyOriginal():
    global listv
    for v in sMesh.verts:
        for oldCo in listv:
            if oldCo == v.co:
                v.sel = 1

def draw():
    global Button3, Button2, Button1
    Button('Exit', 1, 106, 25, 39, 15, 'Exit script')
    Button('init', 2, 58, 25, 39, 15, 'Initialise')
    Button('SOO', 3, 0, 25, 49, 15, 'Select only original')

def event(evt, val):
    if (evt== QKEY and not val): Exit()

def bevent(evt):
    # must exit edit mode to continue
    Window.EditMode(0)
    if evt == 1: #Button1
        Exit()
    elif evt == 2: #Button2
        initSelection()
    elif evt == 3: #Button3
        SelectOnlyOriginal()
    # return to edit mode
    Window.EditMode(1)

Register(draw, event, bevent)

You pretty much just needed to do a full copy of the coordinates rather than a shallow copy.