Python Forum
variables vcnt, ocnt, and mcnt adding previous values and not resetting to 0
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
variables vcnt, ocnt, and mcnt adding previous values and not resetting to 0
#1
In def DoIt(h5): I am resetting the variables vcnt, ocnt, and mcnt to 0 but when it runs for h4 in hVP: the variables return to their state the last time def count(h4): was triggered by for h4 in hVP:. I want vcnt, ocnt, and mcnt to be 0 every time the for h4 in hVP: loop starts. Help please.



I am checking the state of the variable at this point it the below script

vs.SetRField(h5,'Obj Count','Obj Count',vs.Num2Str(0,ocnt))
vs.AlrtDialog(ocnt) #Displays current count but for somereason is adding the previous count to the current count
vcnt = ocnt = mcnt = 0 #reset to variable to 0
vs.AlrtDialog(ocnt) #displays that variable has been reset to 0

import vs
reset = xP = yP = vcnt = ocnt = mcnt = 0

def count(h4):
	global vcnt, ocnt, mcnt
	ocnt += 1
	if vs.GetTypeN(h4) == 5:
		vcnt += vs.GetVertNum(h4)
	if vs.GetTypeN(h4) == 21:
		vcnt += vs.GetVertNum(h4)
	if vs.GetTypeN(h4) == 40:
		mcnt += 1			

def DoIt(h5):
	global reset,xP,yP, vcnt, ocnt, mcnt
	if h5 != []:
		if vs.GetTypeN(h5) == 16:
			GetHandles(h5)
			for h4 in hVP:
				count(h4)
			vs.SetRecord(h5,'Obj Count')
			vs.SetRField(h5,'Obj Count','Obj Count',vs.Num2Str(0,ocnt))
			vs.AlrtDialog(ocnt) #Displays current count but for somereason is adding the previous count to the current count
			vcnt = ocnt = mcnt = 0 #reset to variable to 0
			vs.AlrtDialog(ocnt) #displays that variable has been reset to 0
			vs.SetRField(h5,'Obj Count','Vertex Count',vs.Num2Str(0,vcnt))
			vs.SetRField(h5,'Obj Count','Mesh Count',vs.Num2Str(0,mcnt))
			vs.Symbol(vs.GetSDName(h5),xP,yP,0)
			xP += 300
			if xP > 5000:
				xP = 0
				yP += 300

def GetHandles2(h1):	
	while h1 != []:
		hSEL.append(h1)
		if vs.GetTypeN(h1) == 92:
			h2 = vs.FInGroup(h1)
			while h2 != []:
				hSEL.append(h2)
				h2 = vs.NextObj(h2)
		h1 = vs.NextObj(h1)

def GetHandles(h5):	
	h2=vs.FInSymDef(h5)
	while h2 != []:
		hVP.append(h2)
		itParent = vs.GetTypeN(h2)  # Get Parent type
		if itParent == 11:
			h3 = vs.FInGroup(h2)
			while h3 != []:
				hVP.append(h3)
				h3 = vs.NextObj(h3)
		h2 = vs.NextObj(h2)

if vs.GetObject('Obj Count') == []:
	vs.NewField('Obj Count', 'Obj Count', '0', 4, 0)
	vs.NewField('Obj Count', 'Vertex Count', '0', 4, 0)
	vs.NewField('Obj Count', 'Mesh Count', '0', 4, 0)


hVP = []
hSEL = []
GetHandles2(vs.FSymDef())
for h5 in hSEL:
	if vs.GetTypeN(h5) == 16:
		DoIt(h5)
Reply
#2
def DoIt(h5):
    global reset,xP,yP, vcnt, ocnt, mcnt
###
            for h4 in hVP:
                count(h4)
###
            vs.AlrtDialog(ocnt) #Displays current count but for somereason is adding the previous count to the current count
            vcnt = ocnt = mcnt = 0 #reset to variable to 0
            vs.AlrtDialog(ocnt) #displays that variable has been reset to 0
count() is where you're setting them. You call count() before you reset them. That's why they have values before you reset them.

And wackiness like that is exactly why you should avoid using globals at all cost. They're hard to debug, and extremely lazy. What's wrong with just returning whatever values you want to use from a function?

import vs
#reset = xP = yP = vcnt = ocnt = mcnt = 0
 
def count(h4, ocnt, vcnt, mcnt):
 #   global vcnt, ocnt, mcnt
    ocnt += 1
    if vs.GetTypeN(h4) == 5:
        vcnt += vs.GetVertNum(h4)
    if vs.GetTypeN(h4) == 21:
        vcnt += vs.GetVertNum(h4)
    if vs.GetTypeN(h4) == 40:
        mcnt += 1           
    return [ocnt, vcnt, mcnt]
 
def DoIt(h5):
    #global reset,xP,yP, vcnt, ocnt, mcnt
    ocnt, vcnt, mcnt = (0, 0, 0)
    if h5 != []:
        if vs.GetTypeN(h5) == 16:
            GetHandles(h5)
            for h4 in hVP:
                ocnt, vcnt, mcnt = count(h4, ocnt, vcnt, mcnt)
            vs.SetRecord(h5,'Obj Count')
            vs.SetRField(h5,'Obj Count','Obj Count',vs.Num2Str(0,ocnt))
            vs.AlrtDialog(ocnt) #Displays current count but for somereason is adding the previous count to the current count
            vcnt = ocnt = mcnt = 0 #reset to variable to 0
            vs.AlrtDialog(ocnt) #displays that variable has been reset to 0
Reply
#3
Then why do you wait until 4 lines after the loop before you reset the counts?

This is bad way to write code, using global variables to pass values between functions. What about doing something like this?
def count(h4, vcnt, mcnt):
    if vs.GetTypeN(h4) == 5:
        vcnt += vs.GetVertNum(h4)
    if vs.GetTypeN(h4) == 21:
        vcnt += vs.GetVertNum(h4)
    if vs.GetTypeN(h4) == 40:
        mcnt += 1
    return vcnt, mcnt

def DoIt(h5):
    global reset,xP,yP
    if h5 != []:
        if vs.GetTypeN(h5) == 16:
            print('reset')
            GetHandles(h5)
            vcnt = mcnt = 0
            ocnt = len(hVP)
            for h4 in hVP:
                vcnt, mcnt = count(h4, vcnt, mcnt)
            vs.SetRecord(h5,'Obj Count')
            vs.SetRField(h5,'Obj Count','Obj Count',vs.Num2Str(0,ocnt))
            vs.AlrtDialog(ocnt)
            vs.AlrtDialog(ocnt)
            vs.SetRField(h5,'Obj Count','Vertex Count',vs.Num2Str(0,vcnt))
            vs.SetRField(h5,'Obj Count','Mesh Count',vs.Num2Str(0,mcnt))
            vs.Symbol(vs.GetSDName(h5),xP,yP,0)
            xP += 300
            if xP > 5000:
                xP = 0
                yP += 300
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Giving all possible values to four different variables quest_ 7 786 Jan-18-2021, 05:18 AM
Last Post: deanhystad
  Adding keys and values to a dictionary giladal 3 690 Nov-19-2020, 04:58 PM
Last Post: deanhystad
  Variables being overridden to initial values. p2bc 6 898 Oct-10-2020, 09:03 PM
Last Post: p2bc
  Print variable values from a list of variables xnightwingx 3 815 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  Assign dynamic values to Variables srikanthpython 6 1,530 Jun-06-2020, 03:36 PM
Last Post: srikanthpython
  adding properties to variables rudihammad 0 632 May-06-2020, 05:09 AM
Last Post: rudihammad
  Problem adding keys/values to dictionary where keynames = "property" and "value" jasonashaw 1 864 Dec-17-2019, 08:00 PM
Last Post: jasonashaw
  Adding markers to Folium map only adding last element. tantony 0 925 Oct-16-2019, 03:28 PM
Last Post: tantony
  Adding Variables absolum 3 1,233 Jul-18-2019, 12:32 PM
Last Post: absolum
  Using Excel Values As Variables for Equation dezmund 1 1,012 May-09-2019, 01:06 PM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020