Skip to content

Instantly share code, notes, and snippets.

@Jeff-LeRoy
Last active April 6, 2016 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jeff-LeRoy/af6f12cd03d0bc058ce5a82a3028d0a6 to your computer and use it in GitHub Desktop.
Save Jeff-LeRoy/af6f12cd03d0bc058ce5a82a3028d0a6 to your computer and use it in GitHub Desktop.
# python
# Author: Jeff LeRoy (extol) www.leroyfx.com
# Ex_Quick_Connect.py
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
#TO-DO / BUGS
#-------------------------------------------------------------------------------
#Still need to handle error close
#-------------------------------------------------------------------------------
#select main layer
sel_mesh_ID = lx.eval('query layerservice layer.index ? main')
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
#Variables
#-------------------------------------------------------------------------------
edg_count_selected = lx.eval('query layerservice edge.N ?')#List of edges selected
edg_tup_frm_modo = ()#Tuple from modo containing selected edges
vrt_sel_order_b = []#Second list of ordered verts to select
vrt_sel_order_a = []#First list of ordered verts to select
end_crawl_vrts = []#The last two verts of the crawl
two_helper_edges = []#Two longest edges in selection
ply_main = int#poly shared between helper edgs
#-------------------------------------------------------------------------------
#Functions
#-------------------------------------------------------------------------------
def Edge_Crawl(test_vrt, v_s_o_lsts):
#lx.out("Edge_Crawl")
#Loop through edg_List_frm_modo with every edge from edg_tup_frm_modo
#Then extract verts and turn two verts into a list
for i in edg_list_frm_modo:
#lx.out("Testing with vert", test_vrt)
tmp = i[1:-1]
tmp = tmp.split(',')
#Test and see if the current crawl vert is in the list just made
if test_vrt in tmp:
idx = tmp.index(test_vrt)
#lx.out("MATCH with vert", test_vrt)
#found a match so remove from list no need to test on other verts
edg_list_frm_modo.remove(i)
#If select the other vert in the list as the next crawl vert
if idx == 0:
crwl_vrt_nxt = tmp[1]
v_s_o_lsts.append(crwl_vrt_nxt)
elif idx == 1:
crwl_vrt_nxt = tmp[0]
v_s_o_lsts.append(crwl_vrt_nxt)
#Found a match no need to test the rest
break
def Extract_Vertices(edge):
vrt_tmp = edge[1:-1]
vrt_tmp = vrt_tmp.split(',')
vrt_a, vrt_b = vrt_tmp
#vrt_a = vrt_tmp[0]
#vrt_b = vrt_tmp[1]
return(vrt_a, vrt_b)
def Error_Close():
lx.eval('dialog.setup info')
lx.eval('dialog.title "Ex_Quick_Connect"')
lx.eval('dialog.msg "I can\'t find a shared polygon, quitting script."')
lx.eval('dialog.open')
quit()
#-------------------------------------------------------------------------------
#Too many edges selected so quit script
#-------------------------------------------------------------------------------
if edg_count_selected > 2:
lx.eval('dialog.setup info')
lx.eval('dialog.title "Ex_Quick_Connect"')
lx.eval('dialog.msg "Too many edges are selected. '
'Only select two edges"')
lx.eval('dialog.open')
quit()
#-------------------------------------------------------------------------------
#Not enough edges are selected
#-------------------------------------------------------------------------------
elif edg_count_selected <= 1:
lx.eval('dialog.setup info')
lx.eval('dialog.title "Ex_Quick_Connect"')
lx.eval('dialog.msg "You need to select two edges"')
lx.eval('dialog.open')
quit()
#-------------------------------------------------------------------------------
#Find shared polygon between two edges, select that polygon, then convert to
#edges, remove those edges from elist in modo because they dont need to be used
#in Edge_Crawl function
#-------------------------------------------------------------------------------
elif edg_count_selected == 2:
##---
edg_tup_frm_modo = lx.eval('query layerservice edges ? selected')#Helper Edgs
#Add helper edgs to list
#Doing this here because we will query modo again for selected edges after we convert
#And we need to remove them from the list so they wont be used in Edge_Crawl
for i in edg_tup_frm_modo:
two_helper_edges.append (i)
#Get poly list from two helper edges
border_edg_chk = lx.eval('query layerservice edge.polyList ? %s' %two_helper_edges[0])
border_2_edg_chk = lx.eval('query layerservice edge.polyList ? %s' %two_helper_edges[1])
#-------------------------------------------------------------------------------
#Check to see if we have border edge selected or not
#-------------------------------------------------------------------------------
#Compare the types, if they are not equal one is a border edge
if type(border_edg_chk) != type(border_2_edg_chk):
if type(border_edg_chk) == int:
ply_main = border_edg_chk
if ply_main in border_2_edg_chk:
pass
else:
Error_Close()
if type(border_2_edg_chk) == int:
ply_main = border_2_edg_chk
if ply_main in border_edg_chk:
pass
else:
Error_Close()
#If tuple not a border edge
elif type(border_edg_chk) and type(border_2_edg_chk) == tuple:
edg_ply_lst = lx.eval('query layerservice edge.polyList ? %s' %two_helper_edges[0])
edg_ply_lst_2 = lx.eval('query layerservice edge.polyList ? %s' %two_helper_edges[1])
#Compare polygons ID in ipl to polygons of ipl2
for i in edg_ply_lst:
if i in edg_ply_lst_2:
ply_main = i
#Break here because we found a match and it wont go to the else: below
break
#Cant find shared poly
else:
quit()
#If int it is a border edge
elif type(border_edg_chk) and type(border_2_edg_chk) == int:
edg_ply_lst = lx.eval('query layerservice edge.polyList ? %s' %two_helper_edges[0])
edg_ply_lst_2 = lx.eval('query layerservice edge.polyList ? %s' %two_helper_edges[1])
#Two edges share the same poly
if edg_ply_lst == edg_ply_lst_2:
ply_main = edg_ply_lst
#No shared polygon
elif edg_ply_lst != edg_ply_lst_2:
quit()
#-------------------------------------------------------------------------------
#Now that we know what polygon is shared selct it, and convert it to edges
#-------------------------------------------------------------------------------
#Drop any selected polygons, select ply_main, convert to edges
lx.eval('select.drop polygon')
lx.eval('select.element layer:%s type:polygon mode:add index:%s' %(sel_mesh_ID,ply_main))
lx.eval('select.convert edge')
##---
#-------------------------------------------------------------------------------
#Define the two helper edges
#-------------------------------------------------------------------------------
#Lets query this again becase we converted the polygon to edges and have more now
edg_tup_frm_modo = lx.eval('query layerservice edges ? selected')
#Creat a list of those edges so we can edit it later
edg_list_frm_modo = list(edg_tup_frm_modo)
#Remove these from the list so they wont be used in Edge_Crawl
for i in two_helper_edges:
edg_list_frm_modo.remove(i)
#define the edge we will start the crawl from
long_e_1 = (two_helper_edges[-1])
long_e_2 = (two_helper_edges[0])
#-------------------------------------------------------------------------------
#Extract the start and end verts from helper edges or longest edges
#-------------------------------------------------------------------------------
#Get the two verts that we will start the crawl with
vrt_a, vrt_b = Extract_Vertices(long_e_1)
vrt_sel_order_a.append(vrt_a)
vrt_sel_order_b.append(vrt_b)
#get the two verts that we will stop the crawl with
vrt_a, vrt_b = Extract_Vertices(long_e_2)
end_crawl_vrts.append(vrt_a)
end_crawl_vrts.append(vrt_b)
#-------------------------------------------------------------------------------
#Run every edge through Edge_Crawl
#-------------------------------------------------------------------------------
for i in edg_tup_frm_modo:
#lx.out(edg_list_frm_modo)
crawl_vert_a = vrt_sel_order_a[-1]
if crawl_vert_a not in end_crawl_vrts:
Edge_Crawl(crawl_vert_a, vrt_sel_order_a)
else:
#Now at one of the two end verts so exit loop
break
for i in edg_tup_frm_modo:
#lx.out(edg_list_frm_modo)
crawl_vert_b = vrt_sel_order_b[-1]
if crawl_vert_b not in end_crawl_vrts:
Edge_Crawl(crawl_vert_b, vrt_sel_order_b)
else:
#Now at one of the two end verts so exit loop
break
#Dropping stuff here because we will be selecting vertices below to connect them
lx.eval('select.drop edge')
lx.eval('select.drop vertex')
#-------------------------------------------------------------------------------
#Iterate through both vrt_sel_order lists in parallel and connect verticies
#These are the longest edges of the polygon OR the helper edges that were selected
#-------------------------------------------------------------------------------
#Removing first and last vrt of list because they already have edges between them
vrt_sel_order_a = vrt_sel_order_a[1:-1]
vrt_sel_order_b = vrt_sel_order_b[1:-1]
for v_1, v_2 in zip(vrt_sel_order_a, vrt_sel_order_b):
lx.eval('select.element layer:%s type:vertex mode:add index:%s' % (sel_mesh_ID, v_1))
lx.eval('select.element layer:%s type:vertex mode:add index:%s' % (sel_mesh_ID, v_2))
lx.eval('poly.split')
lx.eval('select.drop vertex')
lx.eval('select.typeFrom edge')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment