Skip to content

Instantly share code, notes, and snippets.

@rockjail
Created January 4, 2018 08:49
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 rockjail/b83396872463990fdf4e01a2f1c6f83d to your computer and use it in GitHub Desktop.
Save rockjail/b83396872463990fdf4e01a2f1c6f83d to your computer and use it in GitHub Desktop.
Manual implementation of a MeshOperation in python for Modo. This is an example showing how to read from a CurveGroup on items connected on a custom graph.
#python
import lx
import lxu
import lxifc
sSERVER_NAME = "mopCurveGroup"
sGRAPH_NAME = sSERVER_NAME + ".graph"
sGRAPH_USERNAME = sSERVER_NAME + ".graphUserName"
sCHAN_COUNT = "count"
class Instance(lxifc.PackageInstance):
pass
class Package(lxifc.Package, lxifc.ChannelUI):
def pkg_SetupChannels(self, addChan):
addChan = lx.object.AddChannel(addChan)
addChan.NewChannel (sCHAN_COUNT, lx.symbol.sTYPE_INTEGER);
addChan.SetDefault (0.0, 0);
def pkg_TestInterface(self, guid):
return (lx.service.GUID().Compare(guid, lx.symbol.u_PACKAGEINSTANCE) == 0)
def pkg_Attach(self):
return Instance()
class MeshOp(lxifc.MeshOperation):
def __init__(self):
self.item = lx.object.Mesh()
self.count = 0
self.curveGroup = None
def mop_Evaluate(self, mesh, type, mode):
if self.curveGroup is None:
return
mesh = lx.object.Mesh(mesh)
if mesh.test() == False:
return
point = lx.object.Point(mesh.PointAccessor())
if point.test() == False:
return
for index in range(self.curveGroup.Count()):
curve = self.curveGroup.ByIndex(index)
for i in range(self.count):
curve.SetLenFraction(float(i)/(self.count-1))
pos = curve.Position()
point.New(pos)
mesh.SetMeshEdits(lx.symbol.f_MESHEDIT_GEOMETRY)
class Modifier(lxifc.Modifier):
def __init__(self, item, eval):
self.attr = lx.object.Attributes(eval)
item = lx.object.Item(item)
self.crvIdent = None
self.hasCurve = False
scene = item.Context()
if item.test() == False or scene.test() == False:
return
self.index = eval.AddChannelName(item, lx.symbol.sICHAN_MESHOP_OBJ, lx.symbol.fECHAN_WRITE)
self.curveGraph = lx.object.ItemGraph(scene.GraphLookup(sGRAPH_NAME))
if self.curveGraph.RevCount(item) > 0:
crvItem = self.curveGraph.RevByIndex(item, 0)
self.hasCurve = True
self.crvIdent = crvItem.Ident()
self.curveGroupIndex = eval.AddChannelName(crvItem, lx.symbol.sICHAN_MESH_CURVE_GROUP, lx.symbol.fECHAN_READ)
self.countIndex = eval.AddChannelName(item, sCHAN_COUNT, lx.symbol.fECHAN_READ)
def mod_Test(self, item, index):
item = lx.object.Item(item)
scene = item.Context()
if item.test() == False or scene.test() == False:
return False
graph_revCount = self.curveGraph.RevCount(item)
if graph_revCount > 0:
crvItem = self.curveGraph.RevByIndex(item, 0)
testIdent = crvItem.Ident()
return self.crvIdent == testIdent
def mod_Evaluate(self):
meshOp = MeshOp()
val_ref = lx.object.ValueReference(self.attr.Value(self.index, 1))
val_ref.SetObject(meshOp)
if self.hasCurve:
val_ref = lx.object.ValueReference(self.attr.Value(self.curveGroupIndex,0))
meshOp.curveGroup = lx.object.CurveGroup(val_ref.GetObject())
meshOp.count = self.attr.GetInt(self.countIndex)
class EvalModifier(lxifc.EvalModifier):
def __init__(self):
scn_svc = lx.service.Scene()
self.itemType = scn_svc.ItemTypeLookup(sSERVER_NAME)
def eval_Reset(self, scene):
self.scene = lx.object.Scene(scene)
self.index = 0
self.count = self.scene.ItemCount(self.itemType)
def eval_Next(self):
if self.index >= self.count:
return (0,0)
item = self.scene.ItemByIndex(self.itemType, self.index)
self.index += 1
return (item,0)
def eval_Alloc(self, item, index, eval):
return Modifier(item, lx.object.Evaluation(eval))
class Schematic(lxifc.SchematicConnection, lxifc.SchemaDest):
def __init__(self):
self.scn_svc = lx.service.Scene()
def schm_ItemFlags(self, item):
item = lx.object.Item(item)
if(item.Type() == self.scn_svc.ItemTypeLookup(sSERVER_NAME)):
return lx.symbol.fSCON_SINGLE
else:
return 0
def schm_AllowConnect(self, from_obj, to_obj):
item_from = lx.object.Item(from_obj)
try:
return item_from.ChannelLookup(lx.symbol.sICHAN_MESH_CURVE_GROUP) >= 0
except:
return False
def schm_GraphName(self):
return sGRAPH_NAME
tags = { lx.symbol.sSRV_USERNAME: sGRAPH_USERNAME }
lx.bless(Schematic, sGRAPH_NAME, tags)
tags = { lx.symbol.sMOD_TYPELIST: sSERVER_NAME, lx.symbol.sMOD_GRAPHLIST: sGRAPH_NAME }
lx.bless(EvalModifier, sSERVER_NAME, tags)
tags = { lx.symbol.sPKG_SUPERTYPE: lx.symbol.sITYPE_MESHOP, lx.symbol.sPKG_GRAPHS: sGRAPH_NAME }
lx.bless(Package, sSERVER_NAME, tags)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment