Search code examples
3dsmaxmaxscript

How do I generate spinners from an array?


I am trying to create spinners to set the value of Morpher targets, however, num is returning as undefined

`

mf_mod = $.Morpher -- get selected model
    channels = #{} -- empty bitArray
    for i=1 to 100 do channels[i] = WM3_MC_HasData mf_mod i 
    channels = channels as array
    listNames = for num in channels collect WM3_MC_GetName mf_mod num--get target names
        
    fn create_spinners = (
        
        rci = rolloutCreator "myRollout" "My Rollout"
        rci.begin()
        
        for num in channels do (
            
            rci.addControl #spinner listNames[num] listNames[num]
            
            rci.addHandler rci_name #changed paramStr:"val" codeStr:("WM3_MC_SetValue mf_mod num val") 
            
            )
    
            
        createDialog(rci.end())
        
        )
    
    create_spinners()

`


Solution

  • I can see multiple problems with this piece of code:

    • There's an array of channel numbers that don't have to be successive (1,2,20) and array of names (name1, name2, name20) which you address by the numbers - there's only three names in this case yet you'd be trying to get name[20]
    • You are not adding the handler to the control you just created but to the same rci_name (that is undefined in this scope anyway)
    • Object names are used as rollout control identifiers which would break with many object names, better to construct your own
    • You are using 'num' in the code string as a part of the string - as such, it will always be undefined
    • It relies on mf_mod being global variable and there's no error checking
    • Spinners are initialized zeroed out, no matter what the acutal weights are, if the weights are changed by the user in the morpher while the UI isopen, the rollout won't update either - better use the morpher controllers directly
    • This is more of a nitpick but you don't have to convert bitarray to array if all you want is to iterate over it
    (
        fn create_spinners channelData =
        (
            local rci = rolloutCreator "myRollout" "My Rollout"
            rci.begin()
            rci.str += "\tlocal mf_mod = modPanel.getCurrentObject()\n\n"
    
            for item in channelData do
                rci.addControl #spinner ("spn" + item.channel) item.name paramStr:("controller:mf_mod[" + item.channel + "]")
    
            createDialog (rci.end())
        )
    
        local mf_mod = modPanel.getCurrentObject()
    
        if not isKindOf mf_mod Morpher then messageBox "Select morpher modifier" else
        (
            local channelData = for channel = 1 to 100 where WM3_MC_HasData mf_mod channel collect
                dataPair channel:(channel as string) name:(WM3_MC_GetName mf_mod channel)
            create_spinners channelData
        )
    )