[Closed] Dynamic Rollout Creation
Figured I would post for the sake of future generations a stripped down generic version of a script I’ve been working on for Straightface Studios in Seattle. Hope it can be of some use in your projects as well.
It’s a simple script. There is a function to set the number of text boxes. Whenever the spinner is changed up top the number of rollouts changes to match. If there is any text in the box the rollout title changes.
*— Edited to reflect stringstream feedback —
global variety2 = 0
fn changerollouts variety =
(
if variety2 < variety then
(
for j = 1 to (variety - variety2) do
(
i = (variety2 + j) as string
-- Begin Dynamic Rollout Generation --
codestream = "" as stringstream
format ("
rollout box_% \"Box %\" height:400
(
edittext data width:133 pos:[6,6]
on data changed true do
(
if data.text.count > 0 then
(
boxer.subroll02.box_%.title = data.text
)
else
(
boxer.subroll02.box_%.title = \"Box %\"
)
)
)
") i i i i i to:codestream
format "addsubrollout boxer.subroll02 box_%" i to:codestream
execute (codestream as string)
codestream = undefined
-- End Dynamic Rollout Generation --
)
)
else
(
if variety2 > 1 then
(
for j = 1 to (variety2 - variety) do
(
i = (variety2 - j + 1) as string
-- Begin Dynamic Rollout Destruction --
codestream = "" as stringstream
format "removesubrollout boxer.subroll02 box_%" i to:codestream
execute (codestream as string)
codestream = undefined
-- End Dynamic Rollout Destruction --
)
)
)
variety2 = variety
)
rollout boxer "Boxes" height:360 width:200
(
subrollout subroll01 ""
subrollout subroll02 ""
)
rollout setup "Setup"
(
spinner vrty "Products:" type:#integer range:[1,15,1] pos:[32,7] width:65
on vrty changed true do
(
changerollouts vrty.value
)
)
createdialog boxer
AddsubRollout boxer.subroll01 setup
boxer.subroll02.pos += [0,55]
boxer.subroll02.height += 250
boxer.subroll01.pos += [0,5]
boxer.subroll01.height += 45
changerollouts 1
i read that maxscript leaves a copy of the old string in memory whenever you add something to it. my tests have shown that this is very true…
this will cause problems over time if you use it heavily like in this case.
what i do now whenever i construct rollouts or custom attributes dynamically is use stringstreams.
this is also easier to read and maintain i think
rolloutSS = stringStream ""
format "rollout rlt \"rlt name\"
" to:rolloutSS
format "(
" to:rolloutSS
for i=1 to 10 do
(
format "button btn% \"Button %\"
" i i to:rolloutSS
)
format ")
" to:rolloutSS
createDialog (execute(rolloutSS as string))
rolloutSS = undefined
trust me you want to think about this kind of thing with maxscript
Ooo good tip thanks. So you’re saying every time it runs execute “string” the string it executes gets saved into memory? Or in other words it’s a giant memory leak.
- CODE changed. Is that correct now?
looks good, though if you ‘format’ such a large code block at once it may become a bit of a hassle to manage all those variables.
it doesn’t happen with the execute, it happens when you append something to a string using + or +=