Home Coding, Scripting, Shaders

Can anyone make this more elegant/professional

afb3d
polycounter lvl 3
Offline / Send Message
afb3d polycounter lvl 3

Here’s my script so far. Can anyone make this more elegant/professional for me please? I think this will help alot of people who use wacoms in max. Also will be happy to credit anyone who helps with it in the script.

All for the community folks.

(

global selectionChanged

callbacks.removescripts #selectionSetChanged id:#myCallbackID

callbacks.addscript #selectionSetChanged "selectionChanged()" id:#myCallbackID

fn selectionChanged =

(

if not (isCreatingObject()) and not keyboard.shiftPressed do

max select

)

)

Replies

  • afb3d
    Offline / Send Message
    afb3d polycounter lvl 3
  • afb3d
    Offline / Send Message
    afb3d polycounter lvl 3
  • ProperSquid
    Offline / Send Message
    ProperSquid polycounter lvl 12
    I'm not an expert with Maxscript, but what's happening here? What does this do to help Max users with Wacom tablets?
  • afb3d
    Offline / Send Message
    afb3d polycounter lvl 3
    When using a tablet it's so ridiculously easy to move an object by accident when trying to select it. This script makes sure you're always using the select object tool instead of move,rotate,scale. 

    It's the same as pressing Q. But automates it everytime you make a selection.
  • ProperSquid
    Offline / Send Message
    ProperSquid polycounter lvl 12
    That's pretty cool. From what I can see, the code looks pretty good (without knowing the language). If this was in Python, then I'd probably be able to give you better feedback.
  • afb3d
    Offline / Send Message
    afb3d polycounter lvl 3
    Thanks :) trust me it makes a huge difference when working in max now. I just wish someone who was an actual programmer could "finalize it" so it has no kinks and people can enjoy it. 
  • Revel
    Offline / Send Message
    Revel interpolator
    Interesting, yeah I had similar issue with the wacom selection, and usually I instinctively switch to mouse whenever I want to make a precise selection without accidentally bump the mesh. Thanks for sharing this!
  • poopipe
    Offline / Send Message
    poopipe grand marshal polycounter
    tech artist rather than programmer ... 

    the only potentially, mildly scary bit (without refreshing my memory about maxscript's dirty bits) is that selectionChanged is a global .
    In this context I can't see it being a problem (it's probably required tbh)

    I expect @monster or @miauu can clarify whether it's going to be an issue or not
  • afb3d
    Offline / Send Message
    afb3d polycounter lvl 3
    Possibly. Honestly I have no idea, I've mashed this code together by doing a lot of googling and checking the mxs reference. It all seems to work but I bet it could be better. especially when working in sub object mode.
  • miauu
    Offline / Send Message
    miauu polycounter lvl 13
    Is this is the whole script?
    @poopipe is right, you have to change the name of your global variables to be not so "obvious" and "common". At least use something like this:
    global selectionChanged_adj34wrff


  • poopipe
    Offline / Send Message
    poopipe grand marshal polycounter
    That's simply to reduce the risk of another script overwriting the variable so we're clear.

    As far as the rest of the script goes - if it works you're good. There's not really enough code there to provoke any bugs.

  • afb3d
    Offline / Send Message
    afb3d polycounter lvl 3
    Thank you @miauu and @poopipe appreciate the feedback and explanation :) will change the global name and see how it functions after. Hopefully that'll be all and anyone using a wacom can finally enjoy max 
  • afb3d
    Offline / Send Message
    afb3d polycounter lvl 3
    Right, im being more ambitious with this now....can anyone find any issues with this? trying to get it working now properly with sub object mode. Also can anyone explain why this means? "ev nd" i have no idea, i just followed the maxscript reference.

    Ideally test this out with a wacom :) but woks fine with a mouse too. 

    (
    Global selectionChanged_mod
    Global subObjChanged_mod
    callbacks.removescripts #selectionSetChanged id:#myCallbackID
    --print "script removed"
    callbacks.addscript #selectionSetChanged "selectionChanged_mod()" id:#myCallbackID
    --print "script added"
    fn selectionChanged_mod =
    (
    if not (isCreatingObject()) and not keyboard.shiftPressed do
    (
    max select
    print "selection changed"
    NodeEventCallback subobjectSelectionChanged: subObjChanged_mod
    print "fire subObjChanged_mod"
    )
    )
    fn subObjChanged_mod ev nd=
    (
    max select
    print "sub object changed"
    )

    )



  • afb3d
    Offline / Send Message
    afb3d polycounter lvl 3
    There's a bug i've found which ill try sort today, or if anyone else wants to have a crack at it :)
    If your in "attach" mode it bugs out and changes your selection type.
  • Tekoppar
    Offline / Send Message
    Tekoppar polycounter lvl 10
    (<br>&nbsp;&nbsp;&nbsp; global callbackItem<br>&nbsp;&nbsp;&nbsp; global lastSelectedObject<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; fn toggleToolMode arg = (<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; toolmode.commandmode = #SELECT<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; toolmode.commandmode = arg<br>&nbsp;&nbsp;&nbsp; )<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; --ev returns the event that fired the callback<br>&nbsp;&nbsp;&nbsp; --nd returns the node<br>&nbsp;&nbsp;&nbsp; fn CallbackFnWacomFixSelection ev nd = (<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; --stores the current selected object to compare with last object<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; currentSelection = $<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; --checks if current selection is an editable poly and if so gets the current<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; --selection type and with that gets the current selected and gets it's index<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if ((classOf currentSelection as string) == "Editable_Poly") do (<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; currentMeshSel = currentSelection.GetMeshSelLevel()<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if currentMeshSel != undefined and currentMeshSel != #Object do (<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; currentSelection = currentSelection.GetSelection(currentMeshSel) as array<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; currentSelection = currentSelection[1]<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; )<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; )<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if lastSelectedObject != currentSelection do (<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; --first the store the current toolmode in a variable<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; currentToolMode = toolmode.commandmode<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; --if the current toolmode is select already we don't want to fire any unessecary code&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; --only #SELECT, #MOVE, #ROTATE, #NUSCALE, #USCALE and #SQUASH is supported when setting the toolmode<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; --so for the rest we will simply ignore them<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; case currentToolMode of (<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #MOVE:toggleToolMode(currentToolMode)<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #ROTATE:toggleToolMode(currentToolMode)<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #NUSCALE:toggleToolMode(currentToolMode)<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #USCALE:toggleToolMode(currentToolMode)<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #SQUASH:toggleToolMode(currentToolMode)<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; )<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; )<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; lastSelectedObject = currentSelection<br>&nbsp;&nbsp;&nbsp; )<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; fn registerCallbackFnWacom = (<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; callbackItem = NodeEventCallback selectionChanged:CallbackFnWacomFixSelection subobjectSelectionChanged:CallbackFnWacomFixSelection<br>&nbsp;&nbsp;&nbsp; )<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; fn unregisterCallbackFnWacom = (<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; --set callback to undefined and call garbage collector so it's destroyed<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; --and it's memory is set free<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; callbackItem = undefined<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; gc light:true<br>&nbsp;&nbsp;&nbsp; )<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; rollout wacomSelectionFix "Wacom Fix"<br>&nbsp;&nbsp;&nbsp; (<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; checkbutton theButton "Enable script" checked:false tooltip:"Toggles the wacom selection fix"<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; on theButton changed state do (<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if state == on then (<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; registerCallbackFnWacom()<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; theButton.text = "Disable script"<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ) else (<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; unregisterCallbackFnWacom()<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; theButton.text = "Enable script"<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; )<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; )<br>&nbsp;&nbsp;&nbsp; )<br>&nbsp;&nbsp;&nbsp; createdialog wacomSelectionFix<br>)<br>

    I tried having a go at it, there is a "bug" where if you drag click with move, rotate or scale tool you can still apply the effect by letting go of the mouse button before you stop moving the cursor. Although I haven't been able to get it to apply the tool effect when testing with a wacom, but only with my mouse. I'm assuming it's because something is still waiting on the mouse move to finish and if the left mouse button finishes before mouse move that action overrides the NodeEventCallback.

  • yason01
    Offline / Send Message
    yason01 polycounter lvl 7
    Thank a lot, you are real hero!
  • yason01
    Offline / Send Message
    yason01 polycounter lvl 7
    I have problem( Script doesn't allow to create object (box or other primitive)
  • yason01
    Offline / Send Message
    yason01 polycounter lvl 7
    3d max 2017 and 3d max 2018 
  • afb3d
    Offline / Send Message
    afb3d polycounter lvl 3
  • yason01
    Offline / Send Message
    yason01 polycounter lvl 7
    Oh my god, it work! Thank you very much! You are great!
  • afb3d
    Offline / Send Message
    afb3d polycounter lvl 3
    You are welcome :) glad it's helping you. Please note though I've been looking into this for some time and I've concluded max just isn't great for wacom use. My script certainly helps but you need to make several workflow changes in order for it to be comfortable with a tablet 
  • yason01
    Offline / Send Message
    yason01 polycounter lvl 7
    I don't work with wacom, so this solution completely suits me)
  • afb3d
    Offline / Send Message
    afb3d polycounter lvl 3
    Thats great :) enjoy
Sign In or Register to comment.