Home Technical Talk

[Maya Mel] Needing a hint on why this unsourced script crashes. (also free creasing script)

https://pastebin.com/V6aqyAy5

This little ditty is for assigning to a hotkey to toggle full/half/off for edge creasing. Seemed like a handy thing to have for quick one offs compared to a full-on crease set workflow or remembering to pre-convert a face selection for the crease tool.

It took me a bit to understand that I needed to source this little script, but I don't quite understand why it needs to be sourced. I was going up and down trying to figure out why it insta-crashed, thinking I was doing something noobishly wrong. Is there some rule of thumb or style I should follow for something relatively simple like this?


Replies

  • bitinn
    Options
    Offline / Send Message
    bitinn polycounter lvl 6
    I haven't written a MEL before but from autodesk doc it suggests in order for Maya to learn the global in MEL you need to source it. (It also explains why you need to re-source it manually when you change the file)

    Just a guess.

    EDIT: This doc explains it in more details.
  • Klaudio2U
    Options
    Offline / Send Message
    Klaudio2U polycounter lvl 8
    You don't have to source anything with such short script. Just copy/paste the entire script as it is as a hotkey and you are good to go. 
  • throttlekitty
    Options
    Offline / Send Message
    @bitinn I have plenty of .mel scripts with globals that don't need to be sourced, I can't really find the difference. Most the examples I was pulling up are simple linear scripts, or something larger and more complicated.

    @Klaudio2U I could do that too, I like to keep my hotkeys "clean" for portability or a quick recovery if my prefs ever die.

    Not really a huge deal in this case, I'm just trying to understand so I can avoid something like this in the future. Also noob scripting + a long few days into an insomnia swing are probably a bad mix to begin with :)
  • TTools
    Options
    Offline / Send Message
    TTools polycounter lvl 4
    @throttlekitty I believe the issue you are running into is due to: proc tkPerformCrease() being a local procedure as opposed to a global procedure. 

     On Maya startup and initialization, Maya will parse all scripts located in the environment variable script paths (located via default internal vars and supplemented in your Maya.env file if you ever wish to append it).  As it parses through those files, it is essentially storing the global proc names within your script files as callable procedures.  Local proc names don't get stored.  So, when you make a call to tkCreaseToggle(), it finds it because it's a global proc, but when you call to the local proc tkPerformCrease(), Maya has no idea what you are talking about.  That is why you must source the script.

    My recommendation is to take a more Pythonic approach, and make the vast majority of your procs global so they can be accessed by any other proc at anytime you might need it.  It's a more modular approach to programming.  You'll find python code often times will have a lot of defs (equivalent to global procs) which it leverages as bite sized operations that can be used interchangeably.

    I also agree with your assessment regarding copy/pasting direct code into a hotkey.  It's messy and doesn't leave you in good shape for portability or future improvement.  If you've written it as global procs, you can always reference that code later for other ventures, and you don't have to go digging for it in the shelf editor command UI.


Sign In or Register to comment.