Community
Maya Programming
Welcome to Autodesk’s Maya Forums. Share your knowledge, ask questions, and explore popular Maya SDK topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Proximity Constraint Python/Mel Command (maya 2020)

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
cphyffer
841 Views, 3 Replies

Proximity Constraint Python/Mel Command (maya 2020)

Hello,

 

I cant seem to get any output log when looking up the command to perform a Proximity Constraint. Also couldn't find any api documentation on this.

 

What command performs a proximity Constraint?

 

Chris

3 REPLIES 3
Message 2 of 4
Kahylan
in reply to: cphyffer

Hi!

 

The proximity pin is a bit complexer of a setup than the other constraints, it also has more options to choose from and the options will actually influence how the nodenetwork created by the constraint looks like. So there isn't a command to create this constraint.

 

The easiest way to script this is to create the proximity constraint with the settings you want, and then look at the node network and recreate it node by node using code. In most proximity pin setups there doesn't have to be much created since the proximityPin node handles all calculation.

 

As an Example:

I create a proximity Pin for a selected shape with a selected transform as an input, a new transform as output and no maintained offset. The Nodenetwork looks like this.

proximityPin_setup.png

 

Now if I wanted to create the same setup in pyhton, it would look like this:

import maya.cmds as mc

def proximityConst(shape = "", inTrans = ""):
    """
    creates a proximityPin with the settings input: existing transform , output: new Transform
    mode: edge, maintain offset: False.
    Needs kwargs: 'shape' for shapenode to connect to, 'inTrans' for name of input
    """
    #getting original shape node
    shapeOrig = mc.listConnections("{0}.inMesh".format(shape), d = True)
    #creating original state nodes if no shapeOrig exists
    if shapeOrig == None:
        dup = mc.duplicate(shape, n= "{0}OrigTrans".format(shape), rc = True)[0]
        shapeOrig= mc.listRelatives(dup, s = True)[0]
        shapeOrig = mc.rename(shapeOrig, "{0}Orig".format(shape))
        mc.setAttr("{0}.visibility".format(dup),0)
        mc.setAttr("{0}.intermediateObject".format(shapeOrig), 1)
        mc.connectAttr("{0}.worldMesh[0]".format(shapeOrig), "{0}.inMesh".format(shape))
    #getting name for existing shapeOrig from list
    else:
        shapeOrig = shapeOrig[0]
    #creating the proximityPin node
    pin = mc.createNode("proximityPin", n = "{0}_poximityPin".format(shape))
    
    #setting input connections for proximity Pin
    mc.connectAttr("{0}.worldMesh[0]".format(shape), "{0}.deformedGeometry".format(pin))
    mc.connectAttr("{0}.outMesh".format(shapeOrig), "{0}.originalGeometry".format(pin))
    mc.connectAttr("{0}.matrix".format(inTrans), "{0}.inputMatrix[0]".format(pin))
    
    #creating output transform
    outTrans = mc.createNode("transform", n = "{0}_pinTrans".format(shape))
    #setting input connections for output transform
    mc.connectAttr("{0}.outputMatrix[0]".format(pin), "{0}.offsetParentMatrix".format(outTrans))
    

proximityConst(shape = "Shape", inTrans = "inputTransform")   
        

 

Now this code is something I just wrote for this in like 15 min, so it can certainly be improved, but I thought an example might be helpful.

 

I hope this helps!

Message 3 of 4
cphyffer
in reply to: cphyffer

This is fantastic Kahylan. Thank you so much for taking the time to help me even with examples. It even helped solve an issue of attributes I was having. I will incorporate this code and if it's clear I'll mark this as the solution Immediately.

Message 4 of 4
Kahylan
in reply to: cphyffer

Nice to hear!

I actually just realised a mistake in my code and fixed it. The way I was looking for the shapeOrig node was neglecting the possibility that there are existing deformers on the mesh which would be between the shape and the shapeOrig, so I had to tweek the else statement a bit to account for that.

 

import maya.cmds as mc

def proximityConst(shape = "", inTrans = ""):
    """
    creates a proximityPin with the settings input: existing transform , output: new Transform
    mode: edge, maintain offset: False.
    Needs kwargs: 'shape' for shapenode to connect to, 'inTrans' for name of input
    """
    #getting original shape node
    shapeOrig = mc.listConnections("{0}.inMesh".format(shape), d = True)
    #creating original state nodes if no shapeOrig exists
    if shapeOrig == None:
        dup = mc.duplicate(shape, n= "{0}OrigTrans".format(shape), rc = True)[0]
        shapeOrig= mc.listRelatives(dup, s = True)[0]
        shapeOrig = mc.rename(shapeOrig, "{0}Orig".format(shape))
        mc.setAttr("{0}.visibility".format(dup),0)
        mc.setAttr("{0}.intermediateObject".format(shapeOrig), 1)
        mc.connectAttr("{0}.worldMesh[0]".format(shapeOrig), "{0}.inMesh".format(shape))
    #getting name for existing shapeOrig from list
    else:
        if mc.objectType(shapeOrig[0]) == "mesh":
            shapeOrig = shapeOrig[0]
        else:
            shapeOrig = mc.listConnections("{0}.originalGeometry[0]".format(shapeOrig[0]), d = True, sh = True)[0]
    #creating the proximityPin node
    pin = mc.createNode("proximityPin", n = "{0}_poximityPin".format(shape))
    
    #setting input connections for proximity Pin
    mc.connectAttr("{0}.worldMesh[0]".format(shape), "{0}.deformedGeometry".format(pin))
    mc.connectAttr("{0}.outMesh".format(shapeOrig), "{0}.originalGeometry".format(pin))
    mc.connectAttr("{0}.matrix".format(inTrans), "{0}.inputMatrix[0]".format(pin))
    
    #creating output transform
    outTrans = mc.createNode("transform", n = "{0}_pinTrans".format(shape))
    #setting input connections for output transform
    mc.connectAttr("{0}.outputMatrix[0]".format(pin), "{0}.offsetParentMatrix".format(outTrans))
    

proximityConst(shape = "Shape", inTrans = "inputTransform")   
        
    

 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report