Home Technical Talk

Maxscript - Applying script to multiple objects

polycounter
Offline / Send Message
Justo polycounter
I've created some scripts in the past, but most of them were grabbed from the Listener and even then they were pretty basic. Now I wanted to automate the applying of a quad chamfer + turbosmooth with some settings, but I cannot make it work when doing it for multiple objects. After some reading, the best I could come up with was this:

macroScript chamfnSmooth
category:"Jus Tools"
toolTip:"Chamfer Smoother"

sel = selection as array
for obj in sel do
(
modPanel.addModToSelection (Quad_Chamfer ()) ui:on
obj.modifiers[#Quad_Chamfer].chamfer_type = 1
obj.modifiers[#Quad_Chamfer].quad_intersections = 1
modPanel.addModToSelection (TurboSmooth ()) ui:on
obj.modifiers[#TurboSmooth].iterations = 2
obj.modifiers[#TurboSmooth].explicitNormals = on
)
)


However, this will unfortunately repeat itself on every object, times the amount of my selection's objects. So for example if I have two cubes selected when I use this, it will create four modifiers for every cube.

tl;dr How can I make scripts work for multiple objects? Do I have to make some counter inside my array that keeps track of everything?

Replies

  • monster
    Options
    Offline / Send Message
    monster polycounter
    You were very close you just needed to change your selection for modPanel.addModToSelection to work correctly.

    NOTE: I don't have Quad_Chamfer, so I just tested with Chamfer (which has quad support since v2015).

    macroScript chamfnSmooth
    category:"Jus Tools"
    toolTip:"Chamfer Smoother"
    ( 
    	sel = selection as array
    	for obj in sel do
    	(
    		--SELECT ONE OBJECT
    		select obj
    		
    		modPanel.addModToSelection (Chamfer()) ui:on
    		obj.modifiers[#Chamfer].chamfertype = 1
    		obj.modifiers[#Chamfer].segments = 1
    		modPanel.addModToSelection (TurboSmooth ()) ui:on
    		obj.modifiers[#TurboSmooth].iterations = 2
    		obj.modifiers[#TurboSmooth].explicitNormals = on
    	)
    	
    	--RESELECT MY ORIGINAL SELECTION
    	select sel
    )
  • monster
    Options
    Offline / Send Message
    monster polycounter
    Just to be thorough...

    You can make the script faster in several ways. And more readable too. :)

    1. Avoid changing selection by using AddModifier as opposed to ModPanel.AddModToSelection. Selection changes update the UI and make things very slow.
    2. Create only one pair of modifiers in memory and apply copies to the objects. Duplicating memory is faster than using the CPU to create a maxscript object.
    3. Only access modifier properties once instead of accessing them in a loop. Anything you can do outside of a loop is always faster.
    4. Accessing properties by a name is slower than using a variable. (obj.modifiers[#TurboSmooth].iterations VS tMod.iterations)

    With these updates running the script on 56 boxes went from 4.261 seconds to 0.056 seconds.

    macroScript chamfnSmooth
    category:"Jus Tools"
    toolTip:"Chamfer Smoother"
    ( 
    	cMod = Chamfer()
    	cMod.chamfertype = 1
    	cMod.segments = 1
    	
    	tMod = TurboSmooth()
    	tMod.iterations = 2
    	tMod.explicitNormals = true
    	
    	for obj in selection do
    	(
    		addModifier obj (copy cMod)
    		addModifier obj (copy tMod)
    	)
    )
  • Justo
    Options
    Offline / Send Message
    Justo polycounter
    @monster
    Thanks Juan!! I really thank you for taking the time to write all those tips too; they seem simple enough for a beginner like myself to understand what they're about. Really cool that you moved everything to variables (still find it weird that maxscript doesn't need us to define what those variables are, ha...) so that you would simplify the contents of the loop. 


     I just tested your version and it works like a charm. However, they seem to be applying individually per object. How can I modify this so that these two modifiers are applied to the whole selection, so that by changing the parameters of one, the rest of the objects are affected by this? To explain myself better, I am simply trying to replicate Max' normal behaviour when applying a modifier to a selection of objects. 

    Also, congrats with Lucky's Tale's launch last month :) I had a chance to play it; was awesome. 
  • monster
    Options
    Offline / Send Message
    monster polycounter
    Thanks!

    About the script, I just assumed you didn't want an instanced modifier since you were creating them in a loop.

    Applying an instance modifier is actually easier and faster.

    macroScript chamfnSmooth
    category:"Jus Tools"
    toolTip:"Chamfer Smoother"
    (
    	cMod = Chamfer()
    	cMod.chamfertype = 1
    	cMod.segments = 1
    	
    	tMod = TurboSmooth()
    	tMod.iterations = 2
    	tMod.explicitNormals = true
    	
    	--MAKE SURE THE MOD PANEL IS UP	
    	max modify mode
    	modPanel.AddModToSelection cMod
    	modPanel.AddModToSelection tMod
    )
  • Amsterdam Hilton Hotel
    Options
    Offline / Send Message
    Amsterdam Hilton Hotel insane polycounter
    Great stuff Juan, thanks for doing all the different implementation examples.
  • Justo
    Options
    Offline / Send Message
    Justo polycounter
    Thanks a million Monster :) You've given me lots of stuff to learn about. Have a nice day Juan! 
Sign In or Register to comment.