Skip to content

Instantly share code, notes, and snippets.

@tcrowson
Last active August 29, 2015 13:59
Show Gist options
  • Save tcrowson/10649635 to your computer and use it in GitHub Desktop.
Save tcrowson/10649635 to your computer and use it in GitHub Desktop.
Function that returns all items in the scene of the specified type(s) as a list of Python objects.
# python
'''
This function searches the current Modo scene and returns all items of
the specified type as a list of Python objects.
The function takes one or more arguments as strings or integers, which are the types of items you're after.
To ensure forward compatibility we'll use the symbol for the item type we need.
To see the full list of item type symbols, visit the following URL:
http://sdk.luxology.com/wiki/Symbols_defined_in_the_lx_package
(Scroll down to the entries beginning with "sITYPE_" to find the type you want)
Usage Example:
allMeshes = getItemsOfType( lx.symbol.sITYPE_MESH )
allLights = getItemsOfType( lx.symbol.sITYPE_LIGHT )
allCameras = getItemsOfType( lx.symbol.sITYPE_CAMERA )
allCamerasAndLights = getItemsOfType( lx.symbol.sITYPE_CAMERA, lx.symbol.sITYPE_LIGHT )
'''
import lx
import lxu.select
def getItemsOfType(*item_types):
'''
Returns all items of the specified type(s) as a list of Modo Python objects.
Returns an empty list if no such items are found.
'''
items = []
sceneService = lx.service.Scene()
scene = lxu.select.SceneSelection().current()
for t in item_types:
if isinstance(t, str):
try:
typeLookup = sceneService.ItemTypeLookup(t)
except LookupError:
raise LookupError("Item Type '%s' not recognized" %t)
elif isinstance(t, int):
typeLookup = t
else:
continue
itemCount = scene.ItemCount(typeLookup)
items.extend((scene.ItemByIndex(typeLookup, n) for n in xrange(itemCount)))
return items
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment