getting return value from hou.frame()

   6263   2   1
User Avatar
Member
2164 posts
Joined: Sept. 2015
Offline
Hello,

So I've went through the video tutorial on Python and thought I would try a few things to get a working feel for it.

I made a simple function in the python source editor which is called new().

I dropped that function as new() in a Transformation node parameter edit box to see if the return value of the function will get placed as a value parameter.

I get an error message though saying that global name hou not defined.

I tried just using frame() instead of hou.frame() but then it says frame() is not defined.

But if I run hou.frame() in python shell it does return whatever the current frame number is.

So I am not sure how to “define” frame() in the source editor in order that it can “see” it.

I do have expression language set for python, and integer frame values unchecked in the global animation options window.

This is the function code in case the screenshot makes it hard to see:

def new():
if hou.frame() == 1:
a = 3
else:
a = 2
return a

any feedback is appreciated, thanks.

Attachments:
hou_frame.jpg (110.2 KB)

User Avatar
Member
9380 posts
Joined: July 2007
Offline
while it should not be necessary to import hou module, it happens sometimes that Source Editor does not recognize it
so to be on the safer side do:
import hou
def new():
if hou.frame() == 1:
a = 3
else:
a = 2
return a
or to shorten the code
import hou
def new():
return 3 if hou.frame() == 1 else 2
Tomas Slancik
CG Supervisor
Framestore, NY
User Avatar
Member
2164 posts
Joined: Sept. 2015
Offline
thanks tamte,

It works now without error but I'm not sure what I did that actually got it to work.

The reason I say this is because at first the ‘import hou’ didn't make a change in that I was still getting the error message.

So a couple times I deleted my transform node, saved the file, and closed/reopened Houdini and the file. But it still was getting the error message and not working.

So I tried changing the code just to play around:

import hou

b = hou.frame()

def new():
if b == 1:
a = 5
else:
a = 7
return a

I understand that this code would only give me an initial result because b is only getting set once and not each time new() is getting called. But in this case the error message went away and got the parameter set to 5 as expected.

So I tried the original code of:

import hou
def new():
if hou.frame() == 1:
a = 3
else:
a = 2
return a

…and this worked too. Not only that but I tried removing ‘import hou’, and it still works. I even deleted the source editor contents, accept, reload, close…and deleted the node, and saved/closed Houdini/File and restarted/opened file and typed in the code again without import hou; And it still works.

I think this is a good experience to have early on in learning Houdini because I have plans make some complex functions in the future and if I have problems running my code it shows that it may not always be my syntax or applied logic.

Thanks for your time and the suggestion.
  • Quick Links