Notifications
Clear all

[Closed] change selection listner on rollout

The code at the bottom of this script adds a listener, but when I change selection in the scene I get the error that changeRange is unknown on undefined.

So it seems SelectSO is unknown, but I added it in the previous line?
Also is it possible to close an existing rolloutfloater that was made in a previous run of the script?


 (
 rollout SelectSO "Select SubObject" width:162 height:300
 (
 	label lbl1 "From" pos:[15,8] width:42 height:17
 	spinner spn3 "" pos:[47,7] width:43 height:16 range:[1,10000,1] type:#integer
 	label lbl2 "To" pos:[15,30] width:29 height:16
 	spinner spn4 "" pos:[47,28] width:43 height:16 range:[1,10000,1] type:#integer
 	label lbl5 "Step" pos:[15,51] width:29 height:16
 	spinner spn5 "" pos:[48,49] width:43 height:16 range:[1,10000,1] type:#integer
 	checkbox chk1 "New Selection" pos:[15,72] width:113 height:15
 	button btn2 "Select" pos:[14,95] width:99 height:19
 		
 	fn changeRange = 
 	(
 		print "changerange"
 		obj=selection[1]
 		if classof obj==Editable_poly or classof obj == PolyMeshObject then
 		(
 			numVerts=polyOp.getNumVerts obj
 			spn3.range = [1,numVerts,1]
 			spn4.range = [1,numVerts,1]
 			print "changed range"
 		)
 	)
 	
 	/*on selectSO open do
 	(
 		print "addchangelistnr"
 		callbacks.addScript #selectionSetChanged "selectSO.changeRange" id:#selectsoChangeObject
 	)*/
 	
 	on selectSO close do
 	(
 		print "removelistnr"
 		callbacks.removeScripts #selectionSetChanged id:#selectsoChangeObject
 	)	
  
 	on btn2 pressed do
 	(
 		-- store the selected object
 		polyobj = modpanel.getcurrentobject()
 		if subObjectLevel < 1 then
 			MessageBox "Please select a subobject level in Modifier Panel"
 	
 		objclass = classof polyobj as string
 		print objclass
 		if objclass=="Editable_Poly" or objclass=="Edit_Poly" then
 		(
 			if chk1.checked then
 				sel = #{}
 			else
 				sel = polyobj.getSelection subObjectLevel
 			
 			print sel
 			
 			with undo "select SubObject" on
 			(
 				for i = spn3.value to spn4.value by spn5.value do 
 					append sel i
 				polyobj.setSelection subObjectLevel sel
 			)
 		)
 		else (
 			MessageBox "Please select an editable poly (or edit poly modifier)"
 		)
 	
 	)
 )
 -- how to close the existing floater?
 newroll=newrolloutfloater "polyTools" 260 302 750 80
 addrollout SelectSO newroll
 print "addlistnr"
 callbacks.addScript #selectionSetChanged "SelectSO.changeRange" id:#selectsoChangeObject
 
 addrollout SliceIt newroll
 )
 

thanks for your help

edit: when I use


on selectSO open do
	(
		print "addchangelistnr"
		callbacks.addScript #selectionSetChanged "changeRange" id:#selectsoChangeObject
	)

Inside the rollout, the listner is added but the changeRange function seems not to be fired (“changeRange” is not printed)

7 Replies
1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

why do you change max number of spinners’ range? there are several reasons not do it:
#1) number of vertices usually higher than faces and edges anyway
#2) number of sub-objects might change during the editing
#3) changing the range of spinner clamps spinner’s value (reset the working settings)
#4) why do you not change the range of “Step” spinner as well?

I would use the max available number as max range (1e9).

but if nevertheless you want to control the range use #ModPanelSubObjectLevelChanged callback and when topology changes construct.
[i]

– how to close the existing floater?[/i][left]closeRolloutFloater <rolloutFloater>
[/left]

You’re defining SelectSO in a local scope. So the callback cannot access this variable.
Adding this before the rollout definition should help:

global SelectSO
1 Reply
(@ninovanhooff)
Joined: 11 months ago

Posts: 0

it does help, no error anymore. However, changeRange still seems not to fire…
What else might be the problem?

Your callback string should be like this, with parentheses:

callbacks.addScript #selectionSetChanged "SelectSO.changeRange()" id:#selectsoChangeObject

Otherwise it’s just a reference to the function, rather than executing the function

Thanks a bunch, that worked.

Will post it here after polishing the rollout layout, so all can benefit from your tips.

edit: unfortunately I found another bug. Whenever chk1 is checked, an empty selection is made. Why?

I’m not sure if this is the cause of the issue, but the way you’re using the selection BitArray seems a bit odd to me. I would use an index instead of using append:

sel[i] = true

edit: tried it, and you can use append just fine. So never mind then

I just might do that, much simpler

[/i]

I can’t use that since the rolloutfloater variable goes out of scope when the script finishes. When I start the script again, ‘newroll’ is unkown. Maybe if I define global newroll…

The problem with the checkbox really baffles me. So simple…and yet not working.

edit: problem only occurs for the edit poly modifier, not the base object. Its still strange since the only difference is the way the sel bitarray is filled, not api calls.

edit2: solved. I realized I needed to use getselection for some reason so:

if chk1.checked then -- new selection
					polyobj.setSelection subObjectLevel #{} 
				sel = polyobj.getSelection subObjectLevel