hou.hscriptExpression function

Evaluate an Hscript expression.

See also: hou.hscriptFloatExpression, hou.hscriptStringExpression, hou.hscriptVectorExpression, hou.hscriptMatrixExpression

Usage

hscriptExpression(expression) → float, string, or tuple

Given an expression string, this function evaluates it as though it was an Hscript expression on a parameter. The return type depends on the expression that’s evaluated.

Raises hou.OperationFailed if the expression is invalid or generates an error occur during evaluation.

>>> hou.hscriptExpression("$HIP")
'/path/to/hip/directory'
>>> hou.hscriptExpression("$F")
1.0
>>> hou.hscriptExpression('vector("[1, 2, 3]")')
(1.0, 2.0, 3.0)
>>> hou.hscriptExpression('matrix("[[1, 2][3, 4]]")')
((1.0, 2.0), (3.0, 4.0))
>>> hou.hscriptExpression("hello")
'hello'
>>> hou.hscriptExpression("'hello'")
'hello'
>>> hou.hscriptExpression("'hello' + ' world'")
'hello world'
>>> hou.hscriptExpression('"$F"')
'1'
>>> hou.hscriptExpression("'$F'")
'$F'

This function is somewhat similar to hou.expandString with respect to variable expansion. However, expandString will replace the portions of the string containing variables, leaving the rest of the string unchanged. If the variable is unknown, it will not do any expansion.

>>> hou.expandString("$HOME")
'/home/me'
>>> hou.expandString("HOME is $HOME")
'HOME is /home/me'
>>> hou.hscriptExpression("HOME is $HOME")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/opt/hfs9.5/houdini/scripts/python/hou.py", line 19331, in hscriptExpression
    return _hou.hscriptExpression(*args)
OperationFailed: The attempted operation failed.
Syntax error - extra tokens detected in expression
>>> hou.expandString("$F")
'1'
>>> hou.expandString('"$F"')
'"1"'
>>> hou.hscriptExpression("$GARBAGE")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/opt/hfs9.5/houdini/scripts/python/hou.py", line 19331, in hscriptExpression
    return _hou.hscriptExpression(*args)
OperationFailed: The attempted operation failed.
Undefined variable
>>> hou.expandString("$GARBAGE")
'$GARBAGE'
>>> hou.hscript("echo -n $GARBAGE")[0]
''
>>> hou.expandString("")
''
>>> hou.hscriptExpression("")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/opt/hfs9.5/houdini/scripts/python/hou.py", line 19331, in hscriptExpression
    return _hou.hscriptExpression(*args)
OperationFailed: The attempted operation failed.
Invalid expression

See hou.expandString for information about backtick expansion.