Hi Michael,

Normaly picture is a GIF. I think Google get just first frame au GIF...

I try to store output value "outTranslate" for use it when i load my scene.
I set "outTranslate" attribute to storable and in line 116 of my script I 
get attribute value for initialize one variable. I not sure this solution 
is really good beacause cause an error in first compute.
# RuntimeError: maximum recursion depth exceeded in cmp // 


-- 
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_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/92fc610c-e995-427f-b53b-cfc74814a5d8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Attachment: distanceConstraint.ma
Description: Binary data

# coding=ascii

"""
!@Brief Maya animation node.
"""

# ========================================================
#   Initialize Plugin
# ========================================================

import math

from maya import OpenMaya, OpenMayaMPx


# ========================================================
#   Initialize Plugin
# ========================================================

class DistanceConstraint(OpenMayaMPx.MPxNode):

    """
    !@Brief Implementation of distance constraint node.
    """

    kPluginNode = "distanceConstraint"
    kPluginNodeID = OpenMaya.MTypeId(0x2051339)
    kPluginNodeType = OpenMayaMPx.MPxNode.kDependNode

    SOURCE = OpenMaya.MObject()
    DISTANCE = OpenMaya.MObject()

    OUT_TRANSLATE_X = OpenMaya.MObject()
    OUT_TRANSLATE_Y = OpenMaya.MObject()
    OUT_TRANSLATE_Z = OpenMaya.MObject()
    OUT_TRANSLATE = OpenMaya.MObject()

    # ==================================================
    #   Node Init
    # ==================================================

    @classmethod
    def creator(cls):
        return OpenMayaMPx.asMPxPtr(cls())

    def __init__(self):
        super(DistanceConstraint, self).__init__()

        self._mv_last_anchor = None
        self._mv_point = None

    @classmethod
    def initializer(cls):
        
        a_in_attributes = list()
        a_out_attributes = list()

        #   Input
        source_attr = OpenMaya.MFnMatrixAttribute()
        cls.SOURCE = source_attr.create('source', 's')
        source_attr.setStorable(True)
        source_attr.setKeyable(False)
        a_in_attributes.append(cls.SOURCE)

        distance_attr = OpenMaya.MFnNumericAttribute()
        cls.DISTANCE = distance_attr.create('distance', 'd', OpenMaya.MFnNumericData.kDouble, 1.0)
        distance_attr.setStorable(True)
        distance_attr.setKeyable(True)
        a_in_attributes.append(cls.DISTANCE)

        #   Output
        out_translate_X_attr = OpenMaya.MFnNumericAttribute()
        cls.OUT_TRANSLATE_X = out_translate_X_attr.create("outTranslateX", "otx", OpenMaya.MFnNumericData.kDouble, 0.0)
        out_translate_y_attr = OpenMaya.MFnNumericAttribute()
        cls.OUT_TRANSLATE_Y = out_translate_y_attr.create("outTranslateY", "oty", OpenMaya.MFnNumericData.kDouble, 0.0)
        out_translate_z_attr = OpenMaya.MFnNumericAttribute()
        cls.OUT_TRANSLATE_Z = out_translate_z_attr.create("outTranslateZ", "otz", OpenMaya.MFnNumericData.kDouble, 0.0)
        out_translate_attr = OpenMaya.MFnNumericAttribute()
        cls.OUT_TRANSLATE = out_translate_attr.create("outTranslate", "ot", cls.OUT_TRANSLATE_X, cls.OUT_TRANSLATE_Y, cls.OUT_TRANSLATE_Z)
        out_translate_attr.setKeyable(False)
        out_translate_attr.setStorable(True)
        a_out_attributes.append(cls.OUT_TRANSLATE)

        #   Add attribute
        for mo_attribute in (a_in_attributes + a_out_attributes):
            cls.addAttribute(mo_attribute)

        #   Attribute dependencies
        for mo_out_attribute in a_out_attributes:
            for mo_in_attribute in a_in_attributes:
                cls.attributeAffects(mo_in_attribute, mo_out_attribute)

    # ==================================================
    #   Compute
    # ==================================================

    def compute(self, plug, data):

        #   Set valid plugs
        a_valid_output = [self.OUT_TRANSLATE_X, self.OUT_TRANSLATE_Y, self.OUT_TRANSLATE_Z, self.OUT_TRANSLATE]
        if plug in a_valid_output:
            
            #   Get Data
            mm_source = data.inputValue(self.SOURCE).asMatrix()
            f_distance = data.inputValue(self.DISTANCE).asDouble()

            mtr_source = OpenMaya.MTransformationMatrix(mm_source)
            mv_anchor = mtr_source.getTranslation(OpenMaya.MSpace.kWorld)

            #   Compute only if current anchor is different of last anchor
            if self._mv_last_anchor is not None and mv_anchor == self._mv_last_anchor:
                return

            #   Get output point
            if self._mv_point is None:
                mp_out_translate = OpenMaya.MFnDependencyNode(self.thisMObject()).findPlug(self.OUT_TRANSLATE)
                self._mv_point = OpenMaya.MVector(
                    mp_out_translate.child(0).asDouble(),
                    mp_out_translate.child(1).asDouble(),
                    mp_out_translate.child(2).asDouble()
                )
            else:
                mv_diff = self._mv_point - mv_anchor
                if mv_diff.length() > f_distance:
                    self._mv_point = (mv_diff.normal() * f_distance) + mv_anchor

            self._mv_last_anchor = mv_anchor

            #   Set output
            if plug == self.OUT_TRANSLATE_X:
                h_out_translate_x = data.outputValue(self.OUT_TRANSLATE_X)
                h_out_translate_x.setDouble(self._mv_point.x)

            if plug == self.OUT_TRANSLATE_Y:
                h_out_translate_y = data.outputValue(self.OUT_TRANSLATE_Y)
                h_out_translate_y.setDouble(self._mv_point.y)

            if plug == self.OUT_TRANSLATE_Z:
                h_out_translate_z = data.outputValue(self.OUT_TRANSLATE_Z)
                h_out_translate_z.setDouble(self._mv_point.z)

            if plug == self.OUT_TRANSLATE:
                h_out_translate = data.outputValue(self.OUT_TRANSLATE)
                h_out_translate.child(self.OUT_TRANSLATE_X).setDouble(self._mv_point.x)
                h_out_translate.child(self.OUT_TRANSLATE_Y).setDouble(self._mv_point.y)
                h_out_translate.child(self.OUT_TRANSLATE_Z).setDouble(self._mv_point.z)

            data.setClean(plug)


# ========================================================
#   Initialize Plugin
# ========================================================

#   load
def initializePlugin(m_object):

    """
    !@Brief
    """

    plugin = OpenMayaMPx.MFnPlugin(
        m_object,
        "Remi Deletrain -- remi.deletr...@gmail.com",
        "1.0",
        "Any"
    )

    try:
        plugin.registerNode(
            DistanceConstraint.kPluginNode,
            DistanceConstraint.kPluginNodeID,
            DistanceConstraint.creator,
            DistanceConstraint.initializer,
            DistanceConstraint.kPluginNodeType
        )
    except Exception as e:
        raise RuntimeError("Failed to register command: {0}\n{1}".format(DistanceConstraint.kPluginNode, e))


#   Unload
def uninitializePlugin(m_object):

    """
    !@Brief
    """

    plugin = OpenMayaMPx.MFnPlugin(m_object)

    try:
        plugin.deregisterNode(DistanceConstraint.kPluginNodeID)
    except Exception as e:
        raise RuntimeError("Failed to register command: {0}\n{1}".format(DistanceConstraint.kPluginNode, e))

Reply via email to