PDA

View Full Version : How to call a function, when I select object.


johnyz
01-25-2009, 01:56 AM
I'm creating my own character rig and I want to add some fancy extra controls.
I want to create a kind of a switch. I need a control (curve object) that changes some things when I click it. Let's say I want to create a kind of button in my character control array (curves objects - sliders ext.) - that selects all the control objects and keys them.
I know how to write such a script, but I don't know how to make it happen after I click on a curve object - that will be my button.
If anybody knows how can I achieve this (call some mel procedures when selecting object) I would be very grateful.
have a nice day.

ewerybody
01-26-2009, 08:20 AM
you need a selectionChanged scriptJob:

scriptJob -e "SelectionChanged" "delete `ls -sl`";

If you have a UI you work with parent the job with it! So it will be killed if the UI gets closed. Otherwise you'd want to remember the scriptJobID to be able to kill it afterwards. You could also just browse all existing jobs to look for your command to prevent double execution of the job.

johnyz
01-27-2009, 01:39 AM
alright. That sounds nice... Could you explain how do I connect this script with my object? I'm not experienced in Mel scripting - forgive me. Let's say I have an object PCube1. And whenever I select it i want some functions to execute. How do I do it using your script?

ewerybody
01-27-2009, 09:52 AM
its not my script ;]
"scriptJob" is a Maya built in command. With it you can set up invoking commands on certain conditions or events occur. One of these events is "SelectionChanged".

Now whenever you select anything you could now look if the selection is "PCube1".

{
int $scriptJobID = `scriptJob -e "SelectionChanged" "johnyz_SelChangeCommand"`;
print ("Remember to kill scriptJob " + $scriptJobID + " if you don't need it anymore!\n");
}

global proc johnyz_SelChangeCommand()
{
string $selectionArray[] = `ls -selection`;
if ($selectionArray[0] == "PCube1")
{
print "Hey you actually selected \"PCube1\"\n";
}
}

look at the docs for scriptJob (http://download.autodesk.com/us/maya/2008help/Commands/scriptJob.html). Its quite interesting.
with -listJobs you can have a look at all running jobs. -kill well... kills a job with the given number.

johnyz
01-27-2009, 03:23 PM
Thank you so much everybody for such detailed explanation. This is what I needed my friend :D

CGTalk Moderation
01-27-2009, 03:23 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.