Last active
December 17, 2015 01:49
-
-
Save lukpazera/5531664 to your computer and use it in GitHub Desktop.
A simple modo Python API command that implements group.stats command.
The command enumerates all selected groups and returns a number of items and channels in every group.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#python | |
""" | |
Group Statistics Command | |
------------------------ | |
A simple command that enumerates all selected groups | |
and returns a number of items and channels in every group | |
Author: Lukasz Pazera | |
""" | |
import lx | |
import lxifc | |
import lxu.command | |
import lxu.select | |
class GroupVisitor(lxifc.Visitor): | |
""" | |
Visitor is used to enumerate group members | |
Information about currently processed group member can be obtained | |
via the grpEnum GroupEnumerator object that is passed to the __init__ method | |
""" | |
def __init__(self, grpEnum): | |
# GroupEnumerator object | |
self.grpEnum = grpEnum | |
# variables for storing number of items and channels in a group | |
self.itemN = 0 | |
self.chanN = 0 | |
def vis_Evaluate(self): | |
""" | |
vis_Evaluate method is called in one of following ways: | |
- for every group member (both items and channels) | |
- for every item member | |
- for every channel member | |
The choice between these three options is made during GroupEnumerator.Enumerate() method call | |
(see the GroupsStatsCommand code below) | |
In this case the evaluate method will be called for every member. | |
We determine the type of currently enumerated member using GroupEnumerator.Type() method | |
and we increase itemN or chanN variables accordingly. | |
NOTE: group members such as actor's poses for example count as items too | |
""" | |
# test for the type of member that is currently being enumerated | |
if self.grpEnum.Type() == lx.symbol.fGRPTYPE_ITEM: | |
self.itemN += 1 | |
elif self.grpEnum.Type() == lx.symbol.fGRPTYPE_CHANNEL: | |
self.chanN += 1 | |
class GroupStatsCommand(lxu.command.BasicCommand): | |
def __init__(self): | |
lxu.command.BasicCommand.__init__(self) | |
def cmd_Flags(self): | |
return 0 | |
def basic_Execute(self, msg, flags): | |
""" | |
This is the main method that will be executed when the command is fired in modo | |
""" | |
# grab item selection | |
currentSelection = lxu.select.ItemSelection() | |
# initialize scene service object | |
# we need that object to look up the integer type number of the group item | |
scn_svc = lx.service.Scene() | |
group_type = scn_svc.ItemTypeLookup(lx.symbol.sITYPE_GROUP) | |
# we'll count number of selected groups just for fun | |
groupsCount = 0 | |
# we're going to cycle through the item selection | |
# and enumerate all found group items | |
for x in xrange(len(currentSelection.current())): | |
# localize indexed selected item | |
item_loc = lx.object.Item(currentSelection.current()[x]) | |
# test the item against being of group type | |
# we are going to use the group_type integer that we previously obtained from scene service | |
if not item_loc.TestType(group_type): | |
continue | |
# a group is found! | |
groupsCount += 1 | |
# get group item name | |
grpName = item_loc.UniqueName() | |
# cast the Item object to GroupItem so we can access GroupItem specific methods | |
groupItem = lx.object.GroupItem(item_loc) | |
# get the GroupEnumerator object for the group. | |
# The enumerator object will be used to iterate through group members and count our stats | |
groupEnum = lx.object.GroupEnumerator(groupItem.Enumerator()) | |
# initialize our visitor object and pass GroupEnumerator object to it | |
grpVisitor = GroupVisitor(groupEnum) | |
# call GroupEnumerator.Enumerate() method to enumerate the group | |
# pass our visitor as first argument | |
# pass a symbol that'll determine the way in which the group will be enumerated as second argument: | |
# lx.symbol.fGRPTYPE_BOTH - visitor's vis_Evaluate method will be called for every group member | |
# lx.symbol.fGRPTYPE_ITEM - only item members will be enumerated | |
# lx.symbol.fGRPTYPE_CHANNEL - only channel members will be enumerated | |
groupEnum.Enumerate(grpVisitor, lx.symbol.fGRPTYPE_BOTH) | |
# at this point enumeration is completed, visitor's vis_Evaluate() method was called for every group member | |
# the visitor should have a complete information on number of items and channels in a group | |
# so we can output this data | |
lx.out('%s Group has: %d items, %d channels' % (grpName, grpVisitor.itemN, grpVisitor.chanN)) | |
# output number of selected groups | |
lx.out('Groups selected: %d' % groupsCount) | |
""" | |
"Blessing" the class promotes it to a fist class server. This basically | |
means that modo will now recognize this plugin script as a command plugin. | |
""" | |
lx.bless(GroupStatsCommand, "group.stats") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment