Skip to content

Instantly share code, notes, and snippets.

@lukpazera
Last active December 19, 2015 02:49
Show Gist options
  • Save lukpazera/5885704 to your computer and use it in GitHub Desktop.
Save lukpazera/5885704 to your computer and use it in GitHub Desktop.
This sample implements custom item that draws a circle in viewport. Circle's size and axis can be defined using item's custom channels. You can edit these custom channels in channel list (they won't show up in properties).
# python
""" Custom item drawing a shape.
----------------------------
Implements a custom item that draws a simple shape in viewport.
The item is of Locator supertype.
"""
import lx
import lxifc
ITEMNAME = 'item.customDraw'
CHAN_SIZE = 'ShapeSize'
CHAN_AXIS = 'ShapeAxis'
class CustomItemInstance(lxifc.PackageInstance, lxifc.ViewItem3D):
""" Item instance needs to inherit from PackageInstance.
It also needs to inherit from ViewItem3D if we want to draw in viewport.
"""
def __init__(self):
self.item = lx.object.Item()
def pins_Initialize(self,item,super):
self.item = lx.object.Item(item)
def vitm_Draw(self, chanRead, strokeDraw, selectionFlags):
""" This method draws shapes in viewport.
We're going to draw a simple circle shape with size and axis
that can be set via item's custom channels.
Method receives ChannelRead interface so we have access to scene's channels
and StrokeDraw object that allows for drawing in viewports.
"""
chanRead = lx.object.ChannelRead(chanRead)
""" We are looking up our item's custom channels and getting
their values so we know in which axis and what size of circle to draw. """
axisIx = self.item.ChannelLookup(CHAN_AXIS)
""" Axis will be integer 0,1,2 for x,y,z. """
axis = chanRead.Integer(self.item, axisIx)
shapeSizeIx = self.item.ChannelLookup(CHAN_SIZE)
shapeSize = chanRead.Double(self.item, shapeSizeIx)
strokeDraw = lx.object.StrokeDraw(strokeDraw)
""" Circle vert vector is a location of a vertex
that is on a circle's edge. """
circleVert = [0.0, 0.0, 0.0]
if axis < 3:
circleVert[axis] = shapeSize
else:
circleVert = (shapeSize, 0.0, 0.0)
""" We set StrokeDraw to draw a circle. To draw a circle we need to
draw two "vertices", one will define circle center,
the other will be a point on circle's edge. Location of this point
sets the radius and axis of the circle shape.
We fix color to yellow and opacity to 100%.
These are last 2 arguments to the Begin call. """
strokeDraw.Begin(lx.symbol.iSTROKE_CIRCLES, (1.0, 1.0, 0.0), 1.0)
strokeDraw.Vertex((0.0,0.0,0.0), lx.symbol.iSTROKE_RELATIVE)
strokeDraw.Vertex(circleVert, lx.symbol.iSTROKE_RELATIVE)
class CustomItemPackage(lxifc.Package, lxifc.ChannelUI):
""" This the main item package class.
Inherit from ChannelUI to be able to set hints for channels!
"""
def __init__(self):
pass
def pkg_SetupChannels(self,addChan):
addChan = lx.object.AddChannel(addChan)
addChan.NewChannel(CHAN_SIZE, lx.symbol.sTYPE_FLOAT)
addChan.SetDefault(1.0, 0)
addChan.NewChannel(CHAN_AXIS, lx.symbol.sTYPE_AXIS)
addChan.SetDefault(0.0, 0)
def pkg_Attach(self):
return CustomItemInstance()
def pkg_TestInterface(self, guid):
return (lx.service.GUID().Compare(guid, lx.symbol.u_PACKAGEINSTANCE) == 0) or (lx.service.GUID().Compare(guid, lx.symbol.u_VIEWITEM3D) == 0)
def cui_Enabled(self,channelName,msg,item,chanRead):
""" Return LXe_CMD_DISABLED if disabled,
LXe_OK for enabled and anything else for a failure code.
"""
chanRead = lx.object.ChannelRead(chanRead)
return lx.symbol.e_OK
def cui_UIHints(self, channelName, hints):
""" This is ChannelUI method.
It allows for applying UI hints to item's channels.
The method is called for every custom item's channel
so we need identify channel by name and set hints accordingly.
"""
hints = lx.object.UIHints(hints)
""" We're setting minimum value for the shape size channel to 0.0 """
if channelName == CHAN_SIZE:
hints.MinFloat(0.0)
""" Be sure to set custom item's super type
so it becomes an independent item. """
tags = { lx.symbol.sPKG_SUPERTYPE: lx.symbol.sITYPE_LOCATOR }
lx.bless(CustomItemPackage, ITEMNAME, tags)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment