Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
This Command will smooth the transition between two conected curves.
"""
import lx
import lxu.command
import lxifc
import lxu.select
def smoothCurveEnds():
# get selected mesh
layer_scan = lx.service.Layer().ScanAllocate(lx.symbol.f_LAYERSCAN_EDIT)
if layer_scan.test() == False:
return None
# check that only one mesh is selected
if layer_scan.Count() != 1:
return None
mesh_loc = layer_scan.MeshEdit(0)
if mesh_loc.test() == False:
return None
poly_loc = lx.object.Polygon(mesh_loc.PolygonAccessor())
if poly_loc.test() == False:
return None
# get selection
sel_svc = lx.service.Selection()
sel_type = sel_svc.LookupType(lx.symbol.sSELTYP_POLYGON)
poly_pkt_trans = lx.object.PolygonPacketTranslation(
sel_svc.Allocate(lx.symbol.sSELTYP_POLYGON))
n_polys = sel_svc.Count(sel_type)
if n_polys != 2:
return None
crvs = []
for i in range(n_polys):
p_pkt = sel_svc.ByIndex(sel_type, i)
polyID = poly_pkt_trans.Polygon(p_pkt)
poly_loc.Select(polyID)
vrts = poly_loc.VertexCount()
crvVrts = [poly_loc.VertexByIndex(x) for x in range(vrts)]
crvs.append([polyID, crvVrts])
inds = []
# check how they are connected
if crvs[0][1][0] == crvs[1][1][0]:
crvs[0].append(False)
crvs[0].append(crvs[1][1][1])
crvs[1].append(False)
crvs[1].append(crvs[0][1][1])
elif crvs[0][1][0] == crvs[1][1][-1]:
crvs[0].append(False)
crvs[0].append(crvs[1][1][-2])
crvs[1].append(True)
crvs[1].append(crvs[0][1][1])
elif crvs[0][1][-1] == crvs[1][1][0]:
crvs[0].append(True)
crvs[0].append(crvs[1][1][1])
crvs[1].append(False)
crvs[1].append(crvs[0][1][-2])
elif crvs[0][1][-1] == crvs[1][1][-1]:
crvs[0].append(True)
crvs[0].append(crvs[1][1][-2])
crvs[1].append(True)
crvs[1].append(crvs[0][1][-2])
else:
return
# create empty storge object
points_storage = lx.object.storage()
points_storage.setType('p')
for c in crvs:
poly_loc.Select(c[0])
# change curve vert list and mark connected end
if c[2]:
poly_loc.SetLastIsControlEndpoint(0)
poly_loc.SetLastIsControlEndpoint(1)
c[1].append(c[3])
else:
poly_loc.SetFirstIsControlEndpoint(0)
poly_loc.SetFirstIsControlEndpoint(1)
c[1].insert(0,c[3])
# set storage object
points_storage.setSize(len(c[1]))
points_storage.set(c[1])
# set new vert list for curve
poly_loc.SetVertexList(points_storage, len(c[1]), 0)
# apply mesh changes
mesh_loc.SetMeshEdits(lx.symbol.f_MESHEDIT_POLYGONS)
layer_scan.Apply()
return True
class CMDcurvessmooth(lxu.command.BasicCommand):
def __init__(self):
lxu.command.BasicCommand.__init__(self)
self.dyna_Add("argumentName", lx.symbol.sTYPE_STRING)
self.basic_SetFlags(0, lx.symbol.fCMDARG_OPTIONAL)
def cmd_Flags(self):
return lx.symbol.fCMD_MODEL | lx.symbol.fCMD_UNDO
def cmd_Query(self, index, vaQuery):
lx.notimpl()
def basic_Execute(self, msg, flags):
result = smoothCurveEnds()
if not result:
return lx.result.FAILED
def basic_Enable(self, msg):
'''
Enabled if a single mesh item is selected
'''
items = lxu.select.ItemSelection().current()
if len(items) != 1:
return False
if not items[0].TestType(lx.symbol.i_CIT_MESH):
return False
return True
def cmd_Interact(self):
pass
lx.bless(CMDcurvessmooth, "curves.smooth")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.