Skip to content

Instantly share code, notes, and snippets.

@lukpazera
Created May 21, 2013 13:10
Show Gist options
  • Save lukpazera/5619653 to your computer and use it in GitHub Desktop.
Save lukpazera/5619653 to your computer and use it in GitHub Desktop.
Plugin implements item.listXfrm command that lists all selected item's transform items in Event Log.
#python
'''
List Item Transforms
--------------------
Plugin implements item.listXfrm command that lists
all selected item's transforms in Event Log
'''
import lx
import lxu.command
import lxu.select
import traceback
CMD_NAME = 'item.listXfrm'
class CmdListXfrm(lxu.command.BasicCommand):
def __init__(self):
lxu.command.BasicCommand.__init__(self)
self.sceneSrv = lx.service.Scene()
self.selectionSrv = lx.service.Selection()
# We need to look up integer transform item types using Scene service.
# We'll use these integers to recognize items.
self.translationType = self.sceneSrv.ItemTypeLookup(lx.symbol.sITYPE_TRANSLATION)
self.rotationType = self.sceneSrv.ItemTypeLookup(lx.symbol.sITYPE_ROTATION)
self.scaleType = self.sceneSrv.ItemTypeLookup(lx.symbol.sITYPE_SCALE)
def basic_Enable(self, msg):
'''
The command will be enabled if at least 1 item is selected
'''
if self.selectionSrv.Count(lx.symbol.iSEL_ITEM) >= 1:
return True
else:
return False
def cmd_Flags(self):
'''
The command doesn't do anything to scene so no need to set any flags
'''
return 0
def basic_Execute(self, msg, flags):
'''
Main command method
'''
try:
lx.out('--- item.listXFrm Log ---')
# get the number of currently selected items
sel_count = self.selectionSrv.Count(lx.symbol.iSEL_ITEM)
# exit if no items selected
if sel_count == 0:
msg.SetCode(lx.symbol.e_FAILED)
return
# create an item selection object
itemSel = lxu.select.ItemSelection()
# get first selected item
# we're going to process first selected item only for simplicity
item = lx.object.Item(itemSel.current()[0])
# test the item, just to make sure it's ok
if not item.test():
msg.SetCode(lx.symbol.e_FAILED)
return
# get scene object based on the selected item
scene = lx.object.Scene(item.Context())
# transform items are in xfrmCore graph
# so first we need to get the xfrmCore item graph from scene
# the command will fail if no such graph exists
try:
xfrmGraph = lx.object.ItemGraph(scene.GraphLookup(lx.symbol.sGRAPH_XFRMCORE))
except:
msg.SetCode(lx.symbol.e_FAILED)
return
# get the number of reverse links for the selected item
# this will be a number of transform items added to the selected item
revN = xfrmGraph.RevCount(item)
if not revN:
lx.out('This Item doesn\'t have any Transform Items added.')
return
# Print some info out
lx.out('%s has %d transform items.' % (item.UniqueName(), revN))
lx.out(' ')
# We are going to walk the xfrm xfrmGraph now
# and output basic info about every item
# we're walking backwards to reflect the order that is in chanenl list
# (first evaluated transform item is at the bottom)
for x in xrange(revN - 1, -1, -1):
# get the transform item by index
xfrmItem = lx.object.Item(xfrmGraph.RevByIndex(item, x))
# get the integer item type
xfrmType = xfrmItem.Type()
# we're going to recognize the type of the transform item using looked up item types
# and display that info accordingly
# if the type is not recognized we're just going to output 'transform'
typeString = 'transform'
if xfrmType == self.translationType:
typeString = 'position'
elif xfrmType == self.rotationType:
typeString = 'rotation'
elif xfrmType == self.scaleType:
typeString = 'scale'
# get the info out
lx.out('%s. %s: %s' % (str(x + 1).zfill(2), typeString, xfrmItem.UniqueName()))
lx.out(' ')
except:
lx.out(traceback.format_exc())
return
#------------------------------
lx.bless(CmdListXfrm, CMD_NAME)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment