Update variable outside function

I’ve been bashing my head on this for the past couple hours and I don’t know how to fix it.

I’ve got a function I want to use, and I’m passing a variable into it, a counter to be specific, and I want it to add the counter up, and pass that variable back out, all added up, to be used again the next time I call that function.

The problem is, I’m getting behavior I don’t expect. Here’s a sample of something that works just fine in the blender console

variable = 1

def rimGeometryGenerator(var1):
    global variable
    variable = variable + 2
    print(variable)

rimGeometryGenerator(variable)
rimGeometryGenerator(variable)
rimGeometryGenerator(variable)

That spits out the answer of 3, 5 and 7 as you would expect, as variable is updated each time the function runs.

However, in my actual code, I’m getting an error saying my variable is not defined. The code snippet looks like this;

            def RimGeometryGenerator(iL, posX, posY, posZ): 
            #global indexLocation       
            verts.append([ #index n
                posX,
                posY,
                posZ
            ])
            
            iL = iL +1 
            #indexLocation = indexLocation +1           
            for index in range(self.subs):
                indexY = (index +1) *2
                indexZ = indexY +2
                verts.append([ #index for center triangle circle
                    posX,
                    math.sin(math.radians((spin/self.subs)*(index +1)))*posZ,
                    math.cos(math.radians((spin/self.subs)*(index +1)))*posZ,            
                ])
                iL = iL +1
                #indexLocation = indexLocation +1
                faces.append([((iL - self.subs) -1),((iL - self.subs) -2),(iL -1),(iL)])
        
        RimGeometryGenerator(indexLocation, rimX, 0, rimZ)
        RimGeometryGenerator(indexLocation, rimXBack, 0, rimZ)

The variable indexLocation, which I have commented out right now, already exists and works up to this point, as it’s counting up some stuff before this particular function is called. I’ve tried placing global indexLocation elsewhere in the code to no avail. To me this looks like I’m doing the same thing as the simple code in the first example, but it’s just not working. Halp?

Nothing declared outside of bpy persists beyond registration so instead of a global variable it needs to be a property. Either registered to a type, or attached to an instance.

Using globals in general, particularly writing to them as a function side effect, is usually not good idea regardless.

Yeah, that helped me figure it out. I didn’t want to use global but I was just brain dead on ideas and struggling. Thank you. I had tried using a property before, but I was just implementing wrong, so I couldn’t get it working at first.