Jump to content
DougStruthers

FORCE A RETURN OUT OF A PYTHON NODE

Recommended Posts

As Python frowns on goto lines, I am looking for a way to break out of a python script.  

return does not appear to work though I found an old 2013 suggestion.

Share this post


Link to post
Share on other sites

There are many different ways to do it in Python and HOM. You can wrap into a function and use returns in the body. Or raise descriptive errors and treat them differently. HOM has all sorts of custom errors too.

This one will cook a node without errors. Depending on the input, it may show a warning and stop the script from executing further:

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

if not pts:
    raise hou.NodeWarning('Input geometry has no points.')

geo.addAttrib(hou.attribType.Global, 'number_of_points', len(pts))

 

Edited by f1480187
  • Like 1

Share this post


Link to post
Share on other sites

the easy solution is to wrap the entire node of the sop in a function, like f1 already suggested

def myfunction():
	print "print this"
	return
	print "but not this"

myfunction() #make sure you actually run your code at the bottom of course

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×