Jump to content

Selections in viewport


salik

Recommended Posts

Hi all, I have written a function that grabs the selection from within the viewport.

While it works if I select one or more objects in the viewport, but it does not seems to be working if nothing is selected.
I have placed a 'check' to prompt an error should there be no selections, but it does not seems to be working and instead, if there are no selections, as I execute my code, it will prompt me to select some geometries.

def selection():
    scene_viewer = toolutils.sceneViewer()
    selected_objects = list(scene_viewer.selectObjects(
        prompt = toolutils.selectionPrompt(hou.objNodeTypeCategory()),
            allow_multisel = True,
            allowed_types = ("geo")
            )
        )
    # if nothing was selected.
    if len(selected_objects) == 0:
        raise hou.Error("Nothing was selected.")
    return selected_objects

Is it suppose to be like this or am I writing it wrongly somewhere...

Link to comment
Share on other sites

1 hour ago, f1480187 said:

allowed_types=("geo",)

Here. Missing comma.

It does not seems to make any difference.. Added that comma in and I am getting the same result where it prompts me to 'select objects. press enter to accept selections'..

Update: Happened to hit Enter by chance and the error will be raised (selection is still empty) Are there any ways in which I can bypass the prompt that is asking me to select objects..

Edited by salik
Link to comment
Share on other sites

  1. If something already selected, return it immediately.
  2. If nothing is selected, ask for selection and wait for Enter.
  3. If nothing was selected, raise hou.Error.

That's how this code works for me. I believe 1 and 2 is how selectObjects tool intended to work. Do you want to to skip the second step? There is hou.selectedNodes() function returning a list of nodes currently selected. It's a bit different thing, but it may work for your case, at least by checking for the type of it's contents. Maybe there is better built-in ways, but I didn't find it inside toolutils and objecttoolutils modules.

  • Like 1
Link to comment
Share on other sites

24 minutes ago, f1480187 said:
  1. If something already selected, return it immediately.
  2. If nothing is selected, ask for selection and wait for Enter.
  3. If nothing was selected, raise hou.Error.

That's how this code works for me. I believe 1 and 2 is how selectObjects tool intended to work. Do you want to to skip the second step? There is hou.selectedNodes() function returning a list of nodes currently selected. It's a bit different thing, but it may work for your case, at least by checking for the type of it's contents. Maybe there is better built-in ways, but I didn't find it inside toolutils and objecttoolutils modules.

Ahhh, ok.. Yes I am planning to skip Step 2 if possible. As I am more of a Maya User and just beginning to code for Houdini, I am thinking more in-line with Maya instead..

In that case, I can try out hou.selectedNodes() and see how it goes..

I have a question though, about the missing comma.. Does it really matters? I asked, cause it seems to work whether or not there is a comma..

Link to comment
Share on other sites

Comma denotes tuple in Python. Without comma it is just a "something inside parentheses". If you implement such a function, you will use type checks and handle strings and tuples in a different manner. The function expecting a tuple. It seems, it does not handle single strings and fails silently. I cannot select anything (step 2 is not working). So, it matters both in general and in the case of the tool.

 

Edited by f1480187
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...