Skip to content

Instantly share code, notes, and snippets.

@lukpazera
Created May 21, 2013 13:59
Show Gist options
  • Save lukpazera/5619989 to your computer and use it in GitHub Desktop.
Save lukpazera/5619989 to your computer and use it in GitHub Desktop.
Implements a simple item.listTags command that lists all string tags attached to the selected item.
#python
"""
List all Item's Tags
--------------------
Simple command that lists all tags attached to the selected item.
The output goes to Event Log.
"""
import lx
import lxu.command
import lxu.select
import traceback
import struct
CMD_NAME = 'item.listTags'
class CmdListTags(lxu.command.BasicCommand):
def __init__(self):
lxu.command.BasicCommand.__init__(self)
# Init services that will be needed
self.sceneSrv = lx.service.Scene()
self.selectSrv = lx.service.Selection()
def basic_Enable(self, msg):
"""
The command will be enabled if at least 1 item is selected
"""
if self.selectSrv.Count(lx.symbol.iSEL_ITEM) >= 1:
return True
else:
return False
def cmd_Flags(self):
"""
Command doesn't affect scene state so no need to set any flags
"""
return 0
def basic_Execute(self, msg, flags):
"""
Main command method
"""
try:
lx.out('--- item.listTags Log ---')
# double check if items are selected
if self.selectSrv.Count(lx.symbol.iSEL_ITEM) == 0:
return
# grab item selection
# localize the first selected item to Item() object
itemSel = lxu.select.ItemSelection()
item = lx.object.Item(itemSel.current()[0])
# StringTag interface is used to access tags
# so we're localizing the String Tag object for the first selected item
tags = lx.object.StringTag(item)
# pull number of tags attached to the first selected item
tagN = tags.Count()
# output information about number of attached tags
# if no tags found - return
if not tagN:
lx.out('%s item does not have any tags attached.' % item.UniqueName())
return
lx.out('%s item has %d tags attached:' % (item.UniqueName(), tagN))
# go through tags and output their names and values
for x in xrange(tagN):
# tagData will be a two element tuple
# the first element is tag name, the second one - a string value
tagData = tags.ByIndex(x)
# tag name is stored as 4 bytes long integer internally
# each byte represents one character so in the UI tags have 4-letter long names
# we need to convert the integer returned from the tags interface into 4 character string
# struct module does that
tagName = struct.pack('>I', tagData[0])
# at this point tagName holds a string equivalent of the 4-byte integer
# and we can output tag data
lx.out('%s. %s : %s' % (str(x + 1).zfill(2), tagName, tagData[1]))
except:
lx.out(traceback.format_exc())
return
#------------------------------
lx.bless(CmdListTags, CMD_NAME)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment