Thursday, March 4, 2010

Efficient Scripting In MEL - Part I

TL;DR - The less lines in your code the better, and use argument passing tricks whenever possible. Keep it clean!

After talking to a coworker today about how it'd be cool to try and rewrite a script he made in only ten lines of code...I thought it'd be good to to write a short post about how to increase efficiency in your MEL scripting. By efficiency, I mean saving yourself time both in the creation and the editing of your code by condensing your scripts into shorter blocks and not using more lines than you need to. I've found that it helps me a lot in the long term when my scripts are much shorter and easier to edit. On top of that, it's just way more awesome to write a script in ten lines that does the exact same thing as one that does it in fifty.

We'll start with some simple examples:

Let's start with a FOR Loop. Normally you'd setup a FOR loop like this:

for($i = 0; $i < 10; $i++) {
print $i ;
}

Or like this..


for($item in $myArray){
print $item ;
}

But why not condense it? Let's put that three lines of code into one.


for($item in $myArray) print $item

It doesn't seem like much of a gain, but in actuality you've cut the size by 66%.

Now let's add an IF statement to this line..


for($item in $myArray) if($item == "myItem") print $item

Still one line of code but you've got another check in there.

Another example, check if an object exists, error out if not.

int $exists = `objExists $myObject` ;
if(!$exists) {
error "Object doesn't exist." ;
}

// Four lines? Bleh. Let's bring it down a few..

if(!`objExists $myObject`) error "Object doesn't exist." ;

Some more error check examples:

if(!`filetest -f $pathToFile`) error "File does not exist." ;
//
if(!`endsWith $mayaFile ".mb"` && !`endsWith $mayaFile ".ma"`) error "File is not a Maya file." ;
//

This helps a lot when you've got a procedure that has a lot of error checking. Don't allow your checks to steal all of your screen real estate. Condense them into one line and celebrate your awesomeness with a frosty pint.

What about when you need to get some data from a textField or any other control? Assuming it returns a string, and not a string array. You condense the following:

string $myText = `textField -query -text myTextField` ;
print $myText ;

// Pffffff.

print `textField -query -text myTextField` ;

// Much better!

So this is a short, basic look into how to do this sort of stuff. I find that although making your code cleaner and nicer is probably in the top ten nerdiest things possible, you'll appreciate it someday when you look step back and look at your script and actually smile at how cool it looks. It is after all an art form. :)

1 comment:

  1. đồng tâm
    game mu
    cho thuê nhà trọ
    cho thuê phòng trọ
    nhac san cuc manh
    số điện thoại tư vấn pháp luật miễn phí
    văn phòng luật
    tổng đài tư vấn pháp luật
    dịch vụ thành lập công ty
    http://we-cooking.com/
    chém giólà bao nhiêu . Theo sau báo nhân là hổ nhân và viên nhân , còn đám bỉ mông cự thú không cử động , chỉ ở bên sườn núi đối diện quan sát tình hình .
    Tô Lạp quay sang Diệp Âm Trúc hỏi : “ Chúng ta nên làm gì bây giờ ? “ .
    “ Vừa rồi ngươi nói là có lửa , phải chăng từ Ngân Tệ ? “ .
    Tô Lạp gật đầu xác nhận : “ Ngân Tệ mặc dù còn rất nhỏ nhưng nó ít nhất có thể phát ra xích cấp hỏa hệ ma pháp , mặc dù không thể tấn công , nhưng dùng để thiêu đốt đồ vật thì dư sức . Bây giờ đang là mùa khô , thân cây cũng dễ đốt hơn “ .
    Diệp Âm Trúc quyết định thật nhanh : “ Tốt lắm , chúng ta hãy dùng hỏa công thử xem sao . Bất luận có thể thành công hay không , chúng ta cũng trở về Khoa ni á thành “ .
    Tô Lạp cho Ngân Tệ rời khỏi vai mình , xoa xoa đầu nó , hỏi : “ Ngươi đã chuẩn bị xong chưaân Tệ giơ cao đầu lên , thể hiện bộ dáng cao ngạo , ma pháp nguyên tố bắt đầu ba động xung quanh thân thể nó . toàn thân tỏa ra quang mang chói mắt . Là một ma pháp long , cho dù còn nhỏ nhưng ma pháp của nó tuyệt đối toàn diện , hỏa hệ tự nhiên cũng có trong số đó .
    Diệp Âm Trúc nhìn xuống phía dưới thấy đám báo nhân đã bắt đầu đến giữa sường núi , quay qua Tô Lạp gật đầu ra hiệu : “ Để cho Ngân Tệ chuẩn bị đi , chỉ cần bọn chúng lên cao hơn một chút là chúng ta bắt

    ReplyDelete

Scripting Topics

MEL (41) Maya (39) Scripting (32) Scripts (21) programming (14) Free Mel Scripts (8) MaxScript (7) Coding (6) Rigging (5) tutorial (5) 3ds Max (4) Python (4) Tricks (4) faceware (4) image metrics (4) Learn (3) Namespace (3) Namespacing (3) animation (3) facial (3) webinar (3) Code (2) GDC (2) Game Developers Conference (2) Multiple Namespaces (2) Print Selected Objects (2) Recursive (2) Removing Namespace (2) Return (2) Set Driven Keys (2) TOkenize (2) Tips (2) Toggle Background Color with MEL (2) animation tools (2) animators resource (2) deformers (2) learning (2) maya tools (2) mesh (2) modeling (2) nodes (2) procedure (2) script swell (2) transforms (2) Animschool (1) Attribute (1) Background Color (1) Beer (1) Blur (1) Character Setup (1) Check if an object exists (1) Class (1) Command Line (1) Constraints (1) Create SDK (1) Create a directory with mel (1) Data (1) Export (1) FilterString (1) Fix (1) Floating Slider Time (1) Functions (1) Get Maya Version MEL (1) Get Parent (1) Google (1) Holiday (1) How To Write To A Text File (1) Import (1) Incremental Save (1) Index (1) Joint Chain (1) Make Set Driven Keys (1) Maya Version (1) Modules (1) Objects (1) Orient Constraint (1) PYMEL (1) Parent (1) Parent Constraint (1) Point Constraint (1) Position (1) Print (1) Print Current Selection (1) Print Random Quotes (1) Print Selection (1) Print Vertices (1) Progress Bar (1) Progress Window (1) PyQT (1) Removing Spaces From Names (1) Scene File Name (1) Select Connections (1) Select Outgoing Nodes (1) Split Bones (1) Split Joints (1) St. Patrick's Day (1) String Array (1) System (1) Transfer UVs (1) Viewport (1) White Space (1) Windows Username (1) Zero Out Attributes (1) animButtonState (1) arrays (1) articles (1) auto key (1) better (1) blendshapes (1) break (1) confirm dialog (1) continue (1) convention (1) e3 (1) efficiency (1) error (1) eval (1) executable (1) fclose (1) fopen (1) fprint (1) games (1) improving (1) infinite loop (1) joints (1) listHistory (1) listRelatives (1) logic (1) loops (1) milestone (1) nodeType (1) objExists (1) recursion (1) rotates (1) rotations (1) schools (1) sculpting (1) setAttr (1) shout outs (1) source (1) source a script with a variable (1) speed (1) tech-artists.org (1) translates (1) video (1) warning (1) world matrix (1) worldMatrix (1)
 
Script Swell - Blogged