Home Technical Talk

[Maxscript] Simple TryCatch script error - Help

polycounter
Offline / Send Message
Justo polycounter
I was trying to make a script to activate the Paint Select mode while turning on ignoreBackfacing so that I don't select faces from the other side when painting.

To make this compatible with both EditablePoly and EditPoly, my thought process was to make a tryCatch function so that if it failed to the first clause because it's an EditPoly, it'd do the catch clause containing the code for that modifier. 

(
	try(	
		$.ignoreBackfacing = on
		actionMan.executeAction 0 "59236"  -- Selection: Paint Selection Region
		
	)
	catch(
		$.modifiers[#Edit_Poly].ignoreBackfacing = on
		actionMan.executeAction 0 "59236"  -- Selection: Paint Selection Region
	)
)

It works fine for the Editable Poly, however with the Edit Poly the ignoreBackfacing command seems to be ignored. Why is that? Any comments on how to improve this are welcome; I always feel like a caveman when it comes to Maxscript. 

Replies

  • kio
    Offline / Send Message
    kio polycounter lvl 15
    im not sure if youre aware of the classof command - its really usefull for stuff like this:
    https://davewortley.wordpress.com/2012/06/21/classes-superclassof-and-classof/

    sorry no idea whats up with the problem itself..

  • Mark Dygert
    Yea you would want to check the object, find out what class it is and then fire off the correct command.

    Also try to avoid "actionMan", that is usually garbage commands that the command recorder spits out.  

    It's worth looking up the actual MaxScript commands which are much more robust and have a lot more hooks for what you're working with and will do what want instead of doing exactly the one thing you did, once upon a time.
  • Justo
    Offline / Send Message
    Justo polycounter
    Ooooh thanks for linking that Kio, lots of useful lessons there for a beginner like me.

    I made it work! Thank you. I didn't know I could use the if and else like other languages just like that either. 

    (
    	max modify mode
    	if classof (modPanel.getCurrentObject()) == Editable_Poly then (
    		$.ignoreBackfacing = on
    		actionMan.executeAction 0 "59236"  -- Selection: Paint Selection Region
    	)
    	else (
    		if classof (modPanel.getCurrentObject()) == Edit_Poly then (
    			$.modifiers[#Edit_Poly].ignoreBackfacing = on
    			actionMan.executeAction 0 "59236"  -- Selection: Paint Selection Region
    		)
    		else (messagebox "The currently selected object does not have an Editable Poly nor has an Edit Poly on top." Title:"Modifier Stack Error")
    	)
    )
    

    Mark, how can I look up its "actual MaxScript command" fast and easy? I went to the MaxScript help docs, typed "paint selection region" and it spat out multiple articles which would take me a good while skimming through to see if the command is even in there. What's your trick ooo Dygert God of Scripting?
  • Pac_187
    Offline / Send Message
    Pac_187 polycounter lvl 11
    Not every action in max can be replicated via maxscript commands.
    In this case there's no value you can set to tell max to switch to paint select region directly.
    The only command which allows you to cycle through the different selection options is "max cycle select", but there's no function/value exposed to maxscript which tells you what the current selection mode is.

    So, actionMan is the best you can do in this case.

  • monster
    Offline / Send Message
    monster polycounter
    You can make the script a little shorter by using SetProperty. I like using SetProperty because it's a mapped function, so you can set a property on an array all at once. (Not used in this case.)

    (
    	max modify mode
    	obj = modPanel.getCurrentObject()
    	bf = #ignoreBackfacing
    	if isProperty obj bf do setProperty obj bf true
    	actionMan.executeAction 0 "59236"  -- Selection: Paint Selection Region
    )

  • Justo
    Offline / Send Message
    Justo polycounter
    @monster  Hey Juan, thank you for answering, I always learn something new from your code. Interesting solution, and thanks to that I think I'm learning about properties and their uses. I also found here Max Listener tells me every Property I want to know if I type in stuff like 'showclass "Edit_Poly.*" '

    Still, am I right in saying that your solution is only working because the property in Edit_Poly and Editable_Poly are named the same by coincidence? If I wanted to work with the SelectByVertex property for example, I wouldn't be able to use just that for both, since  Editable Poly's function is called "SelByVertex"......right?

  • monster
    Offline / Send Message
    monster polycounter
    @Justo You are correct, but in this case #selectByVertex will work for both. I think back when the modeling ribbon was created they added alias for many of the properties to match.
  • Justo
    Offline / Send Message
    Justo polycounter
    @monster So even though the Max Listener is displaying different names for those properties, some of them have hidden aliases that'll match with their Edit/EditablePoly counterparts? Cool, thank you Juan :) 
Sign In or Register to comment.