Skip to content

Instantly share code, notes, and snippets.

@mattcox

mattcox/printMeshTags.py

Last active Oct 4, 2018
Embed
What would you like to do?
This sample code demonstrates how to enumerate over all polygons on the active mesh layer, and print the material tag assigned to each polygon to the event log.
#python
'''
This sample code demonstrates how to enumerate over all polygons on the active
mesh layer, and print the material tag assigned to each polygon to the event
log.
'''
import lx
import lxu.command
class Command(lxu.command.BasicCommand):
def __init__(self):
lxu.command.BasicCommand.__init__(self)
def cmd_Execute(self, msg):
'''
The Layer Service allows us to read active, primary or
background layers. In this case, we want the primary layer,
which is the most recently selected mesh item.
'''
layerService = lx.service.Layer()
layerScan = layerService.ScanAllocate(lx.symbol.f_LAYERSCAN_PRIMARY)
'''
The number of layers should be 1 or 0, but we'll loop over
them all, allowing the code to easily be modified to work on
multiple active layers.
'''
layerCount = layerScan.Count()
for i in range(layerCount):
'''
The Base Mesh is the uneditable mesh in the base of
procedural stack. Similar functions exist for getting
an editable mesh, or the final result from the
procedural system.
'''
mesh = layerScan.MeshBase(i)
polygonCount = mesh.PolygonCount()
if polygonCount > 0:
'''
Polygons are accessed using an accessor. When
polygons are "selected", the accessor is
modified to point at the "selected" polygon.
Note: The polygon is not selected by the
selection system, but just made active.
The PolygonAccessor also provides a StringTag
interface which can be used for querying tags.
'''
polygonAccessor = mesh.PolygonAccessor()
stringTag = lx.object.StringTag(polygonAccessor)
for j in range(polygonCount):
polygonAccessor.SelectByIndex(j)
materialTag = stringTag.Get(lx.symbol.i_PTAG_MATR)
lx.out("Polygon: %d - Material: %s" % (j, materialTag))
lx.bless(Command, "mesh.printTags")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment