Jump to content

Python beginner - executing from external python script


gweiss

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

  

Link to comment
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
Link to comment
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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...