Python Samples

From The Foundry MODO SDK wiki
Jump to: navigation, search

This is the text of the samples shown in the Python API Feature of the Week video.

Service Objects

>>> dir(lx.service)
: AudioAnim
: ChannelUI
: Command
: Deformer
: File
: GUID
: Host
: IO
: Image
: ImageMonitor
: ImageProcessing
: Layer
: Listener
: Log
: Mesh
: Message
: NotifySys
: Packet
: Persistence
: Platform
: PresetBrowser
: Render
: Scene
: ScriptSys
: Selection
: Shader
: StdDialog
: Tableau
: Thread
: Undo
: Value
: __doc__
: __name__
: __package__
>>> ims = lx.service.Image()
>>> dir(ims)
: Create
: CreateCrop
: Duplicate
: Kelvin2RGB
: Load
: RGB2Kelvin
: Resample
: Save
: ScriptQuery
: __class__
: __delattr__
: __doc__
: __format__
: __getattribute__
: __hash__
: __init__
: __new__
: __reduce__
: __reduce_ex__
: __repr__
: __setattr__
: __sizeof__
: __str__
: __subclasshook__
>>> ims.Kelvin2RGB.__doc__
: vector rgbColor = Kelvin2RGB(float kelvin)
>>> ims.Kelvin2RGB(273)
: 1.0
: 0.0
: 0.246930614114
>>> ims.RGB2Kelvin( (0,0,1) )
: 39999.390625
>>> ims.ScriptQuery()
* ERROR: Unhandled exception "exceptions.NotImplementedError"; not implemented
*  .. <string>: 1
- error 80000001: Not Implemented

Imported Objects

>>> ims.Load.__doc__
: Image object = Load(string filePath)
>>> image = ims.Load('C:\lux\content\maps\mandril.jpg')
>>> dir(image)
: Format
: GetLine
: GetPixel
: Size
: __class__
: __delattr__
: __doc__
: __format__
: __getattribute__
: __hash__
: __init__
: __new__
: __reduce__
: __reduce_ex__
: __repr__
: __setattr__
: __sizeof__
: __str__
: __subclasshook__
: set
: test
>>> image.Size()
: 512
: 512
>>> image.Format()
: 3
>>> lx.symbol.iIMP_RGB24
: 3
>>> if image.Format() == lx.symbol.iIMP_RGB24: lx.out("RGB24")
* RGB24

Storage Buffer Objects

>>> image.GetPixel.__doc__
: GetPixel(integer x,integer y,integer type,data[] pixel)
>>> pixel = lx.object.storage()
>>> pixel.setType('f')
>>> pixel.setSize(4)
>>> image.GetPixel(128,128,lx.symbol.iIMP_RGBAFP,pixel)
>>> pixel.get()
: 0.376470625401
: 0.419607877731
: 0.352941185236
: 1.0

Polymorphism

>>> iout = lx.object.ImageWrite(image)
>>> dir(iout)
: AddAttribute
: Format
: SetLine
: SetPixel
: Size
: __class__
: __delattr__
: __doc__
: __format__
: __getattribute__
: __hash__
: __init__
: __new__
: __reduce__
: __reduce_ex__
: __repr__
: __setattr__
: __sizeof__
: __str__
: __subclasshook__
: set
: test
>>> iout.SetPixel.__doc__
: SetPixel(integer x,integer y,integer type,data[] pixel)
>>> iout.SetPixel(0,0,lx.symbol.iIMP_RGBAFP,pixel)
>>> attr = lx.object.Attributes()
>>> attr.test()
: False
>>> attr.Count()
* ERROR: Unhandled exception "exceptions.TypeError"; no interface
*  .. <string>: 1
- error 80000004: No COM Interface
>>> attr.set(image)
: True
>>> attr.Count()
: 0

Utilities

>>> import lxu.select
>>> itmsel = lxu.select.ItemSelection()
>>> itmsel.current()
: <lx.object.Item object at 0x00000000083CC180>
>>> mesh = itmsel.current()[0]
>>> dir(mesh)
: ChannelAdd
: ChannelCount
: ChannelEvalType
: ChannelGradient
: ChannelLookup
: ChannelName
: ChannelPackage
: ChannelStorageType
: ChannelType
: ChannelVectorMode
: Context
: GetTag
: Ident
: InvalidateName
: IsReferenced
: Name
: PackageAdd
: PackageRemove
: PackageStartIndex
: PackageTest
: Parent
: Reference
: Root
: SetIdent
: SetName
: SetParent
: SetSource
: SetTag
: SetUniqueIndex
: Source
: SubByIndex
: SubCount
: TestType
: TestTypes
: Type
: UniqueIndex
: UniqueName
: WasLoaded
: __class__
: __delattr__
: __doc__
: __format__
: __getattribute__
: __hash__
: __init__
: __new__
: __reduce__
: __reduce_ex__
: __repr__
: __setattr__
: __sizeof__
: __str__
: __subclasshook__
: set
: test
>>> mesh.UniqueName()
: Mesh
>>> scene = mesh.Context()

