Skip to content

Instantly share code, notes, and snippets.

@ivogrig
Created August 17, 2015 13:31
Show Gist options
  • Save ivogrig/67a458e0e762366a91d0 to your computer and use it in GitHub Desktop.
Save ivogrig/67a458e0e762366a91d0 to your computer and use it in GitHub Desktop.
This command sets or gets the 'Start Control' or 'End Control' state of a curve (not a bezier curve).
'''
This command sets or gets the 'Start Control' or 'End Control' state of a curve (not a bezier curve).
# Example for querying the Start Control state for every curve polygon found in the selected mesh:
import modo
mesh = modo.Mesh()
for curvePolygonIndex in [i.index for i in mesh.geometry.polygons.iterCurves()]:
startControl = lx.eval('curve.endPoints polyIndex:{0} first:?'.format( curvePolygonIndex ))
lx.out(startControl)
'''
import lx
import lxifc
import lxu.command
import modo
class CmdCurveEndPoint(lxu.command.BasicCommand):
'''
'item:item' : (optional) mesh item to change or query from
'polyIndex:integer': (optional) The index of the curve polygon. Will use the selected polygon if not supplied
'first:boolean' : (optional) Gets or sets the 'Start Control' state of the first control point
'last:boolean' : (optional) Gets or sets the 'End Control' state of the last control point
'''
def __init__(self):
lxu.command.BasicCommand.__init__(self)
self.dyna_Add('item', '&item')
self.basic_SetFlags(0, lx.symbol.fCMDARG_OPTIONAL)
self.dyna_Add('polyIndex', lx.symbol.sTYPE_INTEGER)
self.basic_SetFlags(1, lx.symbol.fCMDARG_OPTIONAL)
self.dyna_Add('first', lx.symbol.sTYPE_BOOLEAN)
self.basic_SetFlags(2, lx.symbol.fCMDARG_OPTIONAL | lx.symbol.fCMDARG_QUERY)
self.dyna_Add('last', lx.symbol.sTYPE_BOOLEAN)
self.basic_SetFlags(3, lx.symbol.fCMDARG_OPTIONAL | lx.symbol.fCMDARG_QUERY)
def cmd_Flags(self):
return lx.symbol.fCMD_MODEL | lx.symbol.fCMD_UNDO
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
def basic_Execute(self, msg, flags):
# Get the arguments if supplied, default to None otherwise
itemId = self.dyna_String(0, None)
polyIndex = self.dyna_Int(1, None)
bFirst = self.dyna_Bool(2, None)
bLast = self.dyna_Bool(3, None)
# Call our function with the given arguments
result = setCurveEndPoints(itemId, polyIndex, bFirst, bLast)
if result == None:
return lx.result.FAILED
def cmd_Query(self, index, vaQuery):
# Same as in the Execute method ...
itemId = self.dyna_String(0, None)
polyIndex = self.dyna_Int(1, None)
bFirst = self.dyna_Bool(2, None)
bLast = self.dyna_Bool(3, None)
result = setCurveEndPoints(itemId, polyIndex, bFirst, bLast)
if not result:
return lx.result.FAILED
# Boilerplate for returning values ...
va = lx.object.ValueArray()
va.set(vaQuery)
bFirst, bLast = result
if index == 2:
va.AddInt( int(bFirst) )
elif index == 3:
va.AddInt( int(bLast) )
return lx.result.OK
lx.bless(CmdCurveEndPoint, "curve.endPoints")
def setCurveEndPoints(itemId=None, polyIndex=None, bFirst=None, bLast=None):
# Get the mesh
curve = modo.Mesh() if not itemId else modo.Mesh(itemId)
# Get the polygon
if polyIndex is None:
# The selected polygon
try:
polygon, = curve.geometry.polygons.selected
except ValueError:
modo.dialogs.alert('Error', dtype='error', message='No curve polygon selected')
return None
else:
# Or if an index was passed, the indexed polygon
polygon = curve.geometry.polygons[polyIndex]
# Test if it is a curve
if not lxu.utils.decodeID4(polygon.accessor.Type()) == 'CURV':
return
# Only do change if one of the arguments is set
if any((bFirst, bLast)):
if bFirst is not None:
polygon.accessor.SetFirstIsControlEndpoint(bFirst)
if bLast is not None:
polygon.accessor.SetLastIsControlEndpoint(bLast)
# Update the mesh to see the change
curve.geometry.setMeshEdits()
# Return the state of first and last as tuple
startSet = polygon.accessor.FirstIsControlEndpoint()
endSet = polygon.accessor.LastIsControlEndpoint()
return startSet, endSet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment