AutoSmooth on selected objects

Hello I'm beginner in MaxScript and I writed a little script to automatically apply an auto-smooth on only one selected object.

But I would like to make it working on a selection of several objects.

Maybe it's also possible to improve the syntax because I'm not sure is the best way to write this script.

Thanks for your help !

(
bob=selection
sel=bob as array
if sel.count !=1 then
(
messageBox "select only one object"
)

else
(
if classof $.modifiers[1] != Edit_Poly then
(
modPanel.addModToSelection (smooth autosmooth:true threshold: 10) ui:on
)

else
(
numFaces = polyop.getNumfaces $
subobjectLevel = 4
$.modifiers[#Edit_Poly].autoSmoothThreshold = 10
$.modifiers[#Edit_Poly].SetSelection #Face #{1..numFaces}
$.modifiers[#Edit_Poly].ButtonOp #Autosmooth
$.modifiers[#Edit_Poly].SetSelection #Face #{}
subobjectLevel = 0
)
)
)

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
henrard.stephane's picture

Thanks

Hello

Thanks for your replies !

Your tips and examples help me a lot to better understand how to script.

I will re-write my script based on your advices.

Cheers!

Anubis's picture

some tips from me

We cannot supply all the good tips just with few words, but here is and some from me.

modPanel methods required Modify panel to be active!

-- Sets Modify command mode active
max modify mode

Using Modify panel is slow, so when you have alternative just use it instead. For adding modifier you can use addModifier function (without need to switch to mod-panel).

There also an useful function validModifier that verify if you can apply concrete modifier class to your object.

if validModifier selection[1] smooth do
addModifier selection[1] smooth

And here is how I imagine your task...

fn autoSmooth tresh = 
(
	local ms
	local poAS = polyOp.setFaceSmoothGroup
	local moAS = meshOp.autosmooth
 
	for obj in selection where \
	--canConvertTo obj Editable_Poly or \
	validModifier obj smooth do
	(
		ms = obj.modifiers.smooth
		if ms != undefined then
		(
			ms.autosmooth = true
			ms.threshold = tresh
		)
		else case classOf obj of
		(
			Editable_Poly: (
				poAS o o.faces tresh add:false
			)
			Editable_Mesh: (
				moAS o o.faces tresh
				update o
			)
			Default: (
				addModifier obj \
				(smooth autosmooth:true threshold:tresh)
			)
		)
	)
)

Cheers!

my recent MAXScripts RSS (archive here)

barigazy's picture

Hi Anubis,I always try to

Hi Anubis,
I always try to learn from the best, but is difficult to explain some stuff using Google Translate :) like me.
My solution is not for learning example because is to short.
I try to explaine just some basic rules for optimizatons.

bga

Anubis's picture

:)

I think that Henrard ask for tips as he say: "also possible to improve the syntax".

Anyway, as I have a bad habit to trust to my memory and post untested code, I presume that I made a syntax typo in my example code. I used: obj.modifiers.smooth but the correct might was: obj.smooth, rigth?

my recent MAXScripts RSS (archive here)

barigazy's picture

:)

Yep. I post the code 75% never tested, some works and some not.
But it would be pointless if we just post the correct codes.
Then we would not have something to talk about.

:)

bga

Anubis's picture

indeed :)

Meanwhile, as I copy/paste some line of code from your example, now I see that you use "o" variable, while I use "obj" and need to edit my example code :) but EDIT option here available only for my last post :( so I attach edited code on this post.

AttachmentSize
fn_autosmooth.ms 601 bytes

my recent MAXScripts RSS (archive here)

barigazy's picture

If you have a time i'm

If you have a time i'm posting new thread about Resize Modifier (XForm+CustAttributes).I need some advice about xform transform and center (modifier) position

bga

barigazy's picture

one tip for you:

Look in the MaxScript help to find out more about meshOp and polyOp operations.
Theses are interface functions and try to store them in some variable before you put them inside for-loop. It is much fester.
Also try to switch from modify to create panel when use theses types
of operations using this line:

if getCommandPanelTaskMode() != #create do setCommandPanelTaskMode mode:#create

Using "undo off (...)" and "with redraw off (...)" you can speed-up the things a lot.
Aniway . . . you deside what you need

;)

bga

barigazy's picture

try this one

I write a simple function

fn autoSmooth tresh: = if selection.count == 0 then (messageBox "Select Some Objects!" title:"Warning" beep:off) else
(
	local poAS = polyop.setFaceSmoothGroup
	local moAS = meshop.autosmooth
	for o in selection where isKindOf o GeometryClass and not isKindOf o Targetobject do
	(
		if o.modifiers.count != 0 then addModifier o (smooth autosmooth:true threshold:tresh) else
		(
			case classof o of (
				(Editable_Poly): (poAS o o.faces tresh add:false)
				(Editable_Mesh): (moAS o o.faces tresh ; update o)
			)
		)
	)
)
autoSmooth tresh:45

bga

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.