[Closed] macroscript check button?
hi, im making a simple toggle on/off script, and i wanted to toggle it right from my interface, so i was wondering how do i make a macroscript button to be checkable, instead of just clickable?
currently my script is like this:
macroScript Toggle category:"Rafael"
(
try(destroyDialog ToggleSmooth)catch()
global Toggle_Smooth
global MS_array = #()
global TS_array = #()
rollout ToggleSmooth "Toggle Smooth on/off" width:150 height:40
(
CheckButton TSmooth "Smooth" highlightColor:green height:20 align:#left offset:[-5,0] across:2
fn TSSelectON =
(
start = timeStamp()
for o in geometry do if superclassof o == geometryClass do
(
for mod in o.modifiers where classof mod == turbosmooth do
(
if o.turbosmooth.enabled == false then
(
o.turboSmooth.enabled = true
)
append TS_array o.name
)
for mod in o.modifiers where classof mod == meshsmooth do
(
if o.meshsmooth.enabled == false then
(
o.meshsmooth.enabled = true
)
append MS_array o.name
)
)
end = timeStamp()
format "Processing took % seconds
" ((end - start) / 1000.0)
)
fn TSSelectOFF =
(
start = timeStamp()
for o in geometry do if superclassof o == geometryClass do
(
for mod in o.modifiers where classof mod == turbosmooth do
(
if o.turbosmooth.enabled == true then
(
o.turboSmooth.enabled = false
)
append TS_array o.name
)
for mod in o.modifiers where classof mod == meshsmooth do
(
if o.meshsmooth.enabled == true then
(
o.meshsmooth.enabled = false
)
append MS_array o.name
)
)
end = timeStamp()
format "Processing took % seconds
" ((end - start) / 1000.0)
)
on TSmooth changed state do
(
if state == on then
(
TSSelectON()
)
else
(
TSSelectOFF()
)
)
on ToggleSmooth close do
(
TS_array = #()
MS_array = #()
)
)
createdialog ToggleSmooth width:220 height:30
)
but this will open another UI, wich is useless for me, and i wanted to have it right off the shelf.
how to do so?
thanks
I don’t quite understand what you’re looking for, the checkbutton is working just fine here, if you want a check, why not use a checkbox? Dunno…
i thought i made it very clear, but let me try again
currently, it works like this, i drag the macroscript button to my toolbar, and when i click it, another interface opens with another button yet. and i wanted to make the actual macroscript button checkable, and have the commands assigned directly to it, instead of opening another window when clicking the first button.
thanks in advance
Here is an optimized and simplified version of your code that does the same and checks the MacroScript button itself:
macroScript Toggle category:"Rafael"
(
local state = false --this variable lives in the Macro and controls its checked state
fn TSSelectONOFF newState = --one functions can do both on and off
(
start = timeStamp()
for o in geometry do --there is no reason to check if a Geometry obj is GeometryClass
(
for m in o.modifiers where classof m == turbosmooth and m.enabled != newState do m.enabled = newState
for m in o.modifiers where classof m == meshsmooth and m.enabled != newState do m.enabled = newState
)
end = timeStamp()
format "Processing took % seconds
" ((end - start) / 1000.0)
)
on isChecked return state --check or uncheck the Macro button
on execute do --if the Macro button was pressed,
(
state = not state --switch the checked state
TSSelectONOFF state --and call the function to turn on or off the smoothing
)
)
You can optimized further by combining the two modifier loops into one:
for m in o.modifiers where (classof m == turbosmooth or classof m == meshsmooth) and m.enabled != newState do m.enabled = newState
Even better, there is no reason to check if the state is set or not already, you can just enforce it:
for m in o.modifiers where (classof m == turbosmooth or classof m == meshsmooth) do m.enabled = newState
thanks bobo, thats exactly what i wanted
i actually just changed the geometry for selection, as suggested by a guy on another thread, so i guess i should keep the geometry check? anyways it works just like i wanted now
heres the final version:
macroScript Toggle category:"Rafael"
(
local state = false --this variable lives in the Macro and controls its checked state
fn TSSelectONOFF newState = --one functions can do both on and off
(
start = timeStamp()
for o in selection do if superclassof o == geometryClass do
(
for m in o.modifiers where (classof m == turbosmooth or classof m == meshsmooth) do m.enabled = newState
)
end = timeStamp()
format "Processing took % seconds
" ((end - start) / 1000.0)
)
on isChecked return state --check or uncheck the Macro button
on execute do --if the Macro button was pressed,
(
state = not state --switch the checked state
TSSelectONOFF state --and call the function to turn on or off the smoothing
)
)
cheers, thanks for all your help
Ahhh, now I get it I actually had the same problem some time ago, I didn’t get it the first time because I ran your script without the macroscript part Thank god we have Bobo
LOL that script looks real real familiar but you have my permission to call it your own
Nice to see someone uses or does something with it it is not very efficient.
I stumbled on this looking for the same solution as you, a main toolbar checkbutton.
Thanks for the solution and smarter snippets Bobo!
EDIT: BTW thanks for reminding me to switch the 'mod' variable for the variable m
it looks too similar to be called anyone else’s then yours.
whatever…
if you have a lot of turbo/mesh smooth modifiers to toggle and you want to do it faster it’s better to collect them all and change their enable state at once:
mods = #()
for node in <nodes> where iskindof node GeometryClass do
for modi in node.modifiers where iskindof modi TurboSmooth or iskindof modi MeshSmooth do appendifunique mods modi
if mods.count > 0 do
(
suspendEditing()
mods.enabled = <state>
resumeEditing()
)
whatever I was just being an ass…
suspend/resumeEditing(), makes sense, thanks