[solved] On Button pressed do Multi_Random_fn1,,fn2,fn3


#1

Hi everybody,

I’m trying to create a multiaction script. The script is divided into rollout_1, rollout_2, rollout_3, rollout_4, and each roll contains multiple buttons that have a different function, exmp button_01 random select object, button_02 random add material …

I would need, if possible, to create a final button that will do all the buttons.

rollout_01 - button_01
rollout_02 - button_02
- button_03
- button_04
- button_05
- button_06
rollout_03 - button_07
- button_08
- button_09
rollout_04 - final_Button

The ideal solution would be “on final_Button pressed do
rollout_01 button_01
rollout_02 random do button_02-06 - all buttons use
rollout_03 random do button_07-09 - all buttons use”
Is this a good procedure?
Thank you for your response.


#2

not sure if it can help but you can declare the functions separately and call them one by one or alltogheter

try DestroyDialog temp catch()
rollout temp "do all roll"
(
button bt0 "delete objects" width:80 height:16 align:#center
button bt1 "create boxes"width:80 height:16 align:#center
button bt2 "random wire"width:80 height:16 align:#center
button bt3 "do all"width:80 height:30 align:#center

---functions declaration

fn deleteobjs = (delete objects)
	
fn createboxes = (
					delete objects
					local increment = 0
					for i=1 to 5 do (box lengthsegs:1 widthsegs:1 heightsegs:1 pos:[increment, 0, 0]
					increment += 40)
				)

fn randomwirecolor = (
					for o in objects do o.wirecolor=random black white
					)
				
---button function calls
	
on bt0 pressed do deleteobjs()
on bt1 pressed do createboxes()
on bt2 pressed do randomwirecolor()

on bt3 pressed do (deleteobjs();createboxes();randomwirecolor())
)
createdialog temp 100 300

#3

Hi Conrad,
I’ve tried it and it works if it’s in the same rollout. If I fn add to last button in rollout 4, fn was not found "call needs fn or class got: ButtonControls: btn


#4

try this

::myfunction()

with the :: before


#5

It doesn’t work yet.
Can’t there be a bug in my rollout?
try DestryDialog newRolloutFloater catch ()
(
)
rf = newRolloutFloater 900 1000
addRollout a rf
addRollout b rf
addRollout c rf
addRollout d rf

I tried on btn pressed do btn1 (), on btn pressed do :: btn1 ()


#7

your code is not going to work as it is

i guess you should look up the reference manual for how to make rollouts - subrollouts - floaters
and structure definitions if you need to go that way.

struct functioncontainer (
	fn bananas = (print "bananas"),
	fn apples = (print "Apples")
)



rollout a "Rollout A"
(
button a1 "a1"

on a1 pressed do functioncontainer.bananas()
	
on a resized val do (format "A: %\n" val)
)
--
rollout b "Rollout B"
(
button b1 "b1"

on b1 pressed do (functioncontainer.bananas();functioncontainer.apples())

on b resized val do (format "B: %\n" val)
)
--
rof=newrolloutfloater "test" 200 200
addrollout a rof
addrollout b rof
rof.size=[300,300]

here is an example patching up some reference manual code


#8

Thanks for the answer, “struct” solved it all. So far, I’ve tried to find everything myself, but I’m starting with ms.

last question, is it possible to set random to fn?
exmp: on button pressed do (fn1 (); fn2 (), fn3 (), fn4 ()) — but fn1 () - still first and fn2 (), fn3 (), fn4 () will be random do:
variant 1 — (fn1 (); fn2 (), fn3 (), fn4 ())
variant 2 — (fn1 (); fn3 (), fn2 (), fn4 ())
variant 3 — (fn1 (); fn3 (), fn4 (), fn2 ())


#9

(
	
	fn f1 = format "1\n" 
	fn f2 = format "2\n" 
	fn f3 = format "3\n" 
	fn f4 = format "4\n" 
	fn f5 = format "5\n"
	fn f6 = format "6\n"
	fn f7 = format "7\n"
	fn f8 = format "8\n"
		

	fn RunRandomOrder = 
	(
		local funcs = #( f2, f3, f4, f5, f6, f7, f8 )
		
		for i = funcs.count to 2 by -1 do
		(
			k = random 2 funcs.count 
			swap funcs[1] funcs[ k ] -- shuffle the array
		)
		
		f1()
		
		for func in funcs do func()
		
	)

	RunRandomOrder()

)

#10

Hi Sergey, thank you for your time.
I believe your code is correct, but I have something else wrong. When the button is running, it cannot call fn. First fn from first rollout crush, if I cancel fn 1 the remainder fn will not do anything but will not write an error.

My scheme:
rollout 1
fn 1 (all code)
on btn_1 pressed to 1 () ------ work ok

rollout 2
fn2 (all code)
fn3 (all code)
fn4 (all code)
on btn_2 pressed to 2 () ------ work ok
on btn_3 pressed to 3 () ------ work ok
on btn_4 pressed to 4 () ------ work ok

I tried:
rollout 1
struct function container
(
fn 1 (all code)
)
on btn_1 pressed to functioncontainer1 () ------ work ok


