Thursday, October 1, 2009

MEL: Check If An Object Or Attribute Exists

Ever needed to check if an object exists in your Maya scene? Or perhaps you need to check if a certain object has an attribute or not?

Use the objExists mel command.

First let's use it in a simple way to check for the existence of an object saved in $myObject
// objExists will return true or false (1 or 0), so you can use it multiple ways.
int $objTest = `objExists $myObject` ;

// If that object exists, it will return 1 (true), so then you could say something like:
if($objTest == 1) {
print "It Exists!" ;
}

// Another trick you can do is an IF statement will simply test a boolean (true or false) and fire if it's true, example:
if($objTest) {
print "It Exists!" ;
}

// So using that, we can simply test for the return of objExists in our IF statement, to simplify it even more.
if(`objExists $myObj`) {
print "It Exists!" ;
}

This works for attributes too, so to check for the existence of an attribute on a certain object, let's say "R_Arm_Ctrl.IKFK" we can use the following:

if(`objExists "R_Arm_Ctrl.IKFK"`) print "It Exists!" ;

Huzzah!

12 comments:

  1. beautiful, thanks! I had a bunch of objects (over 1000) and some had the custom attribute "RandomFloat" and some did not, no real way to select just the ones that had it. I dragged and selected all the objects and ran this:


    string $selections[] = `ls -sl`;
    for ($sel in $selections)
    {
    $myattribute = $sel + ".RandomFloat";

    if(`objExists $myattribute`) {

    setAttr ($sel + ".RandomFloat") (rand(0.8,1));
    }

    };

    Got around the errors about certain objects not having the attribute and thus not running the script at all :) Thanks!

    ReplyDelete
  2. This whole page is inspiring let alone this post! Is there a way of making a script that selects a different object instead, if it can't find the first object listed?
    Example if object A doesn't exist, select B?

    ReplyDelete
  3. Can you give me an example of what object you are looking for and what it might select if it didn't find it? I think I know what you mean but clarify a bit and we can adapt the script to do it.

    And thanks for reading!

    ReplyDelete
  4. Hiya! I am a total newbie. I am tryig to write a script that does various stuff based upon what is selected. But the first thing I need to do is determine if anything is selected. How, using MEL can I get MAYA to do that?

    conceptually I think it should go like this:

    1. Run script
    2. Script checks to see if anything is selecetd
    3. If nothing is selected script tells you nothing is selected, stops.
    4. Else, if something is selected the script reads adds the name of the selection to a string
    5. The string is parsed
    6. Based upon certain flags it does one thing or another...

    ReplyDelete
  5. Hey Aharon,

    To find if something is selected you'd use the "ls" command (list) with the "-sl" flag (selected). It will return an array even if only one thing is selected, so you'll need to keep that in mind.

    So it'd be:
    string $myArray[] = `ls -sl` ;

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Hi Aharon,

    Total newbie here as well but maybe you can try this:

    string $isSomethingSelected [] = `ls -sl`;
    if (size ($isSomethingSelected) == 0)
    {
    error "Nothing Selected";
    }

    essentially you are trying to find out if the array has anything in it, if it comes out as zero, then show the error.hope it works.

    ReplyDelete
  8. is there a way to check if an object is keyed?

    ReplyDelete
  9. Nếu bạn đang muốn đăng tin bán nhà hay bán đất hoặc bạn muốn ban nha quan 12 , nha dat xinh thì hãy đến với chúng tôi rao vat mien phi, với chất lương hàng đầu chúng tôi sẽ giúp các bạn , đăng tin và xem các khu vực nha dat go vap, nha dat quan 9, nha dat thu duc , nha dat binh tan , nha dat tan phu , nha dat tan binh và các khu vực khác trên NguyenManhKha toàn quốc với uy tín và hiệu quả cao khi bạn đến với chúng tôi.

    ReplyDelete
  10. LÀM BẰNG CHẢO (chiên)
    Nguyên liệu làm heo quay: hoc nau an o dau
    – 300g thịt ba chỉ
    – 1/2 gói ngũ vị hương bun rieu cua – 1 củ tỏi (bào nhuyễn)
    – 10ml rượu trắng
    – 10ml giấm
    – Đường, muối, hạt tiêu cach lam banh trang tron Cách làm heo quay:
    – Trộn đều hỗn hợp muối, đường, hạt tiêu, ngũ vị hương, tỏi bào, rượu trắng lại với nhau để làm thành gia vị tẩm ướp.
    – Xoa đều gia vị ướp lên bề mặt của miếng thịt (trừ phần da). banh bao chien – Sau đó, xoa đều muối hạt to lên phần da. vit nau chao – Ướp thịt ít nhất 2 tiếng. Chú ý để ngửa phần da lên trên và giữ cho khô ráo.
    – Sau khi đã ướp xong, để cho miếng thịt ráo nước. suon xao chua ngot – Dùng tăm hoặc kim xiên đều lên toàn vit om sau bộ bề mặt của miếng da. Sau đó, quét nhẹ giấm lên phần da cach lam banh bong lan này. Hay quét thật nhẹ tay thôi để thịt không bị chua nhé.
    – Cho một ít dầu ăn vào chảo, đặt phần da vào chảo chiên trước. Nếu miếng thịt quá to thì các bạn nên hấp thịt khoảng 15 phút trước khi tẩm ướp để thịt khi chiên xong không bị sống. cach lam pate gan – Khi thấy phần da đã giòn, vàng thì các bạn lật miếng thịt lại và chiên cho đến khi thịt chín đều.

    ReplyDelete
  11. thì hơi khó nghe một chút. Nhưng nó chính là sự thật.

    Đồng dạng, trong mắt tu sĩ Nguyên Anh kỳ thì Trúc Cơ kỳ và Kết Đan kỳ

    cũng hoàn toàn như thế. Tương tự, trong mắt lão quái Anh Biến kỳ, tu sĩ
    khoá học kế toán thuế
    trung tâm kế toán tại long biên
    trung tâm dạy kế toán
    http://meomeo007.com
    học kế toán tổng hợp tại huế

    http://kenyseo.com
    http://ngoduong89.com
    luyện thi toeic
    trung tâm kế toán tại nghệ an
    trung tâm kế toán tại cầu giấy
    trung tâm dạy kế toán tại cầu giấy
    học kế toán tại bình dương
    tiếng anh cho người mới bắt đầu
    học kế toán tại đà nẵng
    học kế toán tại đồng nai
    http://meomeo007.com
    học kế toán tại huế

    dưới Hóa Thần kỳ cũng chẳng bằng chó lợn, muốn giết là giết.

    Ánh mắt Vương Lâm lộ rõ một sự kiên định. Đến bây giờ, hắn đã thực sự

    cảm nhận được sự sinh tồn trong tu chân giới còn khốc liệt hơn nhân gian

    rất nhiều. Mạnh được yếu thua. Nếu muốn sinh tồn, nhất định phải có

    được thực lực mạnh mẽ. Mạnh mẽ đến mức mà tất cả mọi người đều sợ hãi

    thì mới không bị bắt nạt.

    - Nếu tu vi của mình mà đạt tới Hóa Thần kỳ hoặc là Anh Biến kỳ thì có

    giết chết Đằng Lệ, cho dù là Đằng Hóa Nguyên cũng không dám nói một câu, lại càng không dám đi tìm cha mẹ ta. Thực lực! Tất cả đều dựa vào thực

    lực. Chỉ có trở thành cường giả mới có thể quyết định sinh tử của người

    khác.

    Trong lòng Vương Lâm quặn đau, ánh mắt lộ ra hàn quang.

    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