PDA

View Full Version : trigger a script


noobmode
06-05-2012, 02:31 AM
We can trigger a script by using script editor, make a button on shelf and click on it, or using the
marking menu, bla bla. But can we do it by using a node in hyperShade? How?

Anything would help, thank

sciLoop
06-05-2012, 07:02 AM
We can trigger a script by using script editor, make a button on shelf and click on it, or using the
marking menu, bla bla. But can we do it by using a node in hyperShade? How?

Anything would help, thank


Have a look into expressions and script jobs for that.
The expression will be part of the DG. The script job will be event/condition driven.

noobmode
06-05-2012, 07:09 AM
Thank for you reply.

However, i know, i used to use script job to trigger script on event too, it's just i don't like it too much since it's hard to control and look like scriptJob save as reference, i will lost it when using a different computer (sorry if i mistake)

Expressions only get evaluated when the currentTime changes. Of course, that's not what i want.

That's true that i want the script being trigger when on some condition, but i want to make it simple so animator don't have to worry about it.

sciLoop
06-05-2012, 07:24 AM
since it's hard to control and look like scriptJob save as reference, i will lost it when using a different computer

You could put it into a script node.


Expressions only get evaluated when the currentTime changes.
No, its evaluated when there is just a normal trigger as for anything else DG related.

noobmode
06-05-2012, 07:32 AM
if(pSphere1.v == 0) {
print ("0" + "\n");
}
else
{
print ("1" + "\n");
}


Try this one, on frame zero, if you change the visibility of the pSphere, nothing will print out, but if you play the timeline, it will print "1111111111111111111...." or something like that.
Actually i quite don't get what you trying to tell me, so can you give me an example?

I did know about scriptnode, but since i don't like it (like i said in the last post), so pass too.

sciLoop
06-05-2012, 08:12 AM
try this:

in the script editor:
addAttr -ln "newVis" -at bool |pSphere1;

then in the expession editor:
pSphere1.newVis = pSphere1.translateX;
if(pSphere1.visibility == 0)
{
print ("0" + "\n");
}
else
{
print ("1" + "\n");
}



The expression node is now triggerered by the pSphere1 DAG. Move the pSphere1 in x, you will see how the expression node updates and prints.
In your above mentioned case, the expression node shows its ability to update itself on time change.

noobmode
06-05-2012, 08:52 AM
What's in the world...

pSphere1.newVis = pSphere1.translateX;


is nothing relate to

if(pSphere1.visibility == 0) {
print ("0" + "\n");
}
else
{
print ("1" + "\n");
}



How can it make different? Because of the translateX update so the status of pSphere1 also update along, and with that. It will print result for each time the pSphere being pull?

will the calculator made scene heavier?

sciLoop
06-05-2012, 09:05 AM
The thing is: you got to have a trigger when something should evaluate in the DG.
There are different trigger mechanisms in Maya.
The DG evaluates when something asks for a value.
Thats why I created the attribute newVis and told it to be the same as the translateX of pSphere1.
So, now, when you move pSphere1 in x, the expression is run because it asks for the translateX value so that it can set the newVis value.
That was just a example to show you the DG evaluation mechanism.

If you need another mechanism, I strongly encourage you to choose a script job. There is nothing strange or difficult about that and it works fine.

noobmode
06-05-2012, 09:41 AM
Thank sciLoop, it's very useful information for me. Grateful.

But you know, it's like a trick. I want the script being trigger when at the moment value change, not all the time. It will lead to unexpected problem if i go with this (i think). So let's make this simple

For a pSphere, i want it's print "1" when the vis is on, and it will print "0" when the vis is off. At that moment only. I don't want to use scriptJob. So is there anyother way to do it?.

sciLoop
06-05-2012, 10:02 AM
In that case, you got to have the newVis ask the value for the visibility:

in the script editor:
addAttr -ln "newVis" -at bool |pSphere1;

in the expression editor:
pSphere1.newVis = pSphere1.visibility;
if(pSphere1.visibility == 0)
{
print ("0" + "\n");
}
else
{
print ("1" + "\n");
}

Then the print out happens only when the value of the visibility really changes.

noobmode
06-05-2012, 10:40 AM
It's work well, nice one sciLoop.
But i wonder why it's not working ulti i open the attribute menu. However, problem solve. Thank for your help :beer:

sciLoop
06-05-2012, 11:01 AM
ah, well, yes, thats a limitation. The request only starts when the attribute editor is open...

You could connect the translateX or so of another DAG to the newVis attribute. Then it should update without the help of the attribute editor....

The better version would be:
string $sphere[] = `polySphere -r 1 -sx 20 -sy 20 -ax 0 1 0 -cuv 2 -ch 1`;
string $cube[] = `polyCube -w 1 -h 1 -d 1 -sx 1 -sy 1 -sz 1 -ax 0 1 0 -cuv 4 -ch 1`;
string $exp = "pCube1.visibility = pSphere1.visibility;\n\n";
$exp += "if(pSphere1.visibility == 0)\n\t";
$exp += "{\n\t\t print (\"0\" + \"\\n\");\n\t}\n\t";
$exp += "else\n\t" ;
$exp += "{\n\t\tprint (\"1\" + \"\\n\");\n\t}";
expression -s $exp -o $sphere[0] -ae 1 -uc all ;

You could then scale the cube to zero, so it does not clutter the viewport.

noobmode
06-06-2012, 02:23 AM
A little confuse here.... mah, everything work out so i'm okay with it. Again, thank Rammner :thumbsup:

sciLoop
06-06-2012, 06:56 AM
I simply changed the triggering a bit. Now, an additional node is the trigger. Simply put that script in the script editor and hit execute.

noobmode
06-06-2012, 07:05 AM
ah, i talking about Maya's mechanisms, kind of funny. But soon, Maya should have a node in hyperShade to trigger those script too....

CGTalk Moderation
06-06-2012, 07:05 AM
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.