Tracking instances in for loop

SORRY I CANNOT GET THE CODE HTML TO WORK
Hey! So I am creating a 3d visualization of a network based on a spreadsheet. The spreadsheet lists each node of the network with the type of node it is (hence the a, b, and c type in my code). The problem is that I later want to examine the position of each of these nodes so that I can change the model in panda if there exists more than one node of the same type in the same position (trekkin?). This may be more of a python problem, but help would definitely be appreciated. Basically, I want to go back afterwards and see if my 8th iteration (/node) has the same position as my 6th, and if so, stack them or something. Here is the code snippet for this. Pardon if I am a terrible writer.

class MyApp(ShowBase):

	def __init__(self):
		ShowBase.__init__(self)
									
		
		#make a model for each node based on type
		def createmodels():
		
			for i in range(0, len(nodeinfo)):
				self.atype=self.loader.loadModel("samples/Disco-Lights/models/sphere")
				self.atype.setColor(1,0,0,0)
				self.btype=self.loader.loadModel("samples/Disco-Lights/models/sphere")
				self.btype.setColor(0,0,1,0)
				self.ctype=self.loader.loadModel("samples/Disco-Lights/models/sphere")
				self.atype.setColor(0,1,0,0)
				if nodeinfo[i][3]=='A':
					self.atype.reparentTo(self.render)
					self.atype.setScale(.4,.4,.4)
					self.atype.setPos(float(nodeinfo[i][1])*4,float(nodeinfo[i][2])*4,2)
				elif nodeinfo[i][3]=='B':
					self.btype.reparentTo(self.render)
					self.btype.setScale(.4,.4,.4)
					self.btype.setPos(float(nodeinfo[i][1])*4,float(nodeinfo[i][2])*4,2)
				elif nodeinfo[i][3]=='C':
					self.ctype.reparentTo(self.render)
					self.ctype.setScale(.4,.4,.4)
					self.ctype.setPos(float(nodeinfo[i][1])*4,float(nodeinfo[i][2])*4,2)

Mod edit: you had the “Disable BBCode in this post” box ticked.

You could put them in a list.

list_of_stuff = []
for info in nodeinfo:
    model = loader.loadModel("smiley")
    model.reparentTo(render)
    list_of_stuff.append(model)

A few improvements I can suggest:

  • You are loading 3 models for each node then parenting one of them to render depending on what type you want. It is much more efficient to just load the one model you need.
  • Make a class for your “node” type. As you add more attributes and functions to your nodes this will help keep all the data organized.
  • Instead of “for i in range(0, len(nodeinfo)):” you can simply do “for info in nodeinfo:”.

Thanks! That helps a lot.