galagast Posted December 17 Share Posted December 17 (edited) 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 December 17 by galagast added tag Quote Link to comment Share on other sites More sharing options...
Alain2131 Posted Tuesday at 10:18 PM Share Posted Tuesday at 10:18 PM Try exit() Quote Link to comment Share on other sites More sharing options...
galagast Posted Wednesday at 05:46 AM Author Share Posted Wednesday at 05:46 AM 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 python_return.hiplc Quote Link to comment Share on other sites More sharing options...
Alain2131 Posted Saturday at 01:06 AM Share Posted Saturday at 01:06 AM 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 ! Quote Link to comment Share on other sites More sharing options...
galagast Posted 2 hours ago Author Share Posted 2 hours ago that Python Null trick is quite neat, thanks for sharing! for the meantime, i'll just go with using def Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.