Here is a simple script in my OTL Operator Type Properties:
=====================================
import inspect
def runFunction():
print inspect.getmodule(runFunction)
runFunction()
=====================================
when I run this scripts inside Operator Type Properties, it returns “None”.
However, that is not what i expect. I thought I would get at least a “hou” or “PyhonModule” module.
It returns me “__main__” if I run this script in a regular python shell outside houdini. It is the way I want.
Does there anyone know the reason why houdini has a “None” top level and is there any way to cheat houdini, to created a fake temp top level module inside my OTL Operator Type Properties?
top level of PyhonModule
4630 8 0- laiwayne
- Member
- 58 posts
- Joined: Sept. 2009
- Offline
- edward
- Member
- 7710 posts
- Joined: July 2005
- Offline
- laiwayne
- Member
- 58 posts
- Joined: Sept. 2009
- Offline
- edward
- Member
- 7710 posts
- Joined: July 2005
- Offline
- laiwayne
- Member
- 58 posts
- Joined: Sept. 2009
- Offline
- lucas
- Staff
- 270 posts
- Joined: July 2005
- Offline
I'm not sure I fully understand what you want to do. Do you want to make a function in a different module appear as though it's defined in the code inside type properties?
If so, one way accessing the current module's dictionary is via globals():
globals() = other_function
Of course, if you're already running this from outside a function/method, you could just write:
function_name = other_function
or
from other_module import other_function
If so, one way accessing the current module's dictionary is via globals():
globals() = other_function
Of course, if you're already running this from outside a function/method, you could just write:
function_name = other_function
or
from other_module import other_function
- laiwayne
- Member
- 58 posts
- Joined: Sept. 2009
- Offline
from functools import wraps
import inspect
def foo(func):
print “wrap”
@wraps(func)
def with_logging(*args, **kwargs):
print func.__name__ + “ wrapped”
return func(*args, **kwargs)
print “wrapII”
print inspect.getmodule(func), func.__name__
setattr(inspect.getmodule(func), func.__name__, with_logging)
return with_logging
def toWrap(x):
print x + x * x
foo(toWrap)
toWrap(2)
so, run this code outside houdini is okay, but error raised inside type properties.
this will make my question more clear
- lucas
- Staff
- 270 posts
- Joined: July 2005
- Offline
Ah. One way to do it is to access the globals dict where the function is defined using the __globals__ attribute of the function object:
from functools import wraps
import inspect
def foo(func):
print “wrap”
@wraps(func)
def with_logging(*args, **kwargs):
print func.__name__ + “ wrapped”
return func(*args, **kwargs)
print “wrapII”
if inspect.getmodule(func) is None:
func.__globals__ = with_logging
else:
setattr(inspect.getmodule(func), func.__name__, with_logging)
return with_logging
def toWrap(x):
print x + x * x
foo(toWrap)
toWrap(2)
from functools import wraps
import inspect
def foo(func):
print “wrap”
@wraps(func)
def with_logging(*args, **kwargs):
print func.__name__ + “ wrapped”
return func(*args, **kwargs)
print “wrapII”
if inspect.getmodule(func) is None:
func.__globals__ = with_logging
else:
setattr(inspect.getmodule(func), func.__name__, with_logging)
return with_logging
def toWrap(x):
print x + x * x
foo(toWrap)
toWrap(2)
- laiwayne
- Member
- 58 posts
- Joined: Sept. 2009
- Offline
-
- Quick Links