Home Technical Talk

Code Snippet Holder for Maya

CreativeSheep
polycounter lvl 8
Offline / Send Message
CreativeSheep polycounter lvl 8
I'm looking for a code snippet for Maya to hold random Maya MEL / Python code snippets without creating an icon on a shelf !

Replies

  • JoakimMellergard
    [code]
    
    [/code]
    I'm looking for a code snippet for Maya to hold random Maya MEL / Python code snippets without creating an icon on a shelf !

    Im not sure what you are looking for but you could put it in a menu or just have it as tabs in the script editor. Would any of that work?

    Here's how you make a menu for scripts:
    import maya.cmds as cmds
    import uuid, os
    def ScriptsMenu():
        if cmds.menu('scriptsMenu', ex=1):
            cmds.deleteUI('scriptsMenu')
        cmds.menu('scriptsMenu', p='MayaWindow', to=1, l='Scripts')
        cmds.menuItem(p='scriptsMenu', l="My Script", command="import MyScript; reload(MyScript)")
    ScriptsMenu()
    

    And this is how the scripts should start and end to run:
    def MyScript():
        #Code
    MyScript()
    
    And your userSetup file:
    import sys
    import maya.utils
    import maya.cmds as cmds
    
    
    sys.path.append('C:/Users/User/Scripts') #Example path to where you store your scripts
    
    def menuImport():
        import ScriptsMenu
    
    
    maya.utils.executeDeferred('menuImport()')
    

    There might be some unnecessary stuff in there and I hope I didnt forget anything. I more or less copied my old setup which I havent used for a year so I dont remember everything in there but I hope it works :)
  • CreativeSheep
    Options
    Offline / Send Message
    CreativeSheep polycounter lvl 8
    Joakim - This is excellent :) I have two questions.

    I put the userSetup code into my userSetup.mel file, correctly me if I'm wrong, shouldn't this load the script menu automatically when Maya starts, or must I always run this command first ?
    def MyScript(): #Code MyScript()
    When I run the command listed above, the script menu that worked with the first code snippet you posted, doesn't appear in the Maya menu bar ?

    Must I save the code snippets as Mel / Python scripts so the menu knows how to run these commands, and it is possible to add short comments to the code snippets ? :)
  • JoakimMellergard
    Oh sorry! All this is Python! Save the user setup stuff in your userSetup.py - if you dont have one create on. I dont write mel so im not sure what would happen if you run mel scripts through the menu but it might be alright seeing its only calling a file's main function. In the menu script you can decide to have whatever name(or label) you want. For example:

    cmds.menuItem(p='scriptsMenu', l="My TOTALLY AWESOME Script YEAY", command="import MyScript; reload(MyScript)")

    Also, you should have the menu file in the same directory as the other scripts and name all scripts including the menu script the same as the main function name of the script but without (). The
    And to answer your questions,
    Yes - the menu should load when maya starts. You should not need to run your scripts main function - the script should run when clicking the menu item in the menu.
    You add comments in scripts by writing # before your comment OR by writing ''' my comments '''. Or did you mean some explaining text while hovering over the menu item? Not sure about that - would have to look it up.

    One cool thing with this menu setup is that you can change your scripts on the fly since it reloads the script file whenever you run the menu item. However, you cant add scripts in the menu on the fly. Youll have to restart maya if adding a new script to the menu.

    I hope it will work now :)
  • JoakimMellergard
    Just to make sure I wasnt lying to you I just applied the setup and it works :)
  • JoakimMellergard
    Also, if the menu gets full or you need to organize your scripts better you can use separators like this:

    cmds.menuItem( divider=True )

    or sub menus like this (for example uv tools):

    cmds.menuItem( subMenu=True, label='UV Tools', to=True)
    cmds.menuItem(l="MY FIRST UV TOOL", command="import TOOL1; reload(TOOL1)")
    cmds.menuItem(l="MY Second UV TOOL", command="import TOOL2; reload(TOOL2)")
    cmds.setParent( '..', menu=True )
  • CreativeSheep
    Options
    Offline / Send Message
    CreativeSheep polycounter lvl 8
    Oh sorry! All this is Python! Save the user setup stuff in your userSetup.py - if you dont have one create on. I dont write mel so im not sure what would happen if you run mel scripts through the menu but it might be alright seeing its only calling a file's main function. In the menu script you can decide to have whatever name(or label) you want. For example:

    cmds.menuItem(p='scriptsMenu', l="My TOTALLY AWESOME Script YEAY", command="import MyScript; reload(MyScript)")

    Also, you should have the menu file in the same directory as the other scripts and name all scripts including the menu script the same as the main function name of the script but without ()

    The userSetup.py script is in the same folder as my other python scripts, when I start Maya the script menu is not there ?
  • JoakimMellergard
    UserSetup.py is the only one that shouldn't by default be in the same folder as the scripts. By default yuou should have userSetup.py here:
    C:\Users\User\Documents\maya\2015-x64\scripts
    It is possible to change that in maya. Also, Its possible to to have all scripts in the same folder as the usersetup file if you write that in the usersetup file.
  • CreativeSheep
    Options
    Offline / Send Message
    CreativeSheep polycounter lvl 8
    UserSetup.py is the only one that shouldn't by default be in the same folder as the scripts. By default yuou should have userSetup.py here:
    C:\Users\User\Documents\maya\2015-x64\scripts
    It is possible to change that in maya. Also, Its possible to to have all scripts in the same folder as the usersetup file if you write that in the usersetup file.

    Hi, I have changed the default path where script are to go in Maya, as you mentioned. And so the userSetup.py file is in the new path, when I start Maya there is no script menu in the Maya menu ?
  • JoakimMellergard
    ok... Try writing:

    print "TEST"

    on the first row in the usersetup file. Just to make sure it is being loaded at all. When you then start maya youll see TEST printed in both the output window and the scriptedtior. You can also in there see if maya outputed any error from the usersetup file.
  • JoakimMellergard
    could be small errors inside your menu script like indentation errors and that would make the usersetup file not load the script. However, these sort of errors should get outputted in the script editor once maya starts so its pretty easy to track them down.
  • JoakimMellergard
    If you want to you can post your usersetup and menu script here and ill debug it. Will probably go faster than this ^^. In that case, only use one of your scripts inside the menu and send that one too.
  • CreativeSheep
    Options
    Offline / Send Message
    CreativeSheep polycounter lvl 8
    If you want to you can post your usersetup and menu script here and ill debug it. Will probably go faster than this ^^. In that case, only use one of your scripts inside the menu and send that one too.
    print "test"
    import sys
    import maya.utils
    import maya.cmds as cmds
    
    
    sys.path.append("<file path to scripts folder>") #Example path to where you store your scripts
    
    def menuImport():
        import ScriptsMenu
    
    
    maya.utils.executeDeferred('menuImport()')
    

    The script is in my custom scripts folder, I have a few scripts in this folder they work successfully :)
  • JoakimMellergard
    So, it works now? Otherwise im going to need at least your menu script aswell.
  • CreativeSheep
    Options
    Offline / Send Message
    CreativeSheep polycounter lvl 8
    So, it works now? Otherwise im going to need at least your menu script aswell.

    The main script userSetup.py should call the menu script scriptMenu.py, correct ? If so I have a file called userSetup.py & a file called scriptMenu.py.

    The following code;

    def MyScript(): #Code MyScript()
    What is the purpose of this code ?
  • JoakimMellergard
    The main script userSetup.py should call the menu script scriptMenu.py, correct ? If so I have a file called userSetup.py & a file called scriptMenu.py.

    The following code;

    def MyScript(): #Code MyScript()
    What is the purpose of this code ?


    Ok so,
    1. userSetup.py loads the scriptfolder AND runs your scriptMenu.py main function.
    - to test if this is actually working print TEST in your userSetup file. When starting Maya you open the script editor and if TEST is printed in the output field that means the userSetup file was loaded by Maya. While doing this you can also take a look if Maya outputted any errors when loaded the usersetup file. If so, please tell me the errors.

    2. each line
    (
    This line:

    cmds.menuItem(p='scriptsMenu', l="Unfold UV", command="import unfoldUV; reload(unfoldUV)")

    )

    in scriptMenu.py loads a scripts main function (if its in the same folder referenced in the usersetup file) - in my case its my script unfoldUV.

    It does NOT matter if theres any errors in the scripts except for scriptMenu.py AND userSetup.py. The menu should still load - its independent of all scripts except for scriptMenu.py AND userSetup.py.

    3. and about...
    def MyScript():
    #Code
    MyScript()
    ...scripts referenced in the menuScript.py needs to start and end this way in order to run.
    For example, my unfoldUV script looks like this:
    import maya.cmds as cmds
    def unfoldUV():
        cmds.unfold(i=5000, ss=0.001, gb=0, gmb=0.5, pub=0, ps=0, oa=2, us=False)
        cmds.unfold(i=5000, ss=0.001, gb=0, gmb=0.5, pub=0, ps=0, oa=1, us=False)
    unfoldUV()
    
  • CreativeSheep
    Options
    Offline / Send Message
    CreativeSheep polycounter lvl 8
    There is neither a menu or a error ? Both scriptMenu.py and userSetup.py are in my custom scripts folder, where all my other scripts are located.
  • JoakimMellergard
    And I suppose TEST isnt printed? that means userSetup.py wasnt loaded which probably means your setting in maya to change server didnt work. Try putting the usersetup file in the default maya location.

    Edit: haha not "server" - it should say "scripts directory". I was playing a game with some friends when writing that and obviously got a little distracted.
  • CreativeSheep
    Options
    Offline / Send Message
    CreativeSheep polycounter lvl 8
    Hi, I moved the two script files into \ProgramFiles\Autodesk\Maya2015\Scripts, loaded Maya, there is no word 'test' printed in the script editor neither is there a script menu item in the menu bar ?
  • JoakimMellergard
    \ProgramFiles\Autodesk\Maya2015\Scripts
    Isnt mayas default scripts path.
    C:\Users\User\Documents\maya\2015-x64\scripts
    is default path.
  • CreativeSheep
    Options
    Offline / Send Message
    CreativeSheep polycounter lvl 8
    That is what I meant to write :) I placed the script files in /User path of Maya, not program files. Either path the script wouldn't run. :(
  • JoakimMellergard
    Ok. I have no idea why your usersetup wont run.. do you have a usersetup.mel in the same folder or in the folder your supposedly changed to? In that case remove the mel file. Otherwise you can try creating a userSetup.mel and just print test in there, just to see if maya only fails to load python.
  • CreativeSheep
    Options
    Offline / Send Message
    CreativeSheep polycounter lvl 8
    Running this script in the script editor gives me EOL Line 1 Scanning String Literal !
    import sys import maya.utils import maya.cmds as cmds sys.path.append('C:/Users/User/Scripts') #Example path to where you store your scripts def menuImport(): import ScriptsMenu maya.utils.executeDeferred('menuImport()')
  • JoakimMellergard
    I dont think you should expect that to work just by executing in the script editor.
    Did you find out anything about why your usersetup wont load? Thats the first thing you need to solve. Everything else might work if you solve that.
  • CreativeSheep
    Options
    Offline / Send Message
    CreativeSheep polycounter lvl 8
    It is working, although it's reading the script in the wrong file path, which was the problem, I never would have thought to put it in the path that it's currently in, that I have to sort though. The scripts menu shows in the menu bar. :)
  • CreativeSheep
    Options
    Offline / Send Message
    CreativeSheep polycounter lvl 8
    The script doesn't work in my custom path for scripts; it only works in my custom system variable MAYA_APP_DIR ?
  • JoakimMellergard
    mhm alright.. so bring me "up to date".
    What is "custom system variable MAYA_APP_DIR"?
    Itd be great if you could show me your current setup. Add all information you can think of that can make me understand. Your directories, scripts etc.
    For example:
    "my usersetup file is in -this- location and contains this."
    "my scriptfolder is in -this- location"
    "when starting maya this way (full explanation) it works"
    "when starting maya this way (full explanation) it doesnt work"
    and so on
  • CreativeSheep
    Options
    Offline / Send Message
    CreativeSheep polycounter lvl 8
    The MAYA_APP_DIR is a system environment variable you can set that tells Maya that you want Maya look elsewhere for the Maya application directory. I have this custom environment variable set for for Maya. The only directory that Maya will read your script is the /Preferences/2015-x64/scripts of my custom path, if I move that to the /scripts directory set forth in my maya.env file your scripts are not read.
  • JoakimMellergard
    Ok. Does this conclude your userSetup file or is that one read? if not you should seek into solving that because thats probably why nothing else works and I dont know why your maya wont read it. Maybe something you changed when you tried changing the maya script folders?
    Anyway, if you cant get that to work - why dont you just have your usersetup file in /Preferences/2015-x64/scripts and link the usersetup to the place with the scripts. That should work I think.
  • CreativeSheep
    Options
    Offline / Send Message
    CreativeSheep polycounter lvl 8
    The userSetup file is read from the /Preferences/2015-x64/scripts folder, but not from my custom scripts folder set in my environment variable that is what I don't understand, I have another python script in my custom scripts folder that works flawlessly, but when I place the userSetup.py file in my custom scripts folder, Maya won't load the script, when I move it to the /Preferences/2015-x64/scripts folder, the script menu loads in Maya.

    Could there be something in the script that is failing Maya to read it anything outside of the /Preferences/...../.... folder, that is the only thing I can think of, otherwise the script works, my environment variables are setup correctly.
  • JoakimMellergard
    I think Maya expects it to be in the /2015-x64/scripts folder and I thought it was possible to change that inside maya but ive never done it. Anyway, what stops you from have the usersetup in that place and the actual scripts in your custom path?
  • CreativeSheep
    Options
    Offline / Send Message
    CreativeSheep polycounter lvl 8
    I prefer to have all scripts in my custom scripts path, that is the only hiccup with your script, which is Maya won't load the script from a custom scripts folder. :)
  • JoakimMellergard
    Sure I can see that but the userSetup file is not a "custom" script in that way. I mean, you will never really touch it again since all scripts will be called from the menu anyway. Still odd that it can't be changed, though. Maybe try making a new thread asking how to change location of the userSetup file.
  • CreativeSheep
    Options
    Offline / Send Message
    CreativeSheep polycounter lvl 8
Sign In or Register to comment.