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
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 ?
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:
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.
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:
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 ?
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.
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 ?
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.
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.
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.
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
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 ?
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.
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:
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.
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 ?
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.
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()')
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.
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.
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
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.
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.
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.
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?
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.
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.
Replies
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:
And this is how the scripts should start and end to run: And your userSetup file:
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
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 ?
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
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 )
The userSetup.py script is in the same folder as my other python scripts, when I start Maya the script menu is not there ?
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 ?
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.
The script is in my custom scripts folder, I have a few scripts in this folder they work successfully
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:
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.
Isnt mayas default scripts path.
C:\Users\User\Documents\maya\2015-x64\scripts
is default path.
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.
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
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.
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.