Skip to content

Tag: python

Season 01 – Episode 24 – Scripting, Managing Plugs

Scripting Component Management, pt3, Maya Plugs Gotchas and Retrieval

More scripting, and specifically lots on plugs in Maya (MPlugs in the API).

Don’t be afraid if you’re not interested in programming. Or maybe not up to scratch on your scripting to follow the details.

This will NOT be necessary to follow the stream once we’ll resume non programming work.
You should still check a couple posts ago for more thoughts on that, and scripting in general, before you give up on the subject.

As before the “code” is available on my Public Repository on Github as linked here.

This episode we dig up the guide of the leg global component and refactor and simplify some connections with the consideration of the guiding/de-guiding automation needing a test bed.

We made good progress on the script front too and we should now have all we need for the final recipe of the guide swap.

Some insider knowledge on gotchas and managing plugs came up as well.

Enjoy,

Season 01 – Episode 23 – Scripting, Retrieving Container Items

Scripting Component Management, pt2, Retrieving from Containers

Don’t be afraid if you’re not interested. Or maybe not up to scratch on your scripting to follow the details.

This will NOT be necessary to follow the stream once we’ll resume non programming work.
You should still check my previous post for more thoughts on that, and scripting in general, before you give up on the subject.

As before the “code” is available on my Public Repository on Github as linked here.

We made some progress on our component instrumentation and we can now reliably retrieve interesting objects and their relationship to our components starting from a simple selection.

Enjoy,

Season 01 – Episode 22 – Scripting, Instrumenting Containers

Scripting Component Management, pt1, Instrumenting Containers

Following popular vote Scripting Sessions start again!

Don’t be afraid if you’re not interested. Or maybe not up to scratch on your scripting to follow the details.

This will NOT be necessary to follow the stream once we’ll resume non programming work.

I do, however, recommend that you try and practice, even if you’re unfamiliar with Python or OpenMaya, and need to use other means to obtain similar results.
Even if you’re completely new to programming and boil the experience down to copying and pasting then running the stuff I will be sharing. There is great mileage to be had in exploring interacting with the Maya client through means other than the UI.

Since we’ll be moving predominantly on the scripting front for a few streams I won’t be posting every bit of text, but all of it will be promptly made available on my Public Repository on Github as linked here.

We start by diving into some exploratory work to learn how to deal with containers through OpenMaya, and how high level concepts such as assets can be mapped to simple graph operations.

Our long term goal is to make the guiding and de-guiding process much, much easier than if we had to manage it manually.

Enjoy,

Pilot Season – Day 14 – Working dynamic callback

Day 14 of the stream, we finally install a fully working callback for the FK <-> IK switch on the pedals.

 

Finishing the Maya Callbacks and OpenMaya stretch of the stream we finally have a fully working FK <-> IK switch for the pedals.
We only have the virtual slider case to fix, which is more  a matter of finding a good stepped signal to work off of than anything else, and then maybe ensuring callbacks are installed every time the scene is opened.

Exercise for home, if you’re inclined:
Look into making the callback work with virtual sliders.

Hint 1: not unlike the reciprocal behaviour added as a demo to the end of the Day 11 Stream it’s about comparing the result we get, with something that changes reliably and not depending on UI interaction

Hint 2: the angles we get from the separate IK and FK feed could be compared against the angle coming from the blend, which is graph dependent and not UI dependent, and comparison should provide the clean state switch we’re after.

As with all previous script heavy days you will find the commented transcription at the end of the page.
Enjoy:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
fkik_attrName = 'FKIK_switch'
 
from maya.api import _OpenMaya_py2 as om2
from maya import cmds
import math
 
def iterSelection():
    """
    generator style iterator over current Maya active selection
    :return: [MObject) an MObject for each item in the selection
    """
    sel = om2.MGlobal.getActiveSelectionList()
    for i in xrange(sel.length()):
        yield sel.getDependNode(i)
 
 
def removeCallbacksFromNode(node_mob):
    """
    :param node_mob: [MObject] the node to remove all node callbacks from
    :return: [int] number of callbacks removed
    """
    cbs = om2.MMessage.nodeCallbacks(node_mob)
    cbCount = len(cbs)
    for eachCB in cbs:
        om2.MMessage.removeCallback(eachCB)
    return cbCount
 
 
def removeCallbacksFromSel():
    """
    Will remove all callbacks from each node in the current selection
    :return: [(int, int)] total number of objects that had callbacks removed,
                            and total count of all callbacks removed across them
    """
    cbCount = 0
    mobCount = 0
    for eachMob in iterSelection():
        mobCount += 1
        cbCount += removeCallbacksFromNode(eachMob)
    return mobCount, cbCount
 
 
