Jump to content

*noob* How to eval Python expression in Group parameter?


Recommended Posts

Hi,

Done this so many times before, but this morning I'm having trouble.

I have a Dissolve SOP where I need to write out the points to erase using a Python expression. So I set the node's language to Python, and pressed Alt+E in the group parameter. I then wrote this simple script.

str = ""
for x in range(0,10):
 str += " p"+str(x)+"-0"
return str

And then the node generates an error saying invalid group ....... with the python script as the group name.

Why isn't it being evaluated?

Edited by hopbin9
Link to comment
Share on other sites

You need to set an expression/key on the string parameter before you put your Python expression in there. Do an Alt + LMB to create the key before you try to edit the parameter and put in your code.

Also, your code will error as you are redefining the built in 'str' function to be a string, then attempting to call the originally named function.

Link to comment
Share on other sites

You need to set an expression/key on the string parameter before you put your Python expression in there. Do an Alt + LMB to create the key before you try to edit the parameter and put in your code.

Also, your code will error as you are redefining the built in 'str' function to be a string, then attempting to call the originally named function.

THANK YOU for the quick response. That fixed everything. I'll use "s" instead :)

Link to comment
Share on other sites

Alt+E is a bit confusing on string parameters. When you do it on a string parameter, it depends on whether it is in expression mode. If you had made sure your group parameter is in expression mode first (ie. click on the word "Group" so that background changes to dark), then Alt+E will set the expression instead of the value.

Link to comment
Share on other sites

I just find it non-intuitive that there has to be a key frame to write an expression. That just seems wrong to me.

as edward said, it depends on mode, if you are in expression mode it will set expression straight away, no keyframe needed, if you are in string mode it will simply interpret your code as string

if you click on string parameter label, you may notice it changes between normal background and highlighted background

normal means string mode

highlighted is expression mode

as well if you Alt+E in string parm the label of edit window says Edit Expression... or Edit String...

so if you are in Edit Expression... put your python expression and apply, you will not need to set the key before, it will be set for you

it doesn't seem so wrong, unless you expect Houdini to be a mindreader and know if you want to write an python expression or just plain text

EDIT: and note that there has always needed to be a key for every expression in houdini, whether it is hscript or python, it's simply how houdini works, you can set any expression function for any time segment between 2 keyframes, it's quite powerful feature

you can even animate string parms this way, have same string on one keyframe different on another

so sometines even setting key will not ensure that that will be an expression, it's really about expression/string mode

Edited by anim
  • Like 2
  • Downvote 1
Link to comment
Share on other sites

it doesn't seem so wrong, unless you expect Houdini to be a mindreader and know if you want to write an python expression or just plain text

Well, in hscript you use back tick quotes to escape the string, but back ticks quotes don't work if the node's script language is set to Python it just reads it as a string. So that's not consistent.

I understand what you mean by Houdini having to know how to handle the parameter, but when you look at the the Python object hou.Parm() there is no property to tell if an expression is "active" for the parameter. The Parm.expression() will return the expression OR the value. There is no way to exactly tell in the HOM if a parameter has an expression on running it. A Python script that contains just a string "4" will give the same response if the parameter just had the string 4, but to me, the two are different cause one is a Python expression and the other a value.

  • Like 1
Link to comment
Share on other sites

just to clarify

above mentioned modes of parameter (expression/string) are just UI way to tell the user what he is looking at/will edit when Alt+E

therefore they doesn't change/activate/deactivate anything

the same principle as float values, when you want to switch between looking at expression or computed value

hou.Parm.expression() will return you expression on current frame of animated parameter

"4" is perfectly good expression and it evaluates to string 4

"return 4" in python is another expression which evaluates to 4

hou.Parm.expression() always returns expression, so don't be fooled by "4" expression, it is still perfectly valid and it's not just a value that's returned, because it could have been anything else with the same result

all animated parameters have expression on each keyframe so if you are animating strings you are actually setting expressions, if parameter doesn't have any keyframe, it doesn't have any expression (in case of direct references the keyframe is not visible in channel view, since you see referenced parameter directly, but it is there anyway)

to make it even more clear even simple keyframed animation on float parameters is made from expression functions like bezier(), linear(), this concept is very powerful since you can mix hand keyframed animation with other functions

the only thing I agree that may be confusing is that string parameters don't set keyframe when writing python expression in expression mode directly to the field it works only with Alt+E or if the keyframe is already present

I guess that if this is fixed, it will be less confusing for many people, this still doesn't mean that keyframe should be present since most of the time you need to have Alt+E editor open when you are dealing with multiline python expressions so it's just the matter of going there from expression mode, setting keyframe to write expression isn't the big deal either since keyfrme needs to be set for every expression anyway and Alt+Click is certainly as easy as writing ` `

the consistency with Hscript may not be a big problem either since Python was planned to replace Hscript at some point so I don't mind if it has different rules.

Edited by anim
Link to comment
Share on other sites

to make it even more clear even simple keyframed animation on float parameters is made from expression functions like bezier(), linear(), this concept is very powerful since you can mix hand keyframed animation with other functions

That is correct. I forgot about that, and it's a good point.

If anything good, after this long chain of postings. If I forget to set a key frame again to write a Python expression in a string. You have my permission to shoot me for asking about it again.

Link to comment
Share on other sites

FWIW, it's really the "backticks" which are inconsistent. If they didn't exist, then there is exactly 1 way to do expressions (hscript or python) in Houdini, for any type of parameter. The problem of course is that people far too addicted to using backticks. :)

  • Like 2
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...