Home Technical Talk

Maya Python - With every button click, add UI element - Possible?

polycounter lvl 11
Offline / Send Message
Overmind5000 polycounter lvl 11
Hello. I'm in the works of making an auto rig script, and it requires the ability to make modules in a scene and show their info in the main UI. I already have that working for one single item, but when I try to add multiple, maya says the UI item is not unique. I've been looking for a solution to iterate the ID name of the UI element. Does anyone have any suggestions? Here's a sample of the code I'm typing up:
	def addToUI(self, name, number):
	
		UIName = name
		
		number = 0
		
		number += 1
		
		iterNum = iter(str(number))
		
		self.widgets['moduleList'] = cmds.columnLayout('moduleList', edit = True)
		
		bipLegExistence = cmds.getAttr('Meta_BipLeg_0' + str(number) + '.existence')
	
		if bipLegExistence == 1:
		
			cmds.frameLayout(UIName + str(number), p = self.widgets['moduleList'], w = 241, cll = True, cl = True, l = 'Biped Leg 0' + str(number), bgc = (0.1, 0.4, 0.75))
			
			bipLeg01 = cmds.text('bipLeg01', p = UIName + str(number), align = 'left', l = 'Generic Leg.')
			bipLeg02 = cmds.text('bipLeg02', p = UIName + str(number), align = 'left', l = 'Best used for humans or humanoids.')
				
		cmds.separator(h = 8, st = 'in')

Replies

  • iconoplast
    Options
    Offline / Send Message
    iconoplast polycounter lvl 13
    First/most important: the line "number = 0" shouldn't be there. You're taking 'number' as an argument, then assigning it to 0, then adding 1 to it. That doesn't make a lot of sense -- first, don't take it as an argument if you're assigning a value to it right away. I'm assuming you want it to be 0 only at first, then add one each time, but you're resetting it to 0 each time too.

    Next: What's up with "iterNum = iter(str(number))"? What's it for? I don't see any for loops here and I'm don't know of any uses of that outside of them. Having talked it over with someone who knows a bit more Python than I do (I'm rusty) you're also going to have issues regardless. If the number isn't a string, you've iterated it with the line above. If it is a string, you've still iterated it with the line above (strings are iterable in Python) and you're then iterating again. (Related: Did you learn C++ first?)

    Try addressing those and see if that fixes the error. I'm betting, based on what I see here, that the biggest issue is "number" always being the same. If not, then I'm betting a larger sample is in order.
Sign In or Register to comment.