Skip to content

Instantly share code, notes, and snippets.

@lukpazera
Last active May 23, 2020 09:30
Show Gist options
  • Save lukpazera/ef149ad9f7a91f160fa2b78e0ede09ef to your computer and use it in GitHub Desktop.
Save lukpazera/ef149ad9f7a91f160fa2b78e0ede09ef to your computer and use it in GitHub Desktop.
Gets mesh and and an index of a polygon that is under the mouse in MODO viewport.
# python
import lx
import modo
# This snippet queries polygon that is under a mouse.
# Querying polygon only works correctly on active meshes.
# That means that if mouse is over the mesh that is not active
# (was not selected prior to entering component mode),
# the query returns None.
# To make the query work on any mesh we will do 2 queries.
#
# We will query for the item under the mouse first.
# If this one is None then we know we're not over any polygon either.
#
# Then we query for polygon. If it's None then it means we're not over the active mesh.
# So we force select the mesh we're over to make sure it becomes the active one.
# Then we query for the polygon again.
#
# This time we can be sure that
# it will not return None if we are in fact over a polygon.
# POLY query returns a string in format: mesh_index,polygon_index
# So you need to parse that string to extract index integers.
# It is also possible to get multiple polygons if you have more then one active
# mesh or you're over an edge that belongs to 2 polygons.
# In such case you will get a tuple of strings and polygons are ordered by depth
# (closest one is first).
def polygonUnderMouse():
itemId = lx.eval("query view3dservice element.over ? ITEM")
if itemId is None:
lx.out("Mouse is not over any items.")
return
scene = modo.Scene()
item = scene.item(itemId)
p = lx.eval("query view3dservice element.over ? POLY")
if p is None:
# We're over a mesh but it's not active one so poly query returns None.
scene.select(item)
p = lx.eval("query view3dservice element.over ? POLY")
if p is None:
lx.out("Cannot find polygon under the mouse.")
return
if not isinstance(p, tuple):
p = [p]
pIndex = int(p[0].split(',')[1])
lx.out("Polygon under mouse: %s - %d" % (item.name, pIndex))
polygonUnderMouse()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment