Home Technical Talk

First steps into MxScript

polycounter lvl 11
Offline / Send Message
MattyWS polycounter lvl 11
Hello all! I've been keeping busy trying to learn MEL script but since most of my office use Max, I started doing a bit of Maxscript too. I'm completely self taught at this point and I'm having a little issue. I'm sure it's something simple but I haven't been able to solve this. I have a button with the function as follows;
on ButtonSmooth pressed do
	(
			ViewPortVal = SIterations.value
			RenderVal = RIterations.value
		
			modPanel.addModToSelection (TurboSmooth ()) ui:on
			$.modifiers[#TurboSmooth].iterations = ViewPortVal
			$.modifiers[#TurboSmooth].useRenderIterations = on
			$.modifiers[#TurboSmooth].renderIterations = RenderVal
		
		)

As I'm sure you can guess, it simply turbosmooth's the object. And it works fine on one object, however I want to be able to use this on multiple objects at a time but it throws an error at me when I try it. Can anyone see why it wouldn't work on a selection of objects?

Any help is much appreciated! =)

Thanks,
Matty

Replies

  • 2cat
    Options
    Offline / Send Message
    2cat polycounter lvl 5
    I'm no expert, but if I remember correctly $ is the current selected object. I don't think it can be multiple, and if it can, I don't think you can apply 1 modifier to multiple objects. Probably you have to cycle through all selected objects and add the modifier per object. Maybe something like (not max syntax I'm sure)

    foreach(object in selection)
    {
    do stuff
    }
  • leslievdb
    Options
    Offline / Send Message
    leslievdb polycounter lvl 15
    this is because if you have multiple object selected you`ll be working with an array instead of just one object.
    what you`ll need to do is go over each element and run that same bit of code

    so for example
    on ButtonSmooth pressed do 
    (     
        ViewPortVal = SIterations.value            
        RenderVal = RIterations.value        
    
        for obj in selection do
        (
            if obj.modifiers[#TurboSmooth] == undefined then addModifier obj (turbosmooth()) -- only add modifier when it doesnt exist yet   
            obj.modifiers[#TurboSmooth].iterations = ViewPortVal             
            obj.modifiers[#TurboSmooth].useRenderIterations = on             
            obj.modifiers[#TurboSmooth].renderIterations = RenderVal               
        )
    )
    
  • monster
    Options
    Offline / Send Message
    monster polycounter
    2cat is right, you need to loop the selection. However, you can apply a single modifier (instance) to multiple objects, but I don't think that's his intention. I've gotten in the habit of not using $ when making tools for others. Instead I always operate on the selection.

    To fix your code, just use a variable to adjust the modifier. This will result in an instanced modifier on all the objects. So changing one turbosmooth will change the rest.
    	on ButtonSmooth pressed do
    	(
    			ViewPortVal = SIterations.value
    			RenderVal = RIterations.value
    		
    			tSmooth = TurboSmooth()
    			tSmooth.iterations = ViewPortVal
    			tSmooth.useRenderIterations = on
    			tSmooth.renderIterations = RenderVal
    		
    			modPanel.addModToSelection tSmooth ui:on
    		
    		)
    

    If your intention wasn't to have an instanced modifier. You can loop the selection.
    	on ButtonSmooth pressed do
    	(
    		for obj in selection do
    		(
    			tSmooth = TurboSmooth iterations:SIterations.value useRenderIterations:on renderIterations:RIterations.value
    			addModifier obj tSmooth
    		)
    		
    	)
    

    But if you really did want an instanced modifier, I would do it this way. It's shorter, uses less variables, and doesn't rely on the modifier panel.
    	on ButtonSmooth pressed do
    	(
    		tSmooth = TurboSmooth iterations:SIterations.value useRenderIterations:on renderIterations:RIterations.value
    		addModifier selection tSmooth
    	)
    
  • MattyWS
    Options
    Offline / Send Message
    MattyWS polycounter lvl 11
    Thanks for all of the replies. =) That last one hit the nailed it! was exactly what I was looking for, didn't think to use a variable like that.

    Thanks Again,
    Matty
  • monster
    Options
    Offline / Send Message
    monster polycounter
    I didn't see Ravenslayers response. I liked that he checked if the modifier existed before adding it.
  • leslievdb
    Options
    Offline / Send Message
    leslievdb polycounter lvl 15
    np

    monster: good examples! also a lot shorter ^^
Sign In or Register to comment.