TomDym

From The Foundry MODO SDK wiki
Revision as of 11:15, 24 October 2019 by TomDym (Talk | contribs) (Listeners)

Jump to: navigation, search

DoWhileUserIsIdle

//If you try to execute a script while holding down a modifier like ctrl or shift, it will postpone execution until you release the modifier
#python
import lx, lxifc
 
class visitor(lxifc.Visitor):                  
    def __init__(self):     # The initial setup method 
        pass
 
    def vis_Evaluate(self):                           
        lx.out("Visitor")
pSrv = lx.service.Platform()
vis = visitor()
com_visitor = lx.object.Unknown(vis)
pSrv.DoWhenUserIsIdle(com_visitor, lx.symbol.fUSERIDLE_MODIFIER_KEYS_UP)

//Too cancel it you need to pass exactly the same com object and flags
pSrv.CancelDoWhenUserIsIdle(com_visitor, lx.symbol.fUSERIDLE_MODIFIER_KEYS_UP )

Toggle Command

lx.eval('tool.set falloff.linear ?+')

Path of Selected Preset Item

#!python
ppaths = lxu.select.PresetPathSelection().current()
print ppaths

View3dService Example

import modo, lx

viewSvc = lx.service.View3Dport()
currentView = lx.object.View3D(viewSvc.View(viewSvc.Current()))

for i in range(viewSvc.Count()):
    view = lx.object.View3D(viewSvc.View(i))

    print view.Matrix(0)
    print view.Angles()
    print view.Axis()
    print view.EyeVector()
    lx.out(view.WorkPlane())
    a = view.To3D(150.0,150.0,2000)
    a = view.Center()
    a = view.WorkPlane()

Listeners

Navigation Listener

#python
import lx
import lxifc
import modo

class ListenerTwo(lxifc.NavigationListener):
	def __init__(self):
			self.listenerService = lx.service.Listener()
			self.listenerService.AddListener(self)
			self.item = modo.Mesh()
			self.view = lx.object.View
			lx.out('hey')

	def nav_Wheel(self,view, item):
		lx.out('Worked')
		
	def nav_Up(self, view, item):
		#is Called when move is started
		lx.out('nav_up')
		
	def nav_Move(self, view, item, hot, pos, rot, zoom):
		pass
		#is called every "move" while held down
		#lx.out('nav_move')
		
	def nav_Delta(self, view, item, hot, pos, rot, zoom):
		lx.out('nav_Delta')
		
	def nav_Down(self, view, item):
		#is Called when move is ended
		lx.out('nav_down')
		
	def nav_HotSyncPre(self, view, item):
		lx.out('nav_HotSyncPre')

	def nav_HotSyncPost(self, view, item):
		lx.out('nav_HotSyncPost')

ListenerTwo() #instance a Listener to initialize it

Mesh Listener

#python
import lx
import lxifc
import modo

geo  = modo.MeshGeometry("Mesh").internalMesh
port = lx.object.ListenerPort(geo)

class mesh_listener(lxifc.MeshListener):
	undoService = lx.service.Undo()
	def __init__(self):
		self.listenerService = lx.service.Listener()	
		self.COM_object = lx.object.Unknown(port)
		
	def ml_Changes(self, event):
		print "change"
		
l = mesh_listener()
port.AddListener(l)

Command Listener


#python
import lx
import lxifc
import modo

svc_listen = lx.service.Listener()

class CmdListener(lxifc.CmdSysListener):
	def __init__(self):
			svc_listen = lx.service.Listener()
			svc_listen.AddListener(self)
			self.armed = True
		
	def cmdsysevent_UserRedo(self):	
		#pass
		#is called everytime a redo is called
		lx.out('cmdsysevent_UserRedo')
		
	def cmdsysevent_UserUndo(self):
		#pass
		#is called everytime a undo is called
		lx.eval('log.masterClear')
		lx.out('cmdsysevent_UserUndo')
		
	def cmdsysevent_ExecutePre(self,cmd,type,isSandboxed,isPostCmd):
		if self.armed:
			cmd = lx.object.Command(cmd)
            # lx.out("'%s' will fire shortly" % cmd.Name())
			if cmd.Name() == "app.quit":
				lx.eval('replay.fileClose')

	def cmdsysevent_ExecutePost(self,cmd,isSandboxed,isPostCmd):
		if self.armed:
			cmd = lx.object.Command(cmd)
			lx.out("'%s' has finished" % cmd.Name())
		lx.out('cmdsysevent_ExecutePost')
		pass

	def cmdsysevent_RefireBegin(self):
        # we don't want a bunch of events when the user is
        # dragging a minislider or something like that,
        # so we disarm the listener on RefireBegin...
		self.armed = False

cmdListener1 = CmdListener()

Scene Item Listener

  1. python

import lx import lxifc import modo

class ListenerTwo(lxifc.SceneItemListener): def __init__(self): self.listenerService = lx.service.Listener() self.COM_object = lx.object.Unknown(self) self.listenerService.AddListener(self.COM_object)

def sil_ItemAdd(self, item): lx.out('sil_ItemAdd')

