Question

I've been working in Maxscript for about 3 months now, so I am fairly new, and i recently came across the ability to execute strings inside of ms files like so:

str = "print \"hello\""
execute str

The Max Listener would then output "hello", as you might expect. i'm trying to use this functionality to create a dynamic rollout that creates a button for every parameter linked to a RailClone object in my scene. The RailClone object isn't important to the question, so I'm pretty sure you shouldn't need to know anything about it to help.

Anyway, here is my code:

    --create an array of button names
    btn_names = for p in obj.paname collect ("btn_" + p as string)
    str = "group \"Custom Parameters\" \n(\n"
    print btn_names
    for p in 1 to btn_names.count do
    (
        str += "button " + btn_names[p] + "\n"
        str += "on " + btn_names[p] + " pressed do\n"
        str += "try\n(\n" + "\tprint \"hello test\"\n"
        str += ")\n" + "catch(messageBox \"Script failed\")\n"
    )
    str += ")\n"
    str = "print \"hello\""
    print str
    execute str

The first line grabs all of the object's parameters, but it just ends up being an array of strings. My issue occurs on the execute line, and the output is:

MAXScript Rollout Handler Exception: -- Runtime error: group() requires a node or node collection, got: "Custom Parameters" <<

As far as I know, all of my formatting is correct, so is there something else I must do? I appreciate any and all help. Thank you.

Was it helpful?

Solution

There are a few issues here, first is that rollout controls need to be declared in a scope of a rollout. Group keyword means something entirely different outside that scope (i.e. the command to create a group of nodes, which is also the reason of the error message). You would have to create the complete rollout this way (look at rollout creator source code – while it has quite a lot of issues on its own, there is no need to reinvent the wheel). The second issue that could bite you in long term is that execute evaluates the supplied string in global scope, every variable you don't declare there but wish to have access to needs to be accessible from the global scope and any new non-nested variable becomes global too.

Anyway, feel free to use rollout creator for pieces of code like this where the resulting dialog is quite simple, and for anything more dynamic reach to .NET forms. Being able to work with collections of controls as objects makes it much more manageable, yet the .NET verbosity might be a bit off-putting.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top