Skip to content

Instantly share code, notes, and snippets.

@rondreas
Created November 29, 2019 15:43
Show Gist options
  • Save rondreas/a085126f06cc6065894ed14f02996c7d to your computer and use it in GitHub Desktop.
Save rondreas/a085126f06cc6065894ed14f02996c7d to your computer and use it in GitHub Desktop.
Heads up display showing percentage of the UV area that current selected meshes/faces covers.
"""
Custom Heads up display, displaying the area current selection covers in UV space.
found this `gist <https://gist.github.com/SEVEZ/d86bf0a4a497794a94df8f125846ca4f>`_ from SEVEZ really useful.
"""
__author__ = "Andreas Ranman"
import maya.cmds as cmds
import maya.api.OpenMaya as om # maya python api 2.0
def uv_coverage(*args):
""" Calculate the polygon area in UV space for selected meshes and polygon components
:returns: formatted string with percent of coverage
:rtype: str
"""
area = 0.0
selection = om.MGlobal.getActiveSelectionList()
# Iterate selection, but only filter on polygon components,
iterSelection = om.MItSelectionList(selection)
while not iterSelection.isDone():
dag, obj = iterSelection.getComponent()
if not obj.isNull():
if obj.hasFn(om.MFn.kMeshPolygonComponent):
iterPoly = om.MItMeshPolygon(dag, obj)
while not iterPoly.isDone():
area += iterPoly.getUVArea()
iterPoly.next(None) # Known bug on Autodesks side, since 2016 at least.
iterSelection.next()
# Iterate over all meshes in selection,
iterMesh = om.MItSelectionList(selection, om.MFn.kMesh)
while( not iterMesh.isDone() ):
dag, obj = iterMesh.getComponent()
iterPoly = om.MItMeshPolygon(dag)
while(not iterPoly.isDone()):
area += iterPoly.getUVArea()
iterPoly.next(None) # Known bug on Autodesks side, since 2016 at least.
iterMesh.next()
return "{} %".format(area * 100.0) # return the area as a percentage formatted string.
def toggle_hud():
""" Toggle the HUD. """
hud_name = "UVCoverage"
# Using a check if it exists to work as a toggle, first run will create a hud, second will remove it.
if not cmds.headsUpDisplay(hud_name, exists=True, query=True):
cmds.headsUpDisplay(
hud_name,
section=1,
block=0,
blockSize='medium',
label='UV Coverage',
labelFontSize='large',
command=uv_coverage,
event='SelectionChanged',
nodeChanges='attributeChange'
)
else:
cmds.headsUpDisplay( hud_name, rem=True ) # remove any previously created hud from this script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment