[Closed] how to prevent Modifier added twice
Hi all, I am making a special modifier. I do not want user to apply it to one object twice. Is it possible to prevent that in script? Thanks!
You can loop through the modifier stack of the object and test each one if it is the one your trying to add.
note, however, that this doesn’t prevent the user from adding the modifier. You could check the stack of the object it is applied to* and make the modifier inactive (no code run, UI showing a warning, perhaps) if the object already has such a modifier applied.
- keep in mind that a single modifier may be applied to multiple objects via instancing
You could go so far as automatically removing the modifier after it is applied, but you have to do so through a timer (so that it removes the modifier outside of the modifier’s creation scope) – otherwise max becomes unstable.
Couldn’t you incorporate such a script in a way, that it is called whenever the user tries to add the modifier?
for o in selection do (if (for m in o.modifiers where m.name == YourMod collect m).count > 0 do addmodifier o YourMod)
That should prevent the modifier to be assigned to an object that has the modifier in its stack already. (But not if it is renamed…is there also a modifier.type?)
That’s easy enough – the scripted modifier plugin would have its node binding code triggered whenever it is assigned to a node for the first time.
wouldn’t that just add another modifier -if- the modifier already exists?
i.e.
for o in selection – loop over the selection
for m in o.modifiers – loop over the modifiers
where m.name == YourMod – get all modifiers named YourMod*
collect m – collect that modifier into an array
.count > 0 – if the array is non-empty (i.e. the object HAS a modifier named YourMod)
addmodifier o YourMod – add a YourMod modifier to the object
It wouldn’t, actually, even if you changed it to .count == 0. All that would happen is that the modifier is already added once (via 3ds Max’s internals), and then next the code would not add it another time. But you’re still stuck with the one 3ds Max’s internals added.
‘classOf <modifier>’ or, even better, ‘<modifier>.classID’
But, again, the basic ‘problem’ is that…
- you cannot prevent a modifier from being applied by the user, unless you want to hide the modifier panel and maxscript from them and force them to go through your own user interface
- you cannot hide a modifier based on whether it is applicable to the object.
The 2nd functionality -is- available in 3ds Max API – it’s what prevents you from applying, say, Edit Spline to a GeoSphere; ‘Edit Spline’ is simply not listed in the modifiers dropdown at all (and if you use buttons, the button is disabled). But this is not exposed to scripted (modifier/simplemod) plugins, and I’m not sure if the API allows you to do a full check of the selected object(s) either, or whether you can only test base classes.
Edit: Apparently, no – the validity of a modifier as related to the current selection is determined via ‘InputType() { return Class_ID(<class>,0); }’, so it is limited to filtering by types of objects – not specific details about those objects.
Yeah, sorry. I have been a bit to hasty…should have been ‘==’ instead of ‘>’
But since it doesn’t work anyway…I just thaught that you could write something like this into the modifier itself, so that adding it doesn’t have an effect, when there is such a modifier in the stack already.