Subject: Maya Python - ScriptJob Triggered Only on Mouse Release?

Subject: Maya Python - ScriptJob Triggered Only on Mouse Release?

ansonsavage
Participant Participant
1,051 Views
3 Replies
Message 1 of 4

Subject: Maya Python - ScriptJob Triggered Only on Mouse Release?

ansonsavage
Participant
Participant

Hi!

 

I've been working on a Python script in Maya that adjusts the focal length of a camera in real-time to maintain the apparent size of a selected object, even when the camera or the time range changes.

I'm using `cmds.scriptJob()` to create script jobs for when the time, playback range, or the camera translation (`tx`, `ty`, `tz`) changes. Here's a chunk of my code:


```

scriptJobIds['timeChange'] = cmds.scriptJob(e=("timeChanged", maintainFocalLengthRatio), protected=True)
scriptJobIds['playbackRangeChange'] = cmds.scriptJob(e=("playbackRangeChanged", maintainFocalLengthRatio), protected=True)
scriptJobIds['txChange'] = cmds.scriptJob(attributeChange=(cam + ".tx", maintainFocalLengthRatio), protected=True)
scriptJobIds['tyChange'] = cmds.scriptJob(attributeChange=(cam + ".ty", maintainFocalLengthRatio), protected=True)
scriptJobIds['tzChange'] = cmds.scriptJob(attributeChange=(cam + ".tz", maintainFocalLengthRatio), protected=True)

```

The problem I'm facing is that these script jobs only seem to be triggered when I release the mouse button after making changes to either the time slider or the camera transformation. I want the script job to be executed continuously as the changes are happening (e.g., as the camera is being translated), not just when the mouse button is released.

Does anyone have any ideas for how I could get the script job to trigger in real-time as changes are made, instead of waiting for the mouse release?

 

Any help or guidance would be much appreciated.

 

Thanks!

Anson

Accepted solutions (1)
1,052 Views
3 Replies
Replies (3)
Message 2 of 4

FirespriteNate
Advocate
Advocate

I don't know how to fix your issue I'm afraid, but one of the things mentioned in the command documentation for scriptJob is:

"Script jobs are tied to the event loop in the interactive application. They are run during idle events",

which suggests that they are not likely to execute while a mouse button is being held/dragged as this presumably counts as a "mouse event" and not idle. The idle state only returns once the mouse is released, which matches what you're experiencing here I think.

 

Runtime update/realtime feedback is usually done better with a node network (where attributes are connected to each other) or via a plug-in node compute(). I guess you could always write a plug-in node to perform your maintainFocalLengthRatio() functionality and connect it to the camera in/out, but this is going to be a lot more complicated. (and scene dependency on a plug-in for eternity is always an extra headache nobody needs).

 

0 Likes
Message 3 of 4

ansonsavage
Participant
Participant

@FirespriteNate 

 

Okay, neat, thanks for the answer!

 

I'm not familiar with plugin nodes, do you have any tutorials you'd recommend? Could I achieve a similar result (i.e., adjusting the camera's focal length based on the distance between the camera and a selected object) using expressions?

0 Likes
Message 4 of 4

ansonsavage
Participant
Participant
Accepted solution

Never mind, ChatGPT and I figured it out! 🙂

 

https://github.com/microMuffin/Focal_Lock/blob/maya/maya/focal_lock.py