def cb(msg, plug1, plug2, payload):
    if msg != 2056: #check most common case first and return unless it's
        return      # an attribute edit type of callback
 
    if not plug1.partialName(includeNodeName=False, useAlias=False) == fkik_attrName:
        # We ensure if the attribute being changed is uninteresting we do nothing
        return
 
    isFK = plug1.asBool() == False # Switched To FK
    isIK = not isFK # Switched to IK
 
    settingsAttrs = { # all interesting attribute names in keys, respective plugs in values
                     'fkRotation': None,
                     'ikRotation': None,
                     'fk_ctrl_rotx': None,
                     'ik_ctrl_translate': None,
                     'ikPedalOffset': None,
                     }
 
    mfn_dep = om2.MFnDependencyNode(plug1.node())
    # We populate the dictionary of interesting attributes with their plugs
    for eachPName in settingsAttrs.iterkeys():
        plug = mfn_dep.findPlug(eachPName, False)
        settingsAttrs[eachPName] = plug
 
    for p in settingsAttrs.itervalues():
        # We will exit early and do nothing if a plug couldn't be initialised, the object
        #  is malformed, or we installed the callback on an object that is only
        #  conformant by accident and can't operate as we expect it to.
        if p is None:
            return
 
    angle = None # empty init
    if isFK:
        # Simplest case, if we switched to FK we copy the roation from IK
        #  to the FK control's X rotation value
        angle = -settingsAttrs.get("ikRotation").source().asDouble()
        fkSourcePlug = settingsAttrs.get("fk_ctrl_rotx").source()
        fkSourcePlug.setDouble(angle)
    elif isIK:
        # If instead we switched to IK we need to
        #  derive the translation of the IK control that produces the result
        #  of an equivalent rotation to the one coming from the FK control
        angle = settingsAttrs.get("fkRotation").source().asDouble()
        projectedLen = settingsAttrs.get("ikPedalOffset").source().asDouble()
 
        y = ( math.cos(angle) * projectedLen ) - projectedLen
        z = math.sin(angle) * projectedLen
 
        ikSourcePlug = settingsAttrs.get("ik_ctrl_translate").source()
        for i in xrange(ikSourcePlug.numChildren()):
            realName = ikSourcePlug.child(i).partialName(includeNodeName = False, useAlias = False)
            if realName == 'ty':
                ikSourcePlug.child(i).setDouble(y)
            elif realName == 'tz':
                ikSourcePlug.child(i).setDouble(z)
 
 
removeCallbacksFromSel()
for eachMob in iterSelection():
    om2.MNodeMessage.addAttributeChangedCallback(eachMob, cb)

Pilot Season – Day 13 – Scripting Version of fk ik Switch

Day 13 of the stream, we keep keeping on with scripting until we obtain the right numbers and get ourselves ready to implement the dynamic, callback driven version

We conclude the scripting tangent required to deploy an actual dynamic FK to IK switch.

With OpenMaya2 introduced in the Day 12 stream, the design of the callback done and some attributes at hand, we finally get onto writing the actual code necessary for the callback to retrieve and transform the value until we obtain the right numbers.

As in previous scripting posts and videos the transcription of the code to the point we left it at the end of the episode is available at the end of this post.

Next stream all we’ll have to will be converting it to an actual callback and possibly ensuring it’s re-installed at scene open, but for now enjoy the scripting part taken from snippets to useful numbers:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from maya.api import _OpenMaya_py2 as om2
from maya import cmds
import math
 
def iterSelection():
    """
    generator style iterator over current Maya active selection
    :return: [MObject) an MObject for each item in the selection
    """
    sel = om2.MGlobal.getActiveSelectionList()
    for i in xrange(sel.length()):
        yield sel.getDependNode(i)
 
 
_MAYA_MATRIX_ATTRIBUTE_NAME = 'worldMatrix'
def wMtxFromMob(node_mob):
    """
    finds the world matrix attribute and returns its value in matrix form
    :param node_mob: [MObject] the node to get the world matrix from 
    :return: [MMatrix] the matrix value of the world transform on the argument node
    """
    if not node_mob.hasFn(om2.MFn.kDagNode):
        return None
 
    mfn_dag = om2.MFnDagNode(node_mob)
    wMtxPlug = mfn_dag.findPlug(_MAYA_MATRIX_ATTRIBUTE_NAME, False)
    elPlug = wMtxPlug.elementByLogicalIndex(0)
 
    node_mob_attr = elPlug.asMObject()
    mfn_mtxData = om2.MFnMatrixData(node_mob_attr)
    return mfn_mtxData.matrix()
 
 
