- Maya Programming with Python Cookbook
- Adrian Herbez
- 444字
- 2021-07-14 10:46:41
Running code from the script editor
Not only is the Script Editor a great way to see which commands correspond to the actions you take in Maya's UI, but it is also a convenient way to write small bits of code. While you will certainly want to use a text editor to write your scripts, it is still important to be comfortable using the script editor to run small sections of code, either to test it out before inclusion in a larger script or to get more information about the current scene.
Getting ready
Make sure that you have the script editor open and that you've switched to the Python tab in the input (bottom) section.
How to do it...
Type the following into the input section:
import maya.cmds maya.cmds.polyCube()
Once you've done that, execute it by either pressing the Execute button at the top of the Script Editor or just by pressing Control + Enter.
Your code will disappear from the input section, a new polygon cube will be created, and the results will be pasted into the output ("History") section of the script editor.
To keep your code from disappearing automatically, highlight it first with Command-A (to select everything), then press Command + Enter. This will cause Maya to run just the selected code without clearing out the input section.
How it works...
Although the polyCube
command does the actual work, we have to first import the Maya library for Python before we can use it. To do this, we have to first use import maya.cmds
.
The script editor is a great way to try out small snippets of code, but the fact that successful code is deleted can get rather frustrating. For any real script development, you'll want to use a programmer-friendly text editor.
There's more...
One handy thing about the script editor is that you can save code from the editor to the shelf. To do this, enter some code into the input section, then go to File | Save Script to Shelf... from the Script Editor menu. Maya will ask you to provide a name for the script and then (after a bit of time), a new button will appear in the "Custom" shelf. Pressing that button will execute the corresponding code.
Although most of your scripting work will involve writing separate scripts, it can sometimes be useful to copy-paste commands from the history (top) section of the Script Editor to the input (bottom) section and save it all to the shelf. This is a bit like recording an action in Photoshop and can be a quick and dirty way to create a new shortcut for commonly used functionality.
- INSTANT Mock Testing with PowerMock
- Python數(shù)據(jù)分析基礎(chǔ)
- Python Deep Learning
- Programming ArcGIS 10.1 with Python Cookbook
- 數(shù)據(jù)結(jié)構(gòu)習題精解(C語言實現(xiàn)+微課視頻)
- 區(qū)塊鏈:以太坊DApp開發(fā)實戰(zhàn)
- 鋒利的SQL(第2版)
- Oracle Database 12c Security Cookbook
- 快速念咒:MySQL入門指南與進階實戰(zhàn)
- Teaching with Google Classroom
- Learning Unreal Engine Android Game Development
- 從Power BI到Analysis Services:企業(yè)級數(shù)據(jù)分析實戰(zhàn)
- Python趣味編程與精彩實例
- Java程序設(shè)計基礎(chǔ)(第6版)
- Simulation for Data Science with R