Last active
June 22, 2017 18:07
-
-
Save mattcox/5677300 to your computer and use it in GitHub Desktop.
Utility functions for manipulating groups inside of Modo.
This file contains 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 | |
''' | |
Class and functions for working with groups inside of modo. | |
''' | |
import lx | |
class Group: | |
def __init__ (self, group): | |
''' | |
Initialize the class by passing it a group object. | |
''' | |
self.group = lx.object.Item(group) | |
self.group_type = lx.service.Scene().ItemTypeLookup(lx.symbol.sITYPE_GROUP) | |
self.scene = self.group.Context() | |
self.group_graph = lx.object.ItemGraph(self.scene.GraphLookup(lx.symbol.sGRAPH_ITEMGROUPS)) | |
def Add (self, items): | |
''' | |
This adds items to a group, if the item isn't already in the group. | |
''' | |
if isinstance(items, list) == False: | |
items = [items] | |
if self.group.test() == False or self.group_graph.test() == False: | |
return False | |
if self.group.TestType(self.group_type) == False: | |
return False | |
for item in items: | |
if item.test() == False: | |
continue | |
connection_found = False | |
rev_count = self.group_graph.RevCount(item) | |
for connection in range(rev_count): | |
group_item = self.group_graph.RevByIndex(item, connection) | |
if group_item.Ident() == self.group.Ident(): | |
connection_found = True | |
break | |
if connection_found == False: | |
self.group_graph.AddLink (self.group, item) | |
return True | |
def Remove (self, items): | |
''' | |
This removes items from a group, if the item is in the group. | |
''' | |
if isinstance(items, list) == False: | |
items = [items] | |
if self.group.test() == False or self.group_graph.test() == False: | |
return False | |
if self.group.TestType(self.group_type) == False: | |
return False | |
for item in items: | |
if item.test() == False: | |
continue | |
connection_found = False | |
rev_count = self.group_graph.RevCount(item) | |
for connection in range(rev_count): | |
group_item = self.group_graph.RevByIndex(item, connection) | |
if group_item.Ident() == self.group.Ident(): | |
connection_found = True | |
break | |
if connection_found == True: | |
self.group_graph.DeleteLink (self.group, item) | |
return True | |
def QueryContents (self, item): | |
''' | |
This checks if an item is in a group and returns True or False. | |
''' | |
if self.group.test() == False or self.group_graph.test() == False or item.test() == False: | |
return False | |
if self.group.TestType(self.group_type) == False: | |
return False | |
connection_found = False | |
rev_count = self.group_graph.RevCount(item) | |
for connection in range(rev_count): | |
group_item = self.group_graph.RevByIndex(item, connection) | |
if group_item.Ident() == self.group.Ident(): | |
connection_found = True | |
break | |
return connection_found | |
def GetContents (self, index=None): | |
''' | |
This gets the contents of a group and returns the items. If index is | |
set, it will return the item at the specified index. If index is not | |
set, it will return a list of all items in the group. | |
''' | |
items = [] | |
if self.group.test() == False or self.group_graph.test() == False: | |
return None | |
if self.group.TestType(self.group_type) == False: | |
return None | |
fwd_count = self.group_graph.FwdCount(self.group) | |
if index == None: | |
for connection in range(fwd_count): | |
group_item = self.group_graph.FwdByIndex(self.group, connection) | |
items.append(group_item) | |
return items | |
else: | |
if index > fwd_count or index < 0: | |
return None | |
else: | |
return self.group_graph.FwdByIndex(self.group, index) | |
def Count (self): | |
''' | |
This function gets the total number of items in a group. | |
''' | |
if self.group.test() == False or self.group_graph.test() == False: | |
return 0 | |
if self.group.TestType(self.group_type) == False: | |
return 0 | |
return self.group_graph.FwdCount(self.group) | |
def GetIndex (self, item): | |
''' | |
This function takes an item and returns its position in the group. | |
If the item isn't in the group, it returns -1. | |
''' | |
if self.group.test() == False or self.group_graph.test() == False or item.test() == False: | |
return -1 | |
if self.group.TestType(self.group_type) == False: | |
return -1 | |
connection_found = -1 | |
fwd_count = self.group_graph.FwdCount(self.group) | |
for connection in range(fwd_count): | |
group_item = self.group_graph.FwdByIndex(self.group, connection) | |
if group_item.Ident() == item.Ident(): | |
connection_found = connection | |
break | |
return connection_found | |
def SetIndex (self, item, index): | |
''' | |
This function takes an item and an index. It sets the position of the | |
item in the group to match the index. | |
''' | |
if self.group.test() == False or self.group_graph.test() == False or item.test() == False: | |
return False | |
if self.group.TestType(self.group_type) == False: | |
return False | |
if index < -1: | |
index = -1 | |
connection_set = False | |
fwd_count = self.group_graph.FwdCount(self.group) | |
for connection in range(fwd_count): | |
group_item = self.group_graph.FwdByIndex(self.group, connection) | |
if group_item.Ident() == item.Ident(): | |
self.group_graph.SetLink(self.group, index, item, -1) | |
connection_set = True | |
break | |
return connection_set |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment