Jump to content

[Python SOP] C# "return" equivalent?


galagast

Recommended Posts

In C#, it's possible to write code where you can use "return;" to stop exit the code processing in a method. Even if there are more lines after it.

Is there an equivalent we can use inside the Python SOP (hopefully without wrapping our code in a "def")?

The idea is to maybe be able to write simple lines of code in a Python SOP that could check for some stuff line by line, but can stop anytime if a line says so.

Edited by galagast
added tag
Link to comment
Share on other sites

thanks!
i tried it before, unfortunately it does not work.
i also tried stop(), break(), continue(), return()

as an example, here's something that i wish worked:

node = hou.pwd()
geo = node.geometry()

foo = geo.attribValue("foo")

if (foo == 1):
    print("start")

if (foo == 2):
    return()
    
if (foo == 3):
    print("end")


this is the version that works, where it is encapsulated inside the def:

node = hou.pwd()
geo = node.geometry()

foo = geo.attribValue("foo")

def test():
    if (foo == 1):
        print("start")

    if (foo == 2):
        return()
    
    if (foo == 3):
        print("end")
        
test()


I'm not against using def, but i'm just wondering if there was a method i could use to do a "return" within the Python SOP itself.
Sort of like considering the Python SOP as its own def itself :D

python_return.hiplc

Link to comment
Share on other sites

Oh, sorry that's my bad, I had tested it through the Python Null (a trick I found here) I often use when testing things out, and turns out that it accepts exit() without complaining, while a normal python node does not. Today I learned !

Adapting the python null trick wouldn't be viable, as (with a normal python node) it would require wrapping the code in a string then calling exec(code_string), adding even more boiler plate fuss than a def.
You could probably make a python null that behaves like a normal python node to cook when the input or when the node changes (and since it'd use exec(), it would accept exit()), but that again goes against the point where the request is for a one-liner "please do not compute the rest of this code".

You're right, I don't see a way to do this, and I'd also like to have a solution !

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...