-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathEventSelection_showFacesCount.py
More file actions
85 lines (69 loc) · 2.81 KB
/
EventSelection_showFacesCount.py
File metadata and controls
85 lines (69 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from pxr import Usd, UsdGeom, UsdPhysics, UsdShade, Sdf, Gf, Tf
import omni.ui
import omni.kit.app
# Get context.
context = omni.usd.get_context()
# Get stage.
stage = context.get_stage()
# Get main window viewport.
window = omni.ui.Window("Viewport")
# ---------------------------------------------.
# Get the number of faces in the mesh.
# ---------------------------------------------.
def GetFacesCount(prim):
if not prim.IsValid():
return 0
allCou = 0
if prim.IsA(UsdGeom.Mesh):
m = UsdGeom.Mesh(prim)
# If it is displayed.
if m.ComputeVisibility() == UsdGeom.Tokens.inherited:
# Get the number of faces of Mesh.
allCou += len(m.GetFaceVertexCountsAttr().Get())
# Recursively traverse the hierarchy.
pChildren = prim.GetChildren()
for cPrim in pChildren:
allCou += GetFacesCount(cPrim)
return allCou
# ---------------------------------------------.
# Update Viewport UI.
# Show the number of faces of the selected shape in the Viewport.
# ---------------------------------------------.
def UpdateViewportUI(paths):
if len(paths) == 0:
with window.frame:
with omni.ui.VStack(height=0):
with omni.ui.Placer(offset_x=20, offset_y=0):
omni.ui.Spacer(width=0, height=8)
return
with window.frame:
with omni.ui.VStack(height=0):
with omni.ui.Placer(offset_x=20, offset_y=50):
f = omni.ui.Label("--- Selection Shapes ---")
f.visible = True
f.set_style({"color": 0xff00ffff, "font_size": 32})
with omni.ui.Placer(offset_x=20, offset_y=0):
omni.ui.Spacer(width=0, height=8)
# Show selection shape name.
for path in paths:
prim = stage.GetPrimAtPath(path)
if prim.IsValid():
facesCou = GetFacesCount(prim)
with omni.ui.Placer(offset_x=28, offset_y=0):
f2 = omni.ui.Label(f'[ {prim.GetName()} ] faces {facesCou}')
f2.visible = True
f2.set_style({"color": 0xff00ff00, "font_size": 32})
# ---------------------------------------------.
# Selected event.
# ---------------------------------------------.
def onStageEvent(evt):
if evt.type == int(omni.usd.StageEventType.SELECTION_CHANGED):
# Get selection paths.
selection = omni.usd.get_context().get_selection()
paths = selection.get_selected_prim_paths()
# Show selected shapes info.
UpdateViewportUI(paths)
# ------------------------------------------------.
# Register for stage events.
# Specify "subs=None" to end the event.
subs = context.get_stage_event_stream().create_subscription_to_pop(onStageEvent, name="sampleStageEvent")