Notifications
Clear all

[Closed] problem with callback and vert selection

I’m trying to make a simple script where, when the checkbutton is activated, the first vert you select while holding shift, it stores the position, and then next vert you select while hold shift moves it to that first position

example…
shift select vert 1 – stores position
shift select vert 2 – moves to vert 1 position

shift select vert 3 – stores position
shift select vert 4 – moves to vert 3 position

etc.

I’ve always had problems trying to get callbacks to work in these instances because i don’t know which ones to pick…

And i’m sure this could be done far more efficiently but i’m not sure how… also i have to use mesh for reasons i can’t go into…

here’s what i have so far, help would be appreciated. thanks


  (
  	global AlignVertCallback
  	
  	rollout AlignVertCallback "" width:134 height:26
  	(
  		checkButton run "Run" pos:[19,5] width:101 height:16
  
  		local MasterPos
  		local pressed = 1
  		  
  		  -- Function for getting vert position
  		  -- ====================================
  		  fn getMeshVert obj =
  		  (
  				  VertArray = getVertSelection(obj) as array
  				return( getVert obj VertArray[1] )
  		  );
  		  
  		   -- Function for moving vert position
  		  -- ====================================
  		  fn moveMeshVert obj pos =
  		  (
  				  VertArray = getVertSelection($) as array
  				
  				for vert in VertArray do
  					meshOp.moveVert obj vert pos
  		  );
  		  
  		  -- Function for helping with aligning verts
  		  -- ====================================
  		  fn Select_nMove obj =
  		  (
  				  if obj != undefined then
  				  (
  					  if keyboard.SHIFTpressed and subobjectLevel == 1 do 
  					  (
  							-- first pressed
  							if pressed == 1 then
  							   (
  								MasterPos = ( getMeshVert obj )
  								pressed = 2
  							)
  							-- second pressed
  							 else	
  							   (
  								moveMeshVert obj MasterPos
  								pressed = 1
  							)
  					 )		
  				  )
  		  )
  		  
  		  -- While loop to make sure button is running.
  		  -- ====================================
  		  
  			on run changed state do
  			(
  				if state == on then
  					callbacks.addScript #selectionSetChanged "AlignVertCallback.Select_nMove $" id:#SHIFTalignVerts 
  				else
  					callbacks.removeScripts id:#SHIFTalignVerts
  			 )
  	 
    
  	)-- end rollout
  	createDialog  AlignVertCallback style:#(#style_toolwindow, #style_sysmenu, #style_resizing)
  )
  
2 Replies

Hi Hobbs,
here is the script you’re asking for. It works in a slightly different way than your request: No callbacks, No SHIFT to press, Preview line of action.
Select an Editable Mesh object, activate it, pick the “first point”, it will then ask for other points to shift on the picked vertex. Click on as many verts as you want to move, then right click to be able to pick another “first point”, or right click again to exit the script. I hope it is what you need.


rollout rolTest "Shift Verts"
(

-- Rollout local variables -----------------------------------------------------

    local MeshObj = undefined

    local iVertSource = 0
    local iVertTarget = 0

    local iClosestVert = 0
    local iMeshFace = 0

    local iClickNumber = 0
    local bShowPickActive = false
    local bShowShiftActive = false

-- Rollout interface -----------------------------------------------------------

    button btRun "Shift Mesh Verts" width:90 align:#center offset:[0, -2]

-- Functions -------------------------------------------------------------------

    function getClosestVert oMesh iMeshFace p3PointOnFace =
    (
        local iClosestVert = 0
        local fShortestDist = 1e6
        local fDist = 0
    
        local baFaceVerts = meshOp.getVertsUsingFace oMesh iMeshFace

        for iVert in baFaceVerts do
        (
            fDist = distance (getVert oMesh iVert) p3PointOnFace
    
            if (fDist < fShortestDist) then
            (
                fShortestDist = fDist
                iClosestVert = iVert
            )
        )
        return iClosestVert
    )

---------------------------------------

    function doShift oMesh iVertSource iVertTarget =
    (
        local p3VertSourcePos = meshOp.getVert oMesh iVertSource
        meshop.setVert oMesh iVertTarget p3VertSourcePos
    )

-- Viewport drawing ------------------------------------------------------------

    function showPick &oMesh &iVertSource =
    (
        gw.setTransform (matrix3 1)

        local p3SourcePos = (meshOp.getVert oMesh iVertSource)

        gw.marker p3SourcePos #hollowBox color:green

        gw.wText [mouse.pos.x +7, mouse.pos.y +20, 0] ("GET") color:green

        gw.enlargeUpdateRect #whole
        gw.updateScreen()
    )

    function regShowPick = showPick &MeshObj &iVertSource

---------------------------------------

    function showShift &oMesh &iVertSource &iVertTarget =
    (
        gw.setTransform (matrix3 1)
        gw.setColor #line white

        local p3SourcePos = (meshOp.getVert oMesh iVertSource)
        local p3TargetPos = (meshOp.getVert oMesh iVertTarget)

        gw.polyLine #(p3SourcePos, p3TargetPos) false --rgb:#(white, white)
        
        gw.marker p3SourcePos #hollowBox color:green
        gw.marker p3TargetPos #hollowBox color:red

        gw.wText [mouse.pos.x +7, mouse.pos.y +20, 0] ("SET") color:red

        gw.enlargeUpdateRect #whole
        gw.updateScreen()
    )

    function regShowShift = showShift &MeshObj &iVertSource &iVertTarget

-- MouseTrack function ---------------------------------------------------------

    function doTest msg ir obj faceNum shift ctrl alt =
    (
        case msg of
        (
            #freeMove:
            (
                if (iClickNumber == 0) then
                (
                    if (ir != undefined) then
                    (
                        iVertSource = getClosestVert MeshObj faceNum ir.pos

                        nodeInvalRect MeshObj
                        redrawViews()

                        if (bShowPickActive == false) then
                        (
                            registerRedrawViewsCallback regShowPick
                            bShowPickActive = true
                            completeRedraw()
                        )
                    )
                    else
                    (
                        unRegisterRedrawViewsCallback regShowPick
                        bShowPickActive = false
                    )
                    #continue
                )
                else
                (
                    if (ir != undefined) then
                    (
                        iVertTarget = getClosestVert MeshObj faceNum ir.pos
                        
                        nodeInvalRect MeshObj
                        redrawViews()

                        if (bShowShiftActive == false) then
                        (
                            registerRedrawViewsCallback regShowShift
                            bShowShiftActive = true
                            completeRedraw()
                        )
                    )
                    else
                    (
                        unRegisterRedrawViewsCallback regShowShift
                        bShowShiftActive = false
                    )
                )
                #continue                
            )
            #mousePoint:
            (
                if (ir != undefined) then
                (
                    iClickNumber += 1
                    
                    if (iClickNumber == 1) then
                    (
                        iVertSource = getClosestVert MeshObj faceNum ir.pos
                        iVertTarget = getClosestVert MeshObj faceNum ir.pos
    
                        unRegisterRedrawViewsCallback regShowPick
                        registerRedrawViewsCallback regShowShift
                        completeRedraw()
                    )
                    else
                    (
                        iVertTarget = getClosestVert MeshObj faceNum ir.pos
                        doShift MeshObj iVertSource iVertTarget 
                    )
                )
                #continue
            )
            #mouseMove:
            (
                #continue
            )
            #mouseAbort:
            (
                if (iClickNumber != 0) then
                (
                    iClickNumber = 0

                    unRegisterRedrawViewsCallback regShowShift
                    registerRedrawViewsCallback regShowPick
                    completeRedraw()
                    
                    #continue
                )
                else
                (
                    unRegisterRedrawViewsCallback regShowPick
                    completeRedraw()
            
                    #stop
                )
            )
        )
    )

-- Interface events ------------------------------------------------------------

    on btRun pressed do
    (
        if ( (selection.count == 1) and ((classOf selection[1]) == Editable_Mesh) ) then
        (
            MeshObj = selection[1]
    
            btRun.enabled = false
            mouseTrack on:MeshObj trackCallback:doTest
            
            unRegisterRedrawViewsCallback regShowPick
            unRegisterRedrawViewsCallback regShowShift

            btRun.enabled = true
        )
        else
        (
            messageBox "Please select a single Editable Mesh object" title:"Shift Mesh Verts"
        )
    )

--------------------------------------------------------------------------------

) -- End Rollout

createDialog rolTest 96 27 style:#(#style_toolwindow, #style_border, #style_sysmenu)


  • Enrico

sweet tool man . thanks for the time and effort.