[Closed] Custom attributes breaking on save/load
I have custom maxscript attributes for objects, but somehow as soon as I save and reload a scene that uses them, all values in the attributes are reset to 0. After that, whenever I change them again, deselect the object and select it again, the attributes are also 0 again. The attributes functioned completely fine as long as I don’t save and then load the scene again.
The definition is in a script file that is set to 3dsmax’s “third party plugins”-group in the “configure system paths”-menu. This way the scripts are always loaded and I can use my plugins.
This is the definition:
obstacleAttributes = attributes CRObstacleAttributesDefinition attribID:#(0x3ef0d9df, 0xe15d5f6)
(
parameters main rollout:params
(
crashSound type:#string ui:crashSoundEditText default:""
pointsOnHit type:#integer ui:pointsOnHitSpinner default:0
)
rollout params "CRObstacleParameters"
(
group "Crash sound group:"
(
editText crashSoundEditText ""
)
group "Points object:"
(
spinner pointsOnHitSpinner "Points on hit:" type:#integer range:[-100000, 100000, 0]
label info1 "(0 means normal obstacle)"
)
)
)
I use this code to add the custom attributes to objects that I have selected:
for obj in $ do
(
if (CustAttributes.get obj obstacleAttributes) == undefined then
(
CustAttributes.add obj obstacleAttributes
)
)
This is in 3dsmax 2009, by the way.
I have no idea why this is happening. Does anyone know what I might be doing wrong here?
Thanks in advance!
I’m not sure what the problem is, your code works fine for me (also 2009).
Perhaps the problem is elsewhere?
Hmm, it’s not working for me in 2009.
- Create an object
- Apply CA code
- Save File
- Load File (No need to restart Max)
- CA on object no longer visible in UI, but is stored in parameters. Changing values no longer stored, and unselecting and reselecting node resets to default value.
Ah, got it. The documentation isn’t very clear, but you need to handle the events in order to load and save the information after a file>load.
Adding this to the rollout fixes the issue:
on crashSoundEditText changed val do crashSound = val
on pointsOnHitSpinner changed val do pointsOnHit = val
on params open do (
crashSoundEditText.text = crashSound
pointsOnHitSpinner.value = pointsOnHit
)
I gave this a try and it indeed works, thanks!
However, I don’t really understand why this is needed. The “ui” parameter in the Parameters block should be doing exactly this, right? The Maxscript documentation even gives an example without the event callbacks you added. It is also made explicit that the “ui” part handles these changes:
The optional parameter ui:<ui_def> is used to specify which user-interface element in the parameter block’s associated rollout is to be linked to this parameter. When so linked, the handling of user actions for these elements are automatic and they also update automatically to follow any changes to the parameter caused by animation, scripts, etc.
I tried it and indeed your suggestion fixes it, but I don’t understand why. Is this maybe a bug in Maxscript or a mistake in the documentation?
i think that the problem in the initialization of a string parameter. by default it’s undefined.
i can’t check the thought now but try to change definition to:
crashSound type:#string default:"" ui:crashSoundEditText
another solution that might help is to add get event handler:
on crashSound get val do if val == undefined then "" else val