Weld vertex (Python)

Hello,

I have been playing with the mergeVertexTool lately and i simply cannot get it to work like a weld vertex tool in Max. I have made a tool where if you selcet all verts it merges vertex’s that are on top of each other but cant get the 1st vertex to go on top of the 2nd vertex i selcet. I there already a simple solution to this already out there?

Thankyou in advance :slight_smile:

Ben

What are you expecting that it isn’t doing? I can use the merge vertex tool and drag from one vertex to another and it will merge the two. Is that not the behavior that you are getting?

Well…its not really doing anything? this seems like a weld function but not a target weld function?

def targetWeldCommand (*args):
 cmds.polyMergeVertex (am=True, d=0.001, tx=True, )
bt2 = cmds.button( label='Target Weld', width=100, command=targetWeldCommand )

polyMergeVertex welds any vertices that are within a certain distance of each other.

In order to weld two vertices together regardless of distance you have to use the alwaysMergeTwoVertices/am flag and pass the function exactly two vertices to weld. The am flag will only work if it is only operating on two vertices–if there are more it will behave as though am is False.
To weld two vertices:

def weldTwoVerts(a, b):
    pm.polyMergeVertex(a, b, am=True)

So am i looking at the wrong command to get what i want?

I want to hit target weld tool and then the 1st vertex i select merges with the 2nd vertex i select in the 2nd vertex’s position.
Sorry if I am sounding like a complete noob…i am :stuck_out_tongue:

def targetWeldCommand (a, b):
 pm.polyMergeVertex (a, b, am=True )
bt2 = cmds.button( label='Target Weld', width=100, command=targetWeldCommand )

Why not just use the Merge Vertex Tool in the Edit Mesh menu? It allows you to merge one vertex to another by dragging between them.

Becasue i am making my own maya ui which has all the modelling tools in one space

Sounds right, bcottage, I’d look into using:

pm.targetWeldCtx('targetWeldCtx1', e=True, mergeToCenter=False )

http://download.autodesk.com/us/maya/2011help/pymel/generated/functions/pymel.core.context/pymel.core.context.targetWeldCtx.html?highlight=weld#pymel.core.context.targetWeldCtx

The easiest method for building your own UI with existing Maya functionality is to create a new, temporary shelf, and hold down CTRL-SHIFT while choosing Menu Items. This creates a Shelf Button that executes the same code as the Menu Item. You can then open the Shelf Editor and inspect what command is executed when that menu item is picked (usually it is a runtime command, but sometimes it can be an embedded mel script).

You can then copy and paste this command into your own UI, or at the very least use the “whatIs” Mel command to find out more about how that particular feature works.

Thanks guys.

i have this which is telling me pm isnt defined?

def targetWeldCommand(*args): pm.targetWeldCtx(‘targetWeldCtx1’, e=True, mergeToCenter=False )
bt2 = cmds.button( label=’||Target Weld||’, width=100, command=targetWeldCommand )

In terms of the ui, i have a pretty good working docked ui which has all my other noob tools (real basic stuff to help me self teach) however i am in the middle of creating a new updated version of my script in QT designer.

“pm” is a commonly used alias for pymel, which is an alternative to “maya.cmds”

[QUOTE=bcottage;26234]So am i looking at the wrong command to get what i want?

I want to hit target weld tool and then the 1st vertex i select merges with the 2nd vertex i select in the 2nd vertex’s position.
Sorry if I am sounding like a complete noob…i am :stuck_out_tongue:

def targetWeldCommand (a, b):
 pm.polyMergeVertex (a, b, am=True )
bt2 = cmds.button( label='Target Weld', width=100, command=targetWeldCommand )

[/QUOTE]

As far as I can tell you have to move the first vert to the location of the second before merging in order to get the desired effect. Your process should be this:

User presses button:

  1. Get selected verts (must be only two verts)
  2. Get world position of the second vert (this is where we want the merged verts to be placed). Use cmds.xform to get it
  3. Move the first vert to the position of the second vert using cmds.xform
  4. Call polyMergeVertex with the two selected verts (cmds.polyMergeVertex(vert1, vert2, am=True)

You can track selection order, so I would probably do it like this:

  1. turn on selection order tracking
  2. select first vertex.
  3. selected second vertex.
  4. user presses Target Weld button.
    5+ like capper said.

I always forget that order selection isn’t on by default

“ls -selection” always returned in the order the objects were selected. Did they change something in a recent version?

It is stored in Preferences. I don’t believe it defaults to True, but I could be wrong (it is a performance hit to track the selection order).
As far as I can remember the “ls” command always returns the selection sorted, not in the order it was selected.

selection(sl) boolean create

List objects that are currently selected.

orderedSelection(os) boolean create

List objects and components that are currently selected in their order of selection. This flag depends on the value of the -tso/trackSelectionOrder flag of the selectPref command. If that flag is not enabled than this flag will return the same thing as the -sl/selection flag would.

I think it only applies to components though. I will check that out!

EDIT: We are dealing with components here… forget that post XD

[QUOTE=capper;26236]Why not just use the Merge Vertex Tool in the Edit Mesh menu? It allows you to merge one vertex to another by dragging between them.[/QUOTE]

Im trying to replicate exactly this. Finding it really hard atm :confused:

If you want the drag behavior, then just activate the merge vertex tool:

cmds.setToolTo('polyMergeVertexContext')

If you want that behavior with selected vertices, then the steps that rgkovach and I outlined should get you there. If you’re having problems with that let us know which parts and/or post your current code.

So a mate of mine taught me how to call mel commands in python. The following does the merge vertex tool.

import pymel.core as pm
from functools import partial

def launchVertexTool(*args):
    mel.eval('MergeVertexTool')
bt2 = cmds.button( label='Target Weld', width=100, command=launchVertexTool)

Thankyou guys for all your help :slight_smile: