Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
# python
#
# Author: Jeff LeRoy (extol) www.leroyfx.com
#
# Ex_SaveRestoreSelections.py v1.0
#
#------------------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------------------
# This is a exercise I did to understand how to save and restore the current selection
# in modo. So if you have a element selected it will save that selection, then a dialog will open
# letting you know the selection has been droppen, after hitting ok the selection will be restored.
#------------------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------------------
#
L_currentSelection = []
currentLayer = lx.eval ('query layerservice layer.index ? main')
selMode = lx.eval ('query layerservice selmode ?')
#
#------------------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------------------
def DropSelection ():
#drop selection and open dialog
#this is just to do something to test restoring selections
lx.eval ('select.drop %s' % selMode)
lx.eval ('dialog.setup info')
lx.eval ('dialog.title "Selection Dropped"')
lx.eval ('dialog.msg "Your selection has been dropped"')
lx.eval ('dialog.open')
#query for what is selected in the current mode
if selMode == "vertex":
selection = lx.eval ('query layerservice verts ? selected')
sel_Type = "vert"
elif selMode == "edge":
selection = lx.eval ('query layerservice edges ? selected')
sel_Type = "edge"
elif selMode == "polygon":
selection = lx.eval ('query layerservice polys ? selected')
sel_Type = "poly"
sel_Count = lx.eval('query layerservice %s.N ?' % sel_Type)
if selMode == "edge":
if sel_Count > 1:
#add selected edges to a list
#each element/edge in the list is a string
for s in selection:
L_currentSelection.append(s)
DropSelection ()
#reselect edges from list
for edge in L_currentSelection:
edgeVerts = edge[1:-1]#remove the parentheses from the string
edgeVerts = edgeVerts.split(',')#split the string, creating a list
lx.eval ( 'select.element %s edge add index:%s index2:%s' % (currentLayer,edgeVerts[0], edgeVerts[1]))
elif sel_Count == 1:
DropSelection ()
edgeVerts = selection[1:-1]
edgeVerts = edgeVerts.split(',')
lx.eval ( 'select.element %s edge add index:%s index2:%s' % (currentLayer,edgeVerts[0], edgeVerts[1]))
elif selMode == "vertex" or "polygon":
if sel_Count > 1:
for s in selection:#add to list
L_currentSelection.append(s)
DropSelection ()#do Stuff
for s in L_currentSelection: #reselect what was selected
lx.eval ('select.element layer:%s type:%s mode:add index:%s' %(currentLayer,selMode,s))
elif sel_Count == 1:
DropSelection ()#do stuff
lx.eval ('select.element layer:%s type:%s mode:add index:%s' %(currentLayer,selMode,selection))
#------------------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment