Snapping an object to another object vertex in Maya over Python Script

Viewed 3897

I want to snap / align an object A to vertex of object B. I have tried using below script but its not snapping exactly to the vertex but snapping with an offset. Can any one suggest me a solution. Here are the snapshots.

import maya.cmds as cmds

vertices = [];
srcObj = "pCone1";

def snapToVertex(vertex,object):

    cmds.select(vertex);
    x,y,z = cmds.pointPosition();
    cmds.select(object);
    cmds.duplicate();
    cmds.move(x,y,z);

def processTask():

    cmds.select( cmds.polyListComponentConversion( tv=True ) );        
    vertices = cmds.ls(sl = True);

    print vertices;
    for vrtx in vertices:
        snapToVertex(vrtx,srcObj);

processTask();

Snapped with my above Script, Which didn't snap exactly to vertex.

enter image description here

But it should be snapped exactly to the Vervex's as below image.

enter image description here

2 Answers
Related