Quick question on node.

31 views
Skip to first unread message

Padraig Ó Cuínn

unread,
May 30, 2020, 4:37:45 PM5/30/20
to Python Programming for Autodesk Maya
Hi Everyone,

Is it possible to use closestpointonmesh node to connect to an sdk to achieve a rigged action when something is close to the point.

Ravi Jagannadhan

unread,
May 30, 2020, 8:49:22 PM5/30/20
to python_in...@googlegroups.com
Can you clarify what you mean by 'connect to an sdk'?

On Sat, May 30, 2020 at 1:37 PM Padraig Ó Cuínn <patchquin...@gmail.com> wrote:
Hi Everyone,

Is it possible to use closestpointonmesh node to connect to an sdk to achieve a rigged action when something is close to the point.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/5d67740c-ad07-42a1-9646-93a1339fd878%40googlegroups.com.


--
Where we have strong emotions, we're liable to fool ourselves - Carl Sagan

Marcus Ottosson

unread,
May 31, 2020, 3:29:26 AM5/31/20
to python_in...@googlegroups.com

It sounds like he means to have a function called whenever something comes close, like a trigger or callback. If so, then yes, but not directly. The node itself doesn’t have any notion of a callback, but you can instead monitor (poll) the output of that node at some rate to roll your own callback mechanism.

triggerOnDistance.gif

from PySide2 import QtCore
from maya.api import OpenMaya as om

# Track triggered status
triggered = False

def monitor_distance(point1, point2):
    """Call some Python commands when the distance reaches some value

    Arguments:
        point1 (str): Path to a position attribute
        point2 (str): ...

    """

    # Leverage the Maya API to compute distance between two positions
    p1 = om.MPoint(*cmds.getAttr(point1))
    p2 = om.MPoint(*cmds.getAttr(point2))
    distance = p1.distanceTo(p2)

    # Avoid being called twice
    global triggered

    if distance < 2.0:
        if not triggered:
            print("In range..")
            cmds.setAttr("blinn1.color", 1, 0.5, 0.5, type="double3")
            triggered = True

    else:
        if triggered:
            print("..out of range")
            cmds.setAttr("blinn1.color", 0.5, 0.5, 0.5, type="double3")
        triggered = False

point1 = "cpConstraintInShape.worldPosition"
point2 = "cpConstraintPosShape.worldPosition"

monitor = QtCore.QTimer()
monitor.setInterval(100)  # Poll every 100 ms
monitor.timeout.connect(lambda: monitor_distance(point1, point2))
monitor.start()
#monitor.stop()

Hopefully the code makes clear where you’ve got your calls to Python/API, and where to edit the distance and so forth.

This particular example polls every 100 ms, but if you’ve got some sort of rig or animation where you need something to happen on the exact frame where the distance reaches the goal then this might miss the mark; in that case you could instead poll whenever the frame changes. There are various callbacks for that in both the API and the Maya script node you could use.


Padraig Ó Cuínn

unread,
May 31, 2020, 6:41:08 AM5/31/20
to Python Programming for Autodesk Maya
Thanks Marcus,

This is exactly what i need. Would it be possible to use this code to trigger a rig state from IKFK to dynamic ( Ideally i would like it to be able to "connect" or "attach" to the point and enable the rig state.

Reply all
Reply to author
Forward
0 new messages