Substance Painter
In this start to finish texturing project within Substance Painter we cover all the techniques you need to texture the robot character.
# 1 21-09-2014 , 01:10 AM
Gen's Avatar
Super Moderator
Join Date: Dec 2006
Location: South FL
Posts: 3,522

Python TreeView

Using Python and Maya's treeView control, I've defined a class for my UI and some methods to handle various portions of it. One method in particular is called by another and loops through a list of subfolders found in a specified path, each folder will be added to the treeView and any subfolders found are parented to the appropriate tree item. I've even gotten over the hump with the treeView not accepting duplicate items by prefixing the item's label with the parent folder(if it's a duplicate).

The list is populated fine and to test if the the tree item is pointing to the right directory, I'm passing a string containing the path to a method attached to the control. The string is printed when the tree item is selected but I'm getting the last value assigned in the loop instead. I have this same path set to be printed during the for loop that adds the items to the treeView and that output is correct but not when called from interacting with the UI? Am I making a noobie mistake?

Code:
  	# Other class methods above
  	
  	def folderContents(self , parentLayout):
  	
  		'''
  		loops through all tabs in list, creating folder trees
  		'''
  		
  		# let's start looping through tab list
  		for label in self.tabs:
  
  			self.tabForm = mc.formLayout(p = parentLayout)
  			self.masterTab = mc.tabLayout(parentLayout, e = True , tl = (self.tabForm, label))
  			self.folderTree = mc.treeView((label + "_scrollList") ,numberOfButtons = 1 , arp = 0 , enk = 1)
  			
  			mc.formLayout(self.tabForm , e = 1 , 
  			af = [(self.folderTree , "left" , 0),
  			(self.folderTree , "right" , 0),
  			(self.folderTree , "top" , 0),
  			(self.folderTree , "bottom" , 0)])
  			
  			# get all files and subfolders
  			for dirName, subdirList, fileList in os.walk(self.mainPath + label):
  				
  				# get folder name to use as label for tree item
  				self.folderName = os.path.basename(os.path.normpath(dirName))
  				
  				# however, if the the folder name starts with a . or _ , ommit it
  				if not (self.folderName.startswith('.') or self.folderName.startswith('_')):
  					
  					# check if there is already an item with that name, if true, prefix lable with parent folder's name
  					self.parentName = os.path.basename(os.path.dirname(dirName))
  					if mc.treeView(self.folderTree , q = 1 , iex = self.folderName):
  						self.folderName = (self.parentName "_" + self.folderName)
  					
  					# going to be passing folder path to method that creates folder's content ctrls
  					self.path = (dirName + "\\")
  					
  					# now let's start adding items to the treeView
  					mc.treeView( self.folderTree, e = 1 , ai = (self.folderName , self.parentName))
  					
  					# edit the tree entry after establishing it or face errors yay - adding select change command and icon
  					mc.treeView( self.folderTree, e = 1 ,
  					i = [self.folderName, 1, self.folderIcon ] ,
  					scc = lambda:self.updateThumbList(self.path , self.folderName))
  					
  					# tidy things up a bit ,collapse subfolders of the parent category
  					if not self.folderName in self.tabs:
  						mc.treeView( self.folderTree, e = 1 , ei = [self.folderName, 0])
  					
  					# making sure tree item points to correct path
  					print("folder \"" + self.folderName + "\" is at " + self.path)
  					
  					
  					
  	def updateThumbList(self , path , folderName):
  		'''
  		populates thumbnail layout for a given folder >.> if it ever works
  		for now, test if ctrl points to right path
  		'''
  		print("folder " + folderName + " is at " + path)
  		
  		return ""


- Genny
__________________
::|| My CG Blog ||::
::|| My Maya FAQ ||::
Posting Rules Forum Rules
You may not post new threads | You may not post replies | You may not post attachments | You may not edit your posts | BB code is On | Smilies are On | [IMG] code is On | HTML code is Off

Similar Threads