def __del__(self): self.listenerService.RemoveListener(self.COM_object)


list = ListenerTwo() #instance a Listener to initialize it </pre>

Selection Listener

#python
import lx
import lxifc
import modo

class ListenerTwo(lxifc.SelectionListener):
	def __init__(self):
			self.listenerService = lx.service.Listener()
			self.COM_object = lx.object.Unknown(self)
			self.listenerService.AddListener(self.COM_object)
			self.type = modo.Item.type
		
	def selevent_Add(self, type, subtType):
		lx.eval('log.MasterClear')
		lx.out('selevent_Add')
		pass
		
	def selevent_Current(self, type):
		lx.eval('log.MasterClear')
		lx.out('selevent_Current')
		pass
   
	def selevent_Remove(self, type, subtType):
		lx.eval('log.MasterClear')
		lx.out('selevent_Remove')
		pass
   
	def selevent_Time(self, time):
		lx.out(time)
		pass
   
	def selevent_TimeRange(self, type):
		pass


list = ListenerTwo() #instance a Listener to initialize it

Command System Listener

#python
import lx
import lxifc
import modo

svc_listen = lx.service.Listener()

class CmdListener(lxifc.CmdSysListener):
	def __init__(self):
			svc_listen = lx.service.Listener()
			svc_listen.AddListener(self)
			self.armed = True
		
	def cmdsysevent_UserRedo(self):	
		#pass
		#is called everytime a redo is called
		lx.out('cmdsysevent_UserRedo')
		
	def cmdsysevent_UserUndo(self):
		#pass
		#is called everytime a undo is called
		lx.eval('log.masterClear')
		lx.out('cmdsysevent_UserUndo')
		
	def cmdsysevent_ExecutePre(self,cmd,type,isSandboxed,isPostCmd):
		if self.armed:
			cmd = lx.object.Command(cmd)
            # lx.out("'%s' will fire shortly" % cmd.Name())
			if cmd.Name() == "app.quit":
				lx.eval('replay.fileClose')

	def cmdsysevent_ExecutePost(self,cmd,isSandboxed,isPostCmd):
		if self.armed:
			cmd = lx.object.Command(cmd)
			lx.out("'%s' has finished" % cmd.Name())
		lx.out('cmdsysevent_ExecutePost')
		pass

	def cmdsysevent_RefireBegin(self):
        # we don't want a bunch of events when the user is
        # dragging a minislider or something like that,
        # so we disarm the listener on RefireBegin...
		self.armed = False

cmdListener1 = CmdListener()

User Value Listener


#python
import lx
import lxifc
import modo

class ListenerTwo(lxifc.UserValueListener?):
	def __init__(self):
			self.listenerService = lx.service.Listener()
			self.COM_object = lx.object.Unknown(self)
			self.listenerService.AddListener(self.COM_object)
			self.userValue = lx.eval('user.value someValue ?')
		
	def uvl_Added(self, userValue):
		lx.out('uvl_Added')
		pass
  
	def uvl_DefChanged(self, userValue):
		lx.out('uvl_DefChanged')
		pass
 
	def uvl_Deleted(self, name):
		lx.out('uvl_Deleted')
		pass
  
	def uvl_ValueChanged(self, userValue):
		lx.out('uvl_ValueChanged')
		pass



list = ListenerTwo() #instance a Listener to initialize it

Session Listener

# python
import lx, lxifc
svc_listen = lx.service.Listener()
class sesListener(lxifc.SessionListener):
    def __init__(self):
        svc_listen = lx.service.Listener()
        svc_listen.AddListener(self)
    def sesl_SystemReady(self):
        lx.out('Session Listener: system was ready')
sesListener = sesListener()

StartCommands Config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <atom type="StartupCommands">
        <list type="Command">sl</list>
    </atom>
 
</configuration>

Polygon Itersect Ray


import modo, lx,lxu

mesh = modo.Mesh()

for poly in mesh.geometry.polygons:
	polygon = lx.object.Polygon(poly)
	
	print polygon.Index()
	
	pos = modo.Vector3(0,0, 0.0, 0.0)
	dir = modo.Vector3(0.0, 1.0, 0.0)
	
	bool_test, hit_nrm, hit_dist = polygon.IntersectRay(pos, dir)
	
	print bool_test, hit_dist

Undo Command

import lx
import lxu

class myUndoCommand(lxu.command.BasicCommand):
    
    def basic_Execute(self, msg, flags):
        
        undo_svc = lx.service.Undo()
        if undo_svc.State() != lx.symbol.iUNDO_INVALID:
            undo_svc.Apply(myUndo(someData))

			mesh = modo.Mesh()
			with cube.geometry as geo:
				for v in geo.vertices:
					v += (0, 0.5, 0)
					
			cube.geometry.setMeshEdits()

    def cmd_Flags(self):
        return lx.symbol.fCMD_UNDO

class myUndo(lxifc.Undo):
    def __init__(self, data):
        self.data = data

    def undo_Forward(self):
        # do stuff for running command and redo
        pass
    
    def undo_Reverse(self):
        # do stuff here for undo
        pass

lx.bless(myUndoCommand, "undoCommand")