Notifications
Clear all

[Closed] multilistbox vs render elements problem

hello everyone, I’m asking for help again
i have a multilistbox collecting channels in render elements dialog as array items.
pressing the delete button with one item selected working fine, but 2 or more selected items don’t work correctly. it deletes every second render element of multilistbox selection :surprised . here’s the code

(
	local re = maxOps.GetCurRenderElementMgr()
	local elementlist = #(VRay_Atmosphere,VRay_Background,VRay_BumpNormals,VRay_Caustics,VRay_DiffuseFilter, \
		VRay_GlobalIllumination,VRay_Lighting,VRay_MatteShadow,VRay_Normals,VRay_Reflection, \
		VRay_ReflectionFilter,VRay_Refraction,VRay_RefractionFilter,VRay_SelfIllumination,VRay_Specular,VRay_MtlID, \
		VRay_ObjectID,VRay_RenderID, VRay_WireColor)
	local theManager = maxOps.GetRenderElementMgr #Production
	local els = #()
	
	
	fn renderElementsList = 
	(
		for y = 0 to (theManager.numrenderelements() - 1) do
		(
			el = re.getrenderelement y
			append els (el.elementname as string)
		)
	)
	
	
	rollout renderElements "Render Elements" width:184 height:408
	(
		button elem_add "Add" pos:[8,8] width:56 height:24
		button elem_del "Delete" pos:[64,8] width:56 height:24
		button elem_clear "Clear" pos:[120,8] width:56 height:24
		multilistbox elem_list "" pos:[8,40] width:168 height:25
		button elem_ok "OK" pos:[8,376] width:84 height:24
		button elem_cancel "Cancel" pos:[92,376] width:84 height:24
		
		
		on renderElements open do
		(
			els = #()
			renderElementsList()
			elem_list.items = els
		)
		
		on elem_add pressed do
		(
			re.removeallrenderelements()
			setsilentmode true
			for n in elementlist do re.addrenderelement (n elementname:(n as string))
			els = #()
			renderElementsList()
			elem_list.items = els
		)
		
		on elem_del pressed do
		(
			for i in elem_list.selection do
			(
				delID = re.GetRenderElement (i-1)
				re.RemoveRenderElement delID
			)
			
			elem_list.selection = #()
			els = #()
			renderElementsList()
			elem_list.items = els
		)
		
		on elem_clear pressed do
		(
			re.removeallrenderelements()
			re.SetElementsActive false
			
			els = #()
			renderElementsList()
			elem_list.items = els
		)
	)
	
	createdialog renderElements style:#(#style_toolwindow, #style_sysmenu)
)

please help to solve this problem

4 Replies
 JHN

Try this


for i in elem_list.selection as array do
...etc...

Multilist box uses a bitarray value for the list. Convert it to array and it should work,

-Johan

1 Reply
(@e-armand)
Joined: 11 months ago

Posts: 0

the problem hasn’t gone :banghead:
i select first 4 items (VRay_Atmosphere, VRay_Background, VRay_BumpNormals, VRay_Caustics), next item’s name is VRay_DiffuseFilter (not currently selected). then click “delete” – VRay_Atmosphere and VRay_BumpNormals are gone, so do VRay_DiffuseFilter! but VRay_Background and VRay_Caustics stay in the list…

UPD and if i select all the items and delete them, only the half of list is gone.
here’s the simplified example

(
	rollout unnamedRollout "Untitled" width:162 height:300
	(
		multilistBox lbx1 "ListBox" pos:[12,13] width:133 height:15
		button btn1 "Button" pos:[13,250] width:132 height:30
		
		on unnamedRollout open do lbx1.items = #("a1","a2","a3","a4","a5","a6","a7","a8","a9","a0")
		on btn1 pressed do
		(
			for i in lbx1.selection as array do
			(
				lbx1.items = deleteitem lbx1.items i
			)
			lbx1.selection = #()
		)
	)
	createdialog unnamedRollout
)
 JHN

You have to reverse delete the items as well. When you delete an item from an array from the beginning the array gets shorter. So the numbers in the higher parts won’t match either.


fn reverse v1 v2 = if v1 > v2 then -1 else 1
local theitems = elem_list.selection as array
qsort theItems reverse
for i in theItems do ... etc.

Not behind max, so you have to check, but the principle is good, walk the array in reverse.

-Johan

1 Reply
(@e-armand)
Joined: 11 months ago

Posts: 0

great. thanks a lot! :applause:
here’s the final example working code

(
	rollout unnamedRollout "Untitled" width:162 height:300
	(
		multilistBox lbx1 "ListBox" pos:[12,13] width:133 height:15
		button btn1 "Button" pos:[13,250] width:132 height:30

		on unnamedRollout open do lbx1.items = #("a1","a2","a3","a4","a5","a6","a7","a8","a9","a0")

		on btn1 pressed do
		(
			fn fixItems v1 v2 = if v1 > v2 then -1 else 1
			local theitems = lbx1.selection as array
			qsort theItems fixItems
			for i in theItems do 
			(
				fixItems i (i+1)
				lbx1.items = deleteitem lbx1.items i
			)
			lbx1.selection = #()
		)
	)
	createdialog unnamedRollout
)