Home Technical Talk

Maya: Creating a crl-obj that selects other objects

polycounter lvl 17
Offline / Send Message
ivars polycounter lvl 17
Hey.
Does anyone know how to set up a control object, that when selected, selects other objects in my scene?
I'm thinking script job, but I've never really used them before, and I can't seem to find the correct condition for it to work.....
Any ideas?

I first tried to just put all the selection handles in one place, but even if I selected all the handles it would only select the top node in the hierarchy. So that didn't work....

Replies

  • MoP
    Offline / Send Message
    MoP polycounter lvl 18
    If you wanted to go the ScriptJob way, then you'd have to have a scriptJob looking for a specifically-named node, and have the job call on the event "SelectionChanged". That way it will constantly be called, but it will be fairly low overhead if you set the very first thing to bail out of the scriptJob if the size of the selection is not exactly 1 and matches the name of the "master node".

    If that condition is passed, then you can just call all the script required to select the objects needed.

    This seems pretty inflexible to me though, you'd have to keep editing the script called by the scriptJob if you wanted to add or remove items from the select group (or if you needed to change the name of the "master node").

    That's the first method that springs to my mind anyway.

    It might be possible to use some sort of scriptNode connected to the "master select node"? Not sure what the evaluate options are for those, though, you might not be able to have them activate on select.
  • ivars
    Offline / Send Message
    ivars polycounter lvl 17
    I see. Thanks. Seems like a hassle :P
  • Whargoul
    Offline / Send Message
    Whargoul polycounter lvl 18
    The script job could look at an attribute of the model you select to determine what it needs to select. You'd add a custom string attribute to your "selector" model, and encode the names of all the object you want it to select instead. The script job queries the selected object to see if it has that attrib, if so, gets the values and selects those objects instead. That way the script job is simple, quick and never needs to change. You can make another simple one to link objects (just edit's the string attr), have multiple selector objects in the scene, etc, still with one simple script job.
  • ivars
    Offline / Send Message
    ivars polycounter lvl 17
    That's an awesome idea. I'll try that :D

    How do you manage script jobs anyways, are they loaded with the scene? Or do you have to start them manually every time?
  • Flynny
    Offline / Send Message
    Flynny polycounter lvl 9
    What about a gui sort of thing? (vague i know)
  • Illusions
    Offline / Send Message
    Illusions polycounter lvl 18
    ivars wrote: »
    Hey.
    Does anyone know how to set up a control object, that when selected, selects other objects in my scene?

    What do you wish to accomplish with this? Because there might be other ways of accomplishing the same end product.
  • MoP
    Offline / Send Message
    MoP polycounter lvl 18
    Seems like a Shelf button would be the best way ;)

    Whargoul's idea is a solid improvement though, a more generic scriptJob that queries a specific custom attribute is much more flexible than a hard-coded one, and allows multiple of these "master control" objects to be used without writing more than one script.
    You could even do something like instead of having a string attribute, just have a compound string array attribute that you can hook up any node's "message" output plug into, that way it doesn't matter if the node name changes, it will still be connected. The scriptJob would simply select all nodes with their .message attribute plugged into your custom attribute.
    That might be more flexible than manually filling in a string with object names.

    If you want it to be something that will work on anyone's install of Maya then you'd probably need to set up a scriptNode on your scene that gets run on "scene opened" which launches the scriptjob to watch for the attribute on selection change, and another script node that kills this scriptJob when the scene is closed or a new scene is opened.
    That way nobody needs to install custom scripts to make it work, it'd all be self-contained in the scene.
  • Illusions
    Offline / Send Message
    Illusions polycounter lvl 18
    MoP wrote: »
    Seems like a Shelf button would be the best way ;)

    Or use Quick Select Sets...
  • MoP
    Offline / Send Message
    MoP polycounter lvl 18
    That'd be the obvious method too ;)
  • Frump
    Offline / Send Message
    Frump polycounter lvl 12
    I just wrote out some stuff about using selection handles then reread your first post and saw that you tried that already. It was the first thing that came to my mind too.

    Quick select sets is probably easiest, and what I have used in the past, but you don't end up with an object to select.
  • ioster
    I'm not sure if this will work or not, but I wrote a quick script.

    You select what you want to be added to the selection group, then run the script. To reselect, make sure you don't have anything else selected and run the script. If you want to make a new selection group, select anything and run the script. It will toss out your first group and you can move forward with your new selection group.

    Let me know if this is what you had in mind.

    string $selectionGroup[];
    string $currentSelection[];
    $currentSelection = `ls -sl`;

    //see if there's anything already in selectionGroup
    int $sizeSelectionGroup = size($selectionGroup);

    //see it there's anything selected
    int $sizeCurrentSelection = size($currentSelection);

    //if it's empty but nothing is selected prompt user to select items for the group
    if (($sizeSelectionGroup == 0) && ($sizeCurrentSelection == 0))
    {
    warning "Please select objects to add to selection group";
    }

    //if it's empty, add the selected items
    if (($sizeSelectionGroup == 0) && ($sizeCurrentSelection != 0))
    {
    $selectionGroup = `ls -sl`;
    }

    //if it has stuff in it, but nothing is selected, select the items in it
    else if (($sizeSelectionGroup != 0) && ($sizeCurrentSelection == 0))
    {
    select $selectionGroup;
    }

    //if it has stuff in it, but something else is selected, clear it an add the new stuff
    else if (($sizeSelectionGroup != 0) && ($sizeCurrentSelection != 0))
    {
    $selectionGroup = `ls -sl`;
    };
  • MoP
    Offline / Send Message
    MoP polycounter lvl 18
    ioster: nice of you to help, but isn't there default Maya function for this already, as Illusions said? Quick Select Sets?
  • ioster
    I don't know about quick select sets. It's sounds pretty likely they would include this function, but I've been out of the Maya game for a little while. It seemed like what he was describing was pretty easily accomplished with a script. Mostly I was just in the mood for a little MEL ;)
  • ivars
    Offline / Send Message
    ivars polycounter lvl 17
    Hey, thanks guys :)
    I'm working on a script now. I've got a solid plan I think, but I've been proven wrong before :P
  • ivars
    Offline / Send Message
    ivars polycounter lvl 17
    Hey all you scripting gurus.

    How do I create a scriptnode that contains a larger amount of code?
    I need to create a scriptnode (with MEL) that contains my global procedure, but the syntax gets broken if I try to add it all in to the scriptnode command....

    Or is there another solution? I want my scriptnode (that runs on scene open) to create the global procedure used by my scriptjob.....

    Did anyone understand a word of that :P
  • ivars
    Offline / Send Message
    ivars polycounter lvl 17
    nvm. Figured it out :P
  • ivars
    Offline / Send Message
    ivars polycounter lvl 17
    Ok. Problem no. 2

    If use this command:
    select pSphere1 pSphere2;
    
    It will select both objects (pSphere1 and pSphere2)

    If I store it as a value in a string and try to use it with the select command:
    string   $ objects = "pSphere1 pSphere2" ;
    select $ objects;
    

    I will get this: // Error: line 1: No object matches name: pSphere1 pSphere2 //

    How do I solve this the best way? Any ideas?

    EDIT: The code tag wont allow me to type "$objects", had to add a blankspace. Ofcourse I would not use that in my script.
  • Illusions
    Offline / Send Message
    Illusions polycounter lvl 18
    I think you would handle this by storing them in an array, and selecting them from an array. I don't know off the top of my head how you would code this, but I hope this can lead you in the right direction, and offer some prompt help.

    I think this may help:

    http://accad.osu.edu/~aprice/courses/694/variables.html#array
  • ivars
    Offline / Send Message
    ivars polycounter lvl 17
    Thanks.

    Yeah I guess I'll have to do something like that... This seems to work:

    string $objects = "pSphere1 pSphere2";
    string $selArray[];

    $selArray = stringToStringArray($objects, " ");

    select -cl;

    for ($i=0; $i < size($selArray); $i++)
    {
    select -add $selArray[$i];
    };


    Anyone knows how to check if a specific scriptnode exists? (using MEL)
  • ivars
    Offline / Send Message
    ivars polycounter lvl 17
    Realized I could also do it like this:

    string $objects = "pSphere1 pSphere2";
    string $select = "select " + $objects;
    eval $select;

    Seems a bit easier :)
  • ivars
    Offline / Send Message
    ivars polycounter lvl 17
    It's finally working :D

    You can get the script here: http://www.igorbazooka.com/createSelector.mel

    This is how you use it:
    - First select the control-object.
    - Then select any number of objects that you want it to select.
    - Run the script.
    - When the control-object is selected, it will select the other objects instead.
    - NOTE! You have to deselect everything before selecting the control-object.
    (Apparently "SomethingSelected" will not evalute when the selection changes, only when you go from nothing selected to something selected..... Didn't find any solution to this.)
    - If the scene is saved the script control-objects will work when the scene is loaded again.

    Thanks for the help guys!
    Maybe someone will find this little script usefull :)
  • Illusions
    Offline / Send Message
    Illusions polycounter lvl 18
    I have to ask though, what was your reason for choosing this over Quick Select Sets? Just curious really...
  • ivars
    Offline / Send Message
    ivars polycounter lvl 17
    Just thought it would be much faster and more intuitive to have a ctrl-obejct than having to go through menus to select it from a list.
    But mostly I used it as an excuse to practice on my scripting :P
  • throttlekitty
    Offline / Send Message
    throttlekitty ngon master
    Slight bump with an easy scriptless method; add a group/node as a child of the object you want as control, and the other objects as children of the node. Has the downside of transform inherits, in case that's an issue.

    Arbitrary Mesh
    -Group
    --Obj1
    --Obj2
    --Obj3
  • claydough
Sign In or Register to comment.