But the final button doesn’t see the way.
exmp: on FinalButon pressed to (functioncontainer.fn1 (), functioncontainer1.fn2 ()) — work ok


I think I should create:
rollout 1
struct function container
(all fn)
rollout 2
struct functioncontainer2
(all fn)
rollout 3
struct functioncontainer3
(all fn)
rollout 4
“Final button”


#11

you need to post as least simplest example of the code that doesn;t work to get the help as there could be a lot of things that could go wrong terminating the execution.
Read about try catch blocks in the reference if you want your code to proceed execution. Maybe that will help


#12

Hi, thanks for the code, it works great.

My problem is that my knowledge is small. I studied something, reworked the whole code structure so that the “fn” were together and then just called them.

I don’t know where to place it (fn f1 = format “1 / n” etc …) but it works without it.

If I ever ask myself as a primitive, sorry.

But when I reworked everything, I rewrote the new code with the old code and lost part, and I don’t know what was already there. Any idea?

This is the line $ .modifiers [#VRayDisplacementMod] .texmap = ???
I need them to add early maps from the listbox.


	on lml2 pressed do
(
	----- loading from matlibrary --------

	prevmlfn = getMatLibFileName()
	mlfn = getMatLibFileName() 
	ff = true
	if chk1d.state == false then 
	(
		fileOpenMatLib()
		mlfn = getMatLibFileName() 
		if prevmlfn == mlfn and matarrayd.count != 0 then ff = false
	)
	if ff == true then
	(
		try
		(
			matarrayd = #()
			loadMaterialLibrary mlfn
			for m in currentMaterialLibrary do 
			(
				--if superclassof m == material then
					if superclassof m == textureMap then
				(
					arrentity = #(m,100)
					append matarrayd arrentity
					refreshlistd()
				)
			)
			--loadMaterialLibrary prevmlfn
			if lbx1d.items.count != 0 then  
			(
				lbx1d.selection = 1
				lbselectedd 1
			)
			else (lbselectedd 0)
		)
		catch
		(
		)
	)
) 

on rnd pressed do
(
	macros.run "Modifier Stack" "Convert_to_Poly"
		modPanel.addModToSelection (VRayDisplacementMod ()) ui:on
			$.modifiers[#VRayDisplacementMod].keepContinuity = on
				$.modifiers[#VRayDisplacementMod].texmap = ------------------------------------???????
					$.modifiers[#VRayDisplacementMod].amount = spn_1a.value
					$.modifiers[#VRayDisplacementMod].shift = spn_2a.value

	--- defining range
	sc = 0
	for m in matarrayd do sc = sc + m[2]
	--- random function in range
	
	--- defining the hit in range
	for o in selection do
	(	
		r = random 0.0 sc
		trg = true
		tmpm = undefined
		s = 0
		for m in matarrayd do
		(
			s = s + m[2] -- greater limit
			if r <= s then 
			(	
				
				if trg then 
				(	
					--print m[1].name
					tmpm = m[1]
				)
				trg = false
			)
		) -- end for matarray
		try
		(
			o.material = tmpm
			o.material.showInViewport = true
		)
		catch()
	) -- end for selection
)

#13

it fully depends what’s inside your listbox, it could or example contain filename or full path to texture

$ .modifiers [#VRayDisplacementMod] .texmap = Bitmaptexture filename:( your_listbox_control.items[ your_listbox_control.selection ] )
or if you want some procedurals
$ .modifiers [#VRayDisplacementMod] .texmap = Checker()


#14

I see only filename. Your first variant work great, i have a lot of textures and i need random add. I think before that I had a function that worked for me, it was quite simply “= m ()” or “= m [1]” or something like that.


#15

I finally solved it this way

$.modifiers[#VRayDisplacementMod].texmap = Bitmaptexture filename:( lbx1d.items [lbx1d.selection + (random 0 spn_limitmax.value )] )

I have to always set “spn”, does anyone have an idea all random from listbox automatic?


#16

Only now have I come to full testing. My current problem is that only the name is written to the modfier.texmap section, but the file is empty, the foreign map is not loaded.

on lml2 pressed do
(
----- loading from matlibrary --------

  					prevmlfn = getMatLibFileName()
  					mlfn = getMatLibFileName() 
  					ff = true
  					if chk1d.state == false then 
  					(
  						fileOpenMatLib()
  						mlfn = getMatLibFileName() 
  						if prevmlfn == mlfn and matarrayd.count != 0 then ff = false
  					)
  					if ff == true then
  					(
  						try
  						(
  							matarrayd = #()
  							loadMaterialLibrary mlfn
  							for m in currentMaterialLibrary do 
  							(
  								--if superclassof m == material then
  									if superclassof m == textureMap then
  								(
  									arrentity = #(m,100)
  									append matarrayd arrentity
  									refreshlistd()
  								)
  							)
  							--loadMaterialLibrary prevmlfn
  							if lbx1d.items.count != 0 then  
  							(
  								lbx1d.selection = 1
  								lbselectedd 1
  							)
  							else (lbselectedd 0)
  						)
  						catch
  						(
  						)
  					)
  				) 

this is my schematic for loading materials into a multilistbox.