def mtxFromPlugSource(plug):
    """
    takes a plug and retrieves the plug's source plug
      then will try to initialise matrix data from it and return it
      if valid
    :param plug: [MPlug] a plug connected to a matrix type source
    :return: [MMatrix | None]
    """
    if plug.isDestination:
        mtxPlug = plug.source()
        node_mob_attr = mtxPlug.asMObject()
        if node_mob_attr.hasFn(om2.MFn.kMatrixAttribute):
            mfn_mtxData = om2.MFnMatrixData(node_mob_attr)
            return mfn_mtxData.matrix()
    return None
 
 
def mPointFromPlugSource(plug):
    """
    similar to mtxFromPlugSource but will retrieve a translate compound source
    :param plug: [MPlug] a plug connected to a translate compound triplet source
    :return: [MPoing | None]
    """
    if plug.isDestination:
        sourcePlug = plug.source()
        if not  sourcePlug.isCompound or not sourcePlug.numChildren() == 3:
            return None
 
        mp = om2.MPoint()
        returnPoint = [False,False,False]
        for i in xrange(sourcePlug.numChildren()):
            realName = sourcePlug.child(i).partialName(includeNodeName = False, useAlias = False)
            if realName == 'tx':
                mp.x = sourcePlug.child(i).asFloat()
                returnPoint[0] = True
            elif realName == 'ty':
                mp.y = sourcePlug.child(i).asFloat()
                returnPoint[1] = True
            elif realName == 'tz':
                mp.z = sourcePlug.child(i).asFloat()
                returnPoint[2] = True
        if all(returnPoint):
            return mp
        return None
 
 
_MAYA_OUTPUT_ATTRIBUTE_NAME = 'output'
def getMRotFromNodeOutput(node_mob, rotOrder = om2.MEulerRotation.kXYZ):
    """
    finds the angular output of the argument node and returns it
     as a Euler rotation where that angle is the X element
    :param node_mob: [MObject] the node to get the output port from
    :param rotOrder: [int] the factory constant for the desired rotation order
                            the returned Euler rotation should be set to
    :return: [MEulerRotation] the Euler rotation composition where the
                                angular output on the argument node is the X value
    """
    mfn_dep = om2.MFnDependencyNode(node_mob)
    angle = om2.MAngle(0.0)
    if node_mob.hasFn(om2.MFn.kAnimBlend) and mfn_dep.hasAttribute(_MAYA_OUTPUT_ATTRIBUTE_NAME):
        plug = mfn_dep.findPlug(_MAYA_OUTPUT_ATTRIBUTE_NAME, False)
        angle = plug.asMAngle()
 
    rot = om2.MEulerRotation(angle.asRadians(), 0.0, 0.0, rotOrder)
    return rot
 
 
# A dictionary of all attributes we're interested in by name, ready
#   to accept values for each.
attribs = {
           'blendedRotation': None,
           'fk_bfr_mtx': None,
           'ik_bfr_mtx': None,
           'ikPedalOffset': None,
           }
 
 
mobTuple = tuple(iterSelection())
if mobTuple:
    mfn_dep = om2.MFnDependencyNode(mobTuple[0])
    plug = om2.MPlug()
    if mfn_dep.hasAttribute("FKIK_switch"):
        plug = mfn_dep.findPlug("FKIK_switch", False)
 
    if plug.isNull:
        print("invalid selection, no FKIK_switch attribute found on first selected item")
    else:
        for eachPName in attribs.iterkeys():
            plug = mfn_dep.findPlug(eachPName, False)
            attribs[eachPName] = plug
 
    isValid = True
    for p in attribs.itervalues():
        if p is None:
            isValid = False
            break
 
    if isValid:
        blendedRot = fk_bfr_mtx = ik_bfr_mtx = ikPedalOffset = None
        blendedRot = attribs.get("blendedRotation").source().asMAngle().asRadians()
        fk_bfr_mtx = mtxFromPlugSource(attribs.get("fk_bfr_mtx"))
        ik_bfr_mtx = mtxFromPlugSource(attribs.get("ik_bfr_mtx"))
        ikPedalOffset = mPointFromPlugSource(attribs.get("ikPedalOffset"))
 
        projectedLen = ikPedalOffset.y
        z = math.sin(blendedRot) * projectedLen
        y = (1.0-math.cos(blendedRot)) * -projectedLen
else:
    print("invalid selection count, nothing found selected from iterator")