Jump to content
gweiss

Python beginner - executing from external python script

Recommended Posts

Hi,

learning python, I'm trying to do a simple test of creating a custom shelf tool that calls a function from a file to create a sphere
I copy the create sphere shelf button script:

import toolutils
import soptoolutils
rad = 0.5
kwargs['bbox'] = hou.BoundingBox(-rad, -rad, -rad, rad, rad, rad)
sphere = soptoolutils.genericTool(kwargs, 'sphere')
sphere.parm("type").set("polymesh")
sphere.parm("radx").set(rad)
sphere.parm("rady").set(rad)
sphere.parm("radz").set(rad)

I save a .py file with a new function and paste the script with a small change of - rad = 1
so I have a saved file called spherebig.py that looks like that

import hou
import toolutils
import soptoolutils

def createBigSphere():
    rad = 1
    kwargs['bbox'] = hou.BoundingBox(-rad, -rad, -rad, rad, rad, rad)
    sphere = soptoolutils.genericTool(kwargs, 'sphere')
    sphere.parm("type").set("polymesh")
    sphere.parm("radx").set(rad)
    sphere.parm("rady").set(rad)
    sphere.parm("radz").set(rad)


in my new shelf tool I enter this on the script tab:

import spherebig
spherebig.createBigSphere()

and when I press the button I get this error:

NameError: global name 'kwargs' is not defined

am I doing something wrong or is there a different way to create objects this way?

Thanks
gweiss

  

Share this post


Link to post
Share on other sites

Well, you have not passed in kwargs. I don't know this for certain, but when a button script is run by Houdini it is probably passing that in, behind the scenes. (i.e. tool shelf code is running inside of some sandbox that has been given kwargs). When you externalize that process you now have to become the provider of kwargs. Maybe read up a bit on python kwargs?

  • Like 1

Share this post


Link to post
Share on other sites

Add an argument to the function. It can be any name, use kwargs as convention:

def createBigSphere(kwargs):
    pass

Pass the kwargs when call it from shelf.

import spherebig
spherebig.createBigSphere(kwargs)

kwargs is a dictionary populated with useful contextual stuff and available in most places where you use Python in Houdini. Contents are varying, so I usually print kwargs to see what's available to use. Shelf script kwargs contain details about how the user activated the script.

 

I think Houdini kwargs is different from common Python **kwargs syntax and idiom, but they still share properties.

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

×