PDA

View Full Version : Possible to run something (scriptJob) when a particular tool "finishes"?


DeadlyNightshade
11-07-2012, 07:49 AM
Is it possible to run a script (via a scriptJob I guess) when a specific tool completes/finishes?
Say, I run the append to polygon -tool and when I'm done with it, I want a specific script to run.

Or do I have to rewrite that entire tool just to add my own stuff to it? (if so, how do you rewrite a tool?)

Nyro
11-07-2012, 10:44 AM
Off the top of my head I can only think of one way to ensure a script to be run whenever, and only if, a certain tool is completed: scriptCtx.

Basically you 'write' your own tool, which works like most other contexts in that you can select things and perform actions based on your selections. With a scriptCtx, you could replicate the original tool's (the one you wanted to work with in the first place) function and simply add your own code to its '-toolFinish' attribute.

DeadlyNightshade
11-07-2012, 11:22 AM
Ok thanks.
I'm not sure I can create what I want to create with that.

scriptCtx
-title "Shortest path tool"
-totalSelectionSets 1
-toolStart "ACTIVATE TOOL"
-finalCommandScript "myScript();"
myTool;


Also I'm getting an error here on the last line - Maya just says "error" and doesn't even tell me what's wrong. Placing everything within brackets solves nothing.
And is scriptCtx supposed to work like this anyway? Making a tool that runs another tool just seems awkward.

Nyro
11-07-2012, 11:26 AM
actually, no, I don't believe you can nestle a tool within a tool, i.e. you can't set the tool to run the command 'setToolTo' in its own command. That would take you out of the current Context (myTool) and never execute finalCommandScript.

Instead, look at the command underlying Shortest Path Tool, and mimic it with your own tool (and selections etc.)

DeadlyNightshade
11-07-2012, 12:10 PM
Okay well, my guess is that the normal "select shortest path tool" is just polySelect -commands with the -shortestEdgePathUV flag. But anyways, just out of curiosity: Where do I find the Maya tools? Not just this one in particular but all of them.

whatIs shortestPathTool;
// Result: Presumed Mel procedure; no definition seen yet. //

/sadface

Nyro
11-07-2012, 12:44 PM
You won't find tool definitions that way. The whatIs command only works on variables and commands. A tool is neither; it's a Context.

Some (but not all) tools are MEL scripts you can find in "..\Autodesk\Maya 201x\scripts".
Many tools are actually contexts built in to MEL. In this case you're looking at the "polyShortestPathCtx". Such contexts don't have a MEL script you can look up, unfortunately.

That said, you've already found out what the underlying command for the polyShortestPathCtx context is. So you can just use that in your own scriptContext.

DeadlyNightshade
11-07-2012, 12:55 PM
Yeah - it just sucks that I have to re-invent the wheel

NaughtyNathan
11-07-2012, 02:05 PM
scriptJob has ToolChanged and PostToolChanged events. these may be of use?

DeadlyNightshade
11-07-2012, 02:09 PM
Nathan: Yea those were the flags I thought of first. But having such a script job would mean that it would be global and activate every time the user changes tool - not just when they switch to a -specific- tool.

Or... is there a way to turn scriptJobs on and off? Maybe by using runOnce followed by a "kill" everytime I run my script? That could be the solution.

Nyro
11-07-2012, 03:02 PM
I thought of that, but had the same issue as Martin: it would trigger on every toolchange. Hence I immediately dismissed it out of efficiency concerns.

That said, it is possible:

Create a scriptJob for the 'ToolChanged' event. The script would then run the 'currentCtx' to query the tool the user just switched to. If its the proper tool, create a new scriptJob with -runOnce set to true and triggered on 'PostToolChanged' event to run a script when the user finishes using the current tool.

This is theoretical, don't have time to test it, but you can try to see if it works. One possible issue is that the user hits Enter to finish the tool but the tool doesn't actually switch, thus not triggering the final scriptJob...

Keilun
11-07-2012, 03:13 PM
If you don't need to wait until the tool actually exits, then you can skip creating the extra scriptJob.

I wouldn't concern yourself with efficiency concerns. ToolChanges aren't that frequent and it's a single MEL callback with an if statement and currentCtx command if it's not the tool you're interested in. I'm pretty sure that will have minimal impact on user interactivity.

Nyro
11-07-2012, 10:11 PM
I wouldn't concern yourself with efficiency concerns. ToolChanges aren't that frequent and it's a single MEL callback with an if statement and currentCtx command if it's not the tool you're interested in. I'm pretty sure that will have minimal impact on user interactivity.

Indeed. I suppose it's just my habit of avoiding unnecessary background processes. I just don't trust 'em and don't want them polluting my memory ;)

CGTalk Moderation
11-07-2012, 10:11 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.