Exported Objects

>>> lx.eval('script.run "macro.scriptservice:19601440010:macro"')
>>> scene.Channels.__doc__
: ChannelRead object = Channels(string name,float time)
>>> chan = scene.Channels(None,0.0)
>>> dir(chan)
: BakedSamples
: Double
: Envelope
: Integer
: IsAnimated
: IsBaked
: String
: Time
: ValueObj
: __class__
: __delattr__
: __doc__
: __format__
: __getattribute__
: __hash__
: __init__
: __new__
: __reduce__
: __reduce_ex__
: __repr__
: __setattr__
: __sizeof__
: __str__
: __subclasshook__
: set
: test
>>> chan.ValueObj.__doc__
: Unknown object = ValueObj(object item,integer channel)
>>> i = mesh.ChannelLookup(lx.symbol.sICHAN_MESH_MESH)
>>> i
: 34
>>> mc = chan.ValueObj(mesh,i)
>>> dir(mc)
: __class__
: __delattr__
: __doc__
: __format__
: __getattribute__
: __hash__
: __init__
: __new__
: __reduce__
: __reduce_ex__
: __repr__
: __setattr__
: __sizeof__
: __str__
: __subclasshook__
: set
: test
>>> lx.object.Mesh(mc)
* ERROR: Unhandled exception "exceptions.TypeError"; no interface
*  .. <string>: 1
- error 80000004: No COM Interface
>>> mf = lx.object.MeshFilter(mc)
>>> dir(mf)
: Evaluate
: Generate
: Type
: __class__
: __delattr__
: __doc__
: __format__
: __getattribute__
: __hash__
: __init__
: __new__
: __reduce__
: __reduce_ex__
: __repr__
: __setattr__
: __sizeof__
: __str__
: __subclasshook__
: set
: test
>>> mf.Generate.__doc__
: Mesh object = Generate()
>>> ball = mf.Generate()

>>> import tricount
>>> tricount.Display(ball)
* found 48 triangles and 240 non-triangles

tricount Module Source

import lx
import lxifc

class TriangleCountVisitor(lxifc.Visitor):
    def __init__(self,poly):
        self.poly = poly
        self.n_tri = 0
        self.n_not = 0

    def vis_Evaluate(self):
        if self.poly.VertexCount() == 3:
            self.n_tri += 1
        else:
            self.n_not += 1

def Display(mesh):
    poly = mesh.PolygonAccessor()
    vis = TriangleCountVisitor(poly)

    poly.Enumerate(lx.symbol.iMARK_ANY, vis, 0)

    lx.out("found {0} triangles and {1} non-triangles".format(vis.n_tri, vis.n_not))

Dummy Image Saver Source

import lx
import lxifc

def LogOut(str):
    ls = lx.service.Log()
    log = ls.SubSystemLookup('scripts')
    log.AddEntry(ls.CreateEntryMessage(0, str))

def WasteTime(n,monitor):
    mon = lx.object.Monitor(monitor)
    mon.Initialize(n)
    for i in range(n):
        mon.Increment(1)

class SizeSaver(lxifc.Saver):
    def sav_Save(self,image,filename,monitor):
        img = lx.object.Image(image)
        w,h = img.Size()
        LogOut('saving ... ' + str (w * h))
        WasteTime(w * h, monitor)

tags = {
    lx.symbol.sSRV_USERNAME: "Size Saver",
    lx.symbol.sSAV_OUTCLASS:  lx.symbol.a_IMAGE,
    lx.symbol.sSAV_DOSTYPE : "PSS"
}
lx.bless(SizeSaver, "pySizeSaver", tags)

Hello World Test

>>> lx.eval('hello.someone')
* Hello to no one!
>>> lx.eval('hello.someone BetaTesters')
* Hello to BetaTesters!

Hello World Command Source

import lx
import lxifc
import lxu.command

class HelloCmdArg(lxu.command.BasicCommand):
    def __init__(self):
        lxu.command.BasicCommand.__init__(self)
        self.dyna_Add('name', lx.symbol.sTYPE_STRING)
        self.basic_SetFlags(0, lx.symbol.fCMDARG_OPTIONAL)

    def basic_Execute(self, msg, flags):
        s = self.dyna_String(0, "no one")
        lx.out("Hello to " + s + "!")

lx.bless(HelloCmdArg, "hello.someone")