Check if something is selected in viewport via python

   225   0   0
User Avatar
Member
9 posts
Joined: 12月 2017
Offline
After a bit of searching this forum I found a method to get the selection from the viewport and it seems to work well. Something like this:

import toolutils

def get_points():
    l = []
    selection = toolutils.sceneViewer().selectGeometry()
    sel = list(selection.selectionStrings())
    for idx, node in enumerate(selection.nodes()):
        x = node.geometry().globPoints(sel[idx])
        l.append([node, x])
    return l

print(get_points())

However, the problem is that if nothing is selected (or I'm not even in "selection mode" in the viewer" this does not return an empty list. It seems like it delays the execution somehow. For example:

1. I select a few points in the viewer and run the code.
>>
[[<hou.SopNode of type grid at /obj/geo1/grid1>, (<hou.Point #0 of geometry in /obj/geo1/grid1 read-only>, <hou.Point #43 of geometry in /obj/geo1/grid1 read-only>)]]

2. Run the code again, now in "object mode", no points selected.
>>

3. Select a point again and run the code.
>>
[[<hou.SopNode of type grid at /obj/geo1/grid1>, (<hou.Point #0 of geometry in /obj/geo1/grid1 read-only>,)]]
[]

You see that the last result is an empty list, it seems like that belongs to step 2. Is there a way around this? If nothing is selected, I want to return an empty list directly.
  • Quick Links