Welcome to od|forum

Register now to gain access to all of our features. Once registered and logged in, you will be able to contribute to this site by submitting your own content or replying to existing content. You'll be able to customize your profile, receive reputation points as a reward for submitting content, while also communicating with other members via your own private inbox, plus much more! This message will be removed once you have signed in.

defc0n1

Calling a function in a python Sop from an HDA button callback

Hey guys

I have an HDA with 2 python SOPs inside. I need a button on the HDA that would trigger a function in one of the python SOPs. I looked at how callback scripts work but it was too confusing. Appreciate it if someone could help me out. Thanks!

Cheers

defc0n1

Share this post


Link to post
Share on other sites

Python SOPs shouldn't contain code which intended to be called outside, Houdini cooks SOPs when it's needed. Define your functions in HDA's PythonModule and call them from there, even from within Python SOPs inside, if you wish so.

Edited by Stalkerx777

Share this post


Link to post
Share on other sites
7 minutes ago, Stalkerx777 said:

Python SOPs shouldn't contain code which intended to be called outside, Houdini cooks SOPs when it's needed. Define your functions in HDA's PythonModule and call them from there, even from within Python SOPs inside, if you wish so.

Right. I'll need to fix that. Could you explain that last part - '...even from within Python SOPs inside, if you wish so.'?

Share this post


Link to post
Share on other sites
8 minutes ago, Stalkerx777 said:

Python SOPs shouldn't contain code which intended to be called outside, Houdini cooks SOPs when it's needed. Define your functions in HDA's PythonModule and call them from there, even from within Python SOPs inside, if you wish so.

Oh wait does it mean I can call a function in an internal Python SOP from within the python module in the HDA?

Share this post


Link to post
Share on other sites
24 minutes ago, defc0n1 said:

Oh wait does it mean I can call a function in an internal Python SOP from within the python module in the HDA?

Exactly. This is, in general, a good practice to define all your shared Python code in a top level HDA, and use it anywhere you need, like that:

module = hou.nodeType("Sop/MyHDA").hdaModule()
module.my_func()

 

Edited by Stalkerx777
1 person likes this

Share this post


Link to post
Share on other sites
8 minutes ago, Stalkerx777 said:

Exactly. This is, in general, a good practice to define all your shared Python code in a top level HDA, and use it anywhere you need, like that:


module = hou.nodeType("Sop/MyHDA").hdaModule()
module.my_func()

 

Thank you!!!

Share this post


Link to post
Share on other sites

He Alex..sorry I hit a wall again. My internal python node adds detail attributes so when I define the same code within the HDA python module, it says that the geometry is read-only. I read somewhere that you can't modify geometry from within an HDA's python module?

My function examines a binary file and adds some attributes based on the parameters it finds in the file. Problem is, the file is HUGE and I don't want it to cook every time I change the file to be analysed.

Share this post


Link to post
Share on other sites

Just pass you geometry as an argument to your function.

In HDA:

def modify_geo(geo):
  geo.addAttribute(...)
  return geo
  

In Python SOP:

module = hou.nodeType("Sop/MyHDA").hdaModule()
geo = module.modify_geo(geo)

 

Share this post


Link to post
Share on other sites
On 24/07/2017 at 3:12 AM, Stalkerx777 said:

Just pass you geometry as an argument to your function.

In HDA:


def modify_geo(geo):
  geo.addAttribute(...)
  return geo
  

In Python SOP:


module = hou.nodeType("Sop/MyHDA").hdaModule()
geo = module.modify_geo(geo)

 

Right....how do I call this from using callback script? Do I just do hou.pwd().hdaModule().modify_geo(geo)?

Share this post


Link to post
Share on other sites
6 minutes ago, defc0n1 said:

Right....how do I call this from using callback script? Do I just do hou.pwd().hdaModule().modify_geo(geo)?

Never mind. Stupid question.

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