{"id":887,"date":"2008-05-09T16:14:03","date_gmt":"2008-05-09T22:14:03","guid":{"rendered":"http:\/\/www.mrbluesummers.com\/?p=887"},"modified":"2008-05-09T16:14:03","modified_gmt":"2008-05-09T22:14:03","slug":"tick-based-ui-updating-in-maxscript","status":"publish","type":"post","link":"http:\/\/www.mrbluesummers.com\/887\/3d-tutorials\/tick-based-ui-updating-in-maxscript","title":{"rendered":"Tick-Based UI Updating in MaxScript"},"content":{"rendered":"

This is going to make all you computer science buffs cringe, but today I learned a really dirty way of managing 3dsMax UI states in a Maxscript rollout or DotNet form. By hooking up a UI refresher to a timer object, you can have your front-end refresh dozens or hundreds of times per second- probably without a performance hit!<\/p>\n

Consider the following script:
\n
\nglobal hForm, mCheckbox, thisTimer
\n(
\n fn whenCheckboxIsPressed a b =
\n (
\n if MCRUtils.IsCreating Box
\n then (Max Select; try((hForm.controls.item(0)).checked = false) catch())
\n else Macros.run 26615
\n )<\/p>\n

fn updateUI =
\n (
\n try
\n (
\n hForm.controls.item(0)).checked = MCRUtils.IsCreating Box
\n ) catch()
\n )<\/p>\n

--Create a DotNet Checkbox
\nmCheckbox = dotNetObject \"System.Windows.Forms.Checkbox\"
\nmCheckbox.appearance = (dotnetClass \"System.Windows.Forms.Appearance\").Button
\nmCheckbox.text = \"Box Button\"
\nmCheckbox.location = dotNetObject \"System.Drawing.Point\" 0 0<\/p>\n

--Create a DotNet Form
\nglobal hForm = dotNetObject \"System.Windows.Forms.Form\"
\nhForm.topmost = true
\nhForm.controls.add mCheckbox --add the Button to the Form<\/p>\n

thisTimer = dotNetObject \"System.Windows.Forms.Timer\"
\nthisTimer.interval = 10<\/p>\n

--Add an Event Handler for the click event:
\ndotNet.addEventHandler mCheckbox \"click\" whenCheckboxIsPressed
\ndotNet.addEventHandler thisTimer \"tick\" updateUI<\/p>\n

hForm.show() --show the Form with the Checkbox
\nthisTimer.start()
\n)<\/code>
\nFirst, we declare our important Maxscript variables globally so that they can be accessed anywhere. This is a scoping issue; use this technique to taste.<\/p>\n

Then we declare the function for when the checkbox is pressed, and accept the sender and event args in a<\/span> and b<\/span> respectively. In this function, we dive into the hForm.controls collection and retrieve the control to set it’s “checked” parameter. We set a Try()Catch() block around it because the timer will continue for a short while after the window is closed. Without this expression, you’ll get an error 100 times per second and you’ll have to restart 3dsMax.<\/p>\n

The second function is the UI update. In this case, we just set the “checked” parameter equal to a Boolean expression checking to see if the user is engaged in some activity. In this case, we want to know if the user is creating a box. For more of these cases, read the Macroscripts included in 3dsMax and you can derive all sorts of neat “Is the user doing this” type expressions.<\/p>\n

The rest is fairly self explanatory. We just create the checkbox, and assign it the visual characteristics we need. We create the DotNet form and assign the checkbox to it, and then create the timer and give it an interval rate of 10 (that is, 1000\/10 or 100x per second).<\/p>\n

We then link up the two event handlers to functions we declared earlier (button click and update ui), and create the form.<\/p>\n

Ta da! The button walks, talks, and acts like a macroscript button, but it’s actually being controlled by DotNet functionality. This means<\/p>\n

    \n
  1. More robust visual design for UI items,<\/li>\n
  2. More robust code options and possibly faster performance, and<\/li>\n
  3. When you use my library some day, you’ll be able to get native macroscripts to work alongside your own snazzy code!<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"

    This is going to make all you computer science buffs cringe, but today I learned a really dirty way of managing 3dsMax UI states in a Maxscript rollout or DotNet form. By hooking up a UI refresher to a timer object, you can have your front-end refresh dozens or hundreds of times per second- probably without a performance hit!<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,16],"tags":[35,44,72,2565,89],"_links":{"self":[{"href":"http:\/\/www.mrbluesummers.com\/wp-json\/wp\/v2\/posts\/887"}],"collection":[{"href":"http:\/\/www.mrbluesummers.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.mrbluesummers.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.mrbluesummers.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.mrbluesummers.com\/wp-json\/wp\/v2\/comments?post=887"}],"version-history":[{"count":0,"href":"http:\/\/www.mrbluesummers.com\/wp-json\/wp\/v2\/posts\/887\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.mrbluesummers.com\/wp-json\/wp\/v2\/media?parent=887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrbluesummers.com\/wp-json\/wp\/v2\/categories?post=887"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrbluesummers.com\/wp-json\/wp\/v2\/tags?post=887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}