Jump to content

Selection element list-user defined script function


---
 Share

Recommended Posts

Hello everyone,

I want to create a dialog window in which there will be selection list. First, i created some variables and then want user to select the name of one of  the created  variable in the dialog. Since i did not create an element and only create a  variable in workspace, i could not create selection list which feed from  names of the created variables.

Is there a way to do that through scripting before creating an actual  element like plane, point etc.?

Many thanks,

Canset

Link to comment
Share on other sites

If it is not possible, could you please provide an example code about the usage of user-defined script function?

Many thanks,

Canset

Link to comment
Share on other sites

Here is a dialog that you can pass a list of items to (and a prompt if desired), and it will have the user to choose one of them. The return value is a string of whichever one was selected. 

It also divides the items into columns of 20 (if you have more than 20 items) to keep it from becoming too long

 

def selection(prompt='', item_list=[]):
	
	if not isinstance(item_list, list):
		
		return
	
	header = '<dialog>' \
			'<title>*</title>' \
			'<control id="Empty"/>' \
			'<position>center</position>' \
			'<sizemode>fixed</sizemode>' \
			'<size width="200" height="100"/>' \
			'<content rows="{}" columns="{}">' \
			'<widget row="0" column="0" type="label">' \
			'<name>prompt</name>' \
			'<text>{}</text>' \
			'</widget>'
	
	button = '<widget column="{}" row="{}" type="button::pushbutton">' \
			'<name>{}</name>' \
			'<text>{}</text>' \
			'<type>toggle</type>' \
			'<focus>True</focus>' \
			'</widget>'
	
	def build_dialog():
		
		num_columns = int(len(item_list) / 20) + 1
		
		dialog = header.format(len(item_list) + 1, num_columns, prompt)
		
		index = 0
		
		for i in range(1, len(item_list) + 1):
			
			if i <= 20:
				
				row = i
				
				column = index
				
				if i == 20:
					
					index += 1
			else:
				
				row = abs(index * 20 - i)
				
				column = index
				
				if i % 20 == 0:
					
					index += 1
			
			dialog += button.format(column, row, i, item_list[i - 1])
		
		return dialog + '</content></dialog>'
	
	def dialog_event_handler(widget):
		
		global selected
		
		if isinstance(widget, gom.Widget):
			
			selected = widget.text
			
			gom.script.sys.close_user_defined_dialog(dialog=dialog)
	
	dialog = gom.script.sys.create_user_defined_dialog(content=build_dialog())
	
	dialog.handler = dialog_event_handler
	
	gom.script.sys.show_user_defined_dialog(dialog=dialog)
	
	return selected

 

Link to comment
Share on other sites

Hi Robert,

Many thanks for the quick response.  I implemented the code into my program and it works fine for me.

Thanks,

Canset

Link to comment
Share on other sites

  • 2 years later...

Hi. I came across this post today. I was able to get the script above working and even changed so that it creates 'selection list' elements instead of 'button' elements, however my goal is to take that same list and populate it into just one selection list box, like so:

I just can't figure out how I can manipulate the 'selection list' element. Is this possible?

 

image.png.9b4b120faad47979f084ddef62eba765.png   image.png.9d9f8f0a2c456fd3b471685eef587003.png

image.png

 

Edited
Link to comment
Share on other sites

Well, nevermind. This is so much easier than I was making it out to be. 🤦‍♂️

listofthings = ['abc', 2.1, 3, 4, 5, 6, 7, 8, 9]

...Dialog creation goes here...

selectedValue = DIALOG.selectionListWidget
selectedValue.items = listofthings
selectedValue.default = "4"

That's all it takes. 

Edited
Link to comment
Share on other sites

 Share

×
×
  • Create New...