February 12, 2012

Dynamic UI creation with MaxScript


There are 3 ways to dynamically modify a MaxScript UI.

One way to to create all the UI elements you need in order to disable and enable them as needed. (This is the most common method)

A similar method is to use their visible property to turn them off and on.

The most complex way is to generate a maxscript on the fly and use the execute command to create the UI elements.

This script has an example of each method: Martinez_DynamicUI.ms

3 comments:

  1. also do not forget to mention the "rolloutCreator", this is a standard maxscript structure of functions for dynamically creating rollouts.

    the system behind it is identical as executing a string that describes the rollout.

    example :

    rci = rolloutCreator "myRollout" "My Rollout"

    rci.begin()

    rci.addControl #button #myButton "My Button"

    rci.addHandler #myButton #pressed filter:on codeStr:"MessageBox @Isn't this cool@ title:@Wow@"

    createDialog (rci.end())

    ReplyDelete
    Replies
    1. Wow thanks, I actually never used that method. Thanks for the tip!

      Delete
  2. hi, great tips, this what i'm exactly looking for. But when i try to implent this i cannot reach dynamicaly created ui elements. the code is below it throws out an error when i try to reach dropdown list. And it sees everyting dynamicaly created as undefined.



    DynamicUIRollout = newRolloutFloater "Dynamic UI" 200 300


    rollout UIDialogTest "Submaterial Instancer" width:200 height:300
    (
    local UInum = 1
    local multiMat
    local nameArray = #(#(),#())


    button btnExecute "Pick Material" pos:[5,5]
    button btnStart "Start Instancing" pos:[90,5]

    on btnExecute pressed do
    (
    if classof $.mat == multimaterial then
    (
    multiMat = $.mat
    for i = 1 to multiMat.count do
    (
    nameArray[1][i] = multiMat[i].name
    )
    nameArray[2] = copy nameArray[1] #nomap
    sort nameArray[1]
    print nameArray[1]

    )

    UIcode = "rollout subRoll" + UInum as string + " \"Choose Materials to instance" + "\"\n"
    UIcode += "("

    UIcode += "dropdownList drop" + i as string + "\"" + "Base Mat" + "\"width:165 height:15 border:false \n"

    for i=1 to multiMat.count do
    (
    UIcode += "checkbox btnMat" + i as string + "\"" + nameArray[1][i] + "\"width:165 height:15 border:false \n"
    )
    UIcode += ")"
    UIcode += "addRollout UIDialogTest" + UInum as string + " DynamicUIRollout"
    execute UIcode

    UInum += 1

    drop.items = nameArray[1]

    )

    on btnStart pressed do
    (
    print UIDialogTest.controls[i]
    )
    )

    ReplyDelete