For Loop Maths for storage dictionary update

Hi Guys,

I want to make a bunch of presets for selecting particular elements in my geo’s.

I have a Table Comp and I’m writing in a datExecute

DatExec.PNG

It needs to be dynamic as i can change the number of rows and columns already so I can’t just hard code these values.

Code so far in my dat execcute is thus. I have no maths yet just possible variables and my for loop works and runs through the dictionary i wish to update. this dictionary is dynamically controlled elsewhere if I change the Rows or Cols in ‘xReps’ and ‘yReps’ in my ‘resolution’ dictionary:

def onTableChange(dat):

	cellID=int(dat[0,0])
	cellNameOp=op('labels')
	cellName= cellNameOp[cellID,0]
	storageCols=me.fetch('resolution')['xReps'] #18
	storageRows=me.fetch('resolution')['yReps'] #10
	selectedGlyphs=me.fetch('selectedLive') #item0-179

	if cellID==0:
		for t in selectedGlyphs: 
			#t=item0-179 in text
			
	else:
		pass

#	print(t)
#	print(cellID)
#	print(cellName)
#	print(storageRows)
#	print(storageCols)


	return

What I want is this NOTE: The items are arranged from top left in columns:

glyph grid chequers.PNG

But where the number of Rows and Cols designates when the “skip” happens.

If you see in the currentv state it is not every other button, the end of a column must be the same as the 1st in the next column and then it returns to every other button. So in the current example with 10 rows in each column, the pattern switches on 10,20,30,40 etc. but if I changed the number of Rows/yReps by 2, it would shift that number by 1 because adding another row desn’t interupt the pattern. If I added a new Column then it would need to switch 1 more time at 180.

Complicated I know. but it can be done I’m sure if you get the maths right.

Something like if there are x xReps and y yReps then do this… it’s bending my mind quite frankly.

Eventually because it’s a loop it will just output a 1 or 0 for the position it is within ‘t’ so no headache there. Part of the Dictionary looks like this:

selectedLiveDict.PNG

Thanks muchly.

Robbie

UPDATE:

I’m now here:

def onTableChange(dat):
	cellID=int(dat[0,0])
	cellNameOp=op('labels')
	cellName= cellNameOp[cellID,0]
	storageCols=me.fetch('resolution')['xReps'] #current value 18
	storageRows=me.fetch('resolution')['yReps'] #current value 10
	selectedGlyphs=me.fetch('selectedLive') #item0-179
	xReps=range(storageCols)

	if cellID==0:
		for s in selectedGlyphs: 
			sNum=s.strip('item')
			sNumByyReps=float(sNum)/storageRows
			#NOTE: s is item0-179 in text
			for x in xReps:
				#print(x)
				x=int(x)
				if sNumByyReps < x+1 :
					if sNumByyReps > x :
						print(x)
						print(sNumByyReps)

	else:
		pass
	return

I might be barking up the wrong tree but this is associating the item with the column number or xReps.

So the print outs are giving a value of x as an int() and a corresponding yrep value of the item num/xReps or Rows as a float(). I.e. 0 and 0.3, 16 and 16.4 etc. I think I’m iterating correctly so I just need now to be able to make an if statement that will allow for changes in the range of x… arrrgggh

x&xRepsCompare.PNG

Please tell me if I’m wrong though and I’ll try to re-think it.

Latest Position is here… almost got it I think, here’s the code with comments:

[code]
def onTableChange(dat):

cellID=int(dat[0,0])
cellNameOp=op('labels')
cellName= cellNameOp[cellID,0]
storageCols=me.fetch('resolution')['xReps'] #current value 18
storageRows=me.fetch('resolution')['yReps'] #current value 10
selectedGlyphs=me.fetch('selectedLive') #item0-179
xReps=range(storageCols)

print(cellNameOp.rows())

print(cellNameOp.rows(cellID)[0])

print(xReps)

print(cellID)

print(cellName)

print(storageRows)

print(storageCols)

# If Button 0 is pushed
if cellID==0:
	# Loop through items in selectedLive Dictionary
	for s in selectedGlyphs: 
		# s =  item(0-179) strip letters from name
		sNum=s.strip('item')
		# Make float and divide by yReps(storageRows) from storage resolution Dict
		sNumByyReps=float(sNum)/storageRows
		# Loop through range() of Columns from xReps(storageCols) in storage resolution Dict
		for x in xReps:
			# cast x to int()
			x=int(x)
			#print(x)
			# Find floats before the next column
			if sNumByyReps < x+1 :
				# Isolate to this column 
				if sNumByyReps > x :
					# Remove the digit to leave only decimal
					position=round(sNumByyReps-x,1)
					# cast decimal value to int() value will be between 0 and yReps-1
					colPosition=int(position*storageRows)
					# check x is even						
					if x % 2 == 0:
						# check colPosition is even
						if colPosition % 2 == 0:
							#print(x)
							#print(sNumByyReps)
							#print(position)
							print(colPosition)
					else:
						pass

else:
	pass

return[/code]

It’s not returning all even columns though?

Ok Completed It

Here’s the Code:

[code]def onTableChange(dat):

cellID=int(dat[0,0])
cellNameOp=op('labels')
cellName= cellNameOp[cellID,0]
storageCols=me.fetch('resolution')['xReps'] #current value 18
storageRows=me.fetch('resolution')['yReps'] #current value 10
selectedGlyphs=me.fetch('selectedLive') #item0-179
xReps=range(storageCols)
# If Button 0 is pushed
if cellID==0:
	# Loop through items in selectedLive Dictionary
	for s in selectedGlyphs: 
		#print(s)
		# s =  item(0-179) strip letters from name
		sNum=s.strip('item')
		#print(sNum)
		# Make float and divide by yReps(storageRows) from storage resolution Dict
		sNumByyReps=float(sNum)/storageRows
		# Loop through range() of Columns from xReps(storageCols) in storage resolution Dict
		#print(sNumByyReps)
		for x in xReps:
			# cast x to int()
			x=int(x)
			#print(x)
			# Find floats before the next column
			if sNumByyReps < x+1 :
				#print(x)
				# Isolate to this column 
				if sNumByyReps >= x :
					#print(x)
					# Remove the digit to leave only decimal
					position=round(sNumByyReps,1)
					# cast decimal value to int() value will be between 0 and yReps-1
					colPosition=int(position*storageRows)
					#print(colPosition)
					# check x (column) is even						
					if x % 2 == 0:
						if colPosition % 2 == 0:
								selectedGlyphs[str(s)] = int(1)
					else:
						if colPosition % 2 == 1:
								selectedGlyphs[str(s)] = int(1)
	#print(selectedGlyphs)
	[me.parent(4).store('selectedLive', selectedGlyphs)]

return[/code]
1 Like