Jump to content

temporary python session global variable


raytheon

Recommended Posts

hi all..

I'm trying to toggle the value of an HDA parameter in the pre- and post-render scripts of a ROP Output Driver. To avoid complications i would like to store the parameter value at the start of the pre-script then restore the value at the end of the post- script. I'm using the python source editor window, which is wonderful to work with save for the fact that i haven't been able to figure out how to create/modify/read a global variable from within a function defined in the editor. My current code is:

import hou

curr_type = 1

def test_global(invoke_type_string):
    global curr_type

    if(invoke_type_string == "pre"):
        curr_type = 99
    else:
        curr_type = 66
    hou.ui.displayMessage("hello world") # this displays ok
    #hou.ui.displayMessage(curr_type)    # this does not display when enabled

It appears to work fine other than access to curr_type. There is a hint at http://www.sidefx.com/docs/houdini10.0/hom/expressions about importing main. The editor considers this an error in Houdini 10 on win64 (maybe other platforms too). I think they may mean importing __main__. I tried this also but no luck.. probably something simple i'm doing wrong :rolleyes: .

thanks!

Edited by raytheon
Link to comment
Share on other sites

Your code highlights an unfortunate problem when using Python through the different scripts in ROPs. If something goes wrong you don't notice it. The commented out line of your code is not working because you are attempting to pass an Integer where it is expecting a String.

If you try running it, nothing will happen, then you can enter the following in the Python shell, and see that it actually fails.

>>> hou.ui.displayMessage(hou.session.curr_type)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/prisms/builder-new/NightlyHEAD/dev/hfs/houdini/python2.6libs/hou.py", line 23217, in displayMessage
    return _hou.ui_displayMessage(*args, **kwargs)
TypeError: in method 'ui_displayMessage', argument 2 of type 'char const *'

Converting it to string before passing to displayMessage() will work as you want.

Importing __main__ is only really useful if you need access to something from the global namespace. In this case, the curr_type variable is a local variable to the hou.session module, and thus is not available through importing the global namespace. Here's an example of using that.

Say I open the Python shell in Houdini and type

blah = 5

I now want to use blah to drive a TY parameter on an object. If I set it to Python and just type in blah it will error since parameters don't have access to the global namespace. In this case I will need to import __main__ and use it to get access to blah.

import __main__
return __main__.blah

Edited by graham
Link to comment
Share on other sites

Thanks! That clears it up.

Placing an exception handler around the code catches the error too:

import hou
import sys
import logging

curr_type = 1

def test_global(invoke_type_string):
    global curr_type
    try:
        if(invoke_type_string == "pre"):
            curr_type = 99
        else:
            curr_type = 66
        hou.ui.displayMessage(curr_type)          # this throws an exception.. bad idea
    except Exception, err:
        msg = "***EXCEPTION: "+str(err)
        hou.ui.displayMessage(msg)
        logging.getLogger().exception(msg)        # good idea in Houdini?
        sys.stderr.write(msg)                     # good idea in Houdini?
        return

which displays "***EXCEPTION: in method 'ui_displayMessage', argument 2 of type 'char const *'" and logs the exception details to the python shell. The shell doesn't open automatically when the exception is logged, though.

Out of idle curiosity, is there a good example of Houdini Python error handling and/or logging somewhere? I notice 3D Buzz has a video.. has anyone found it helpful for this?

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...