Jump to content


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


  • Please log in to reply
9 replies to this topic

#1 hopbin9

hopbin9

    Houdini Master

  • Members
  • PipPipPipPip
  • 803 posts
  • Joined: 14-March 10
  • Location:Canada
  • Name:Hop Bin

Posted 28 July 2012 - 06:37 AM

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, 28 July 2012 - 06:37 AM.

Posted Image
Come download the free open source asset library for Houdini.
Help us reach 1,000 fans on facebook!

#2 graham

graham

    Houdini Master

  • Moderator
  • 666 posts
  • Joined: 13-September 06
  • Location:Santa Monica, CA
  • Name:Graham Thompson

Posted 28 July 2012 - 06:45 AM

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.
I write python and break things.

#3 hopbin9

hopbin9

    Houdini Master

  • Members
  • PipPipPipPip
  • 803 posts
  • Joined: 14-March 10
  • Location:Canada
  • Name:Hop Bin

Posted 28 July 2012 - 06:47 AM

View Postgraham, on 28 July 2012 - 06:45 AM, said:

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 :)
Posted Image
Come download the free open source asset library for Houdini.
Help us reach 1,000 fans on facebook!

#4 edward

edward

    Grand Master

  • Members
  • PipPipPipPipPip
  • 3,325 posts
  • Joined: 10-September 02
  • Name:e.d.w.a.r.d. .

Posted 28 July 2012 - 06:52 AM

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.
don't panic!

#5 hopbin9

hopbin9

    Houdini Master

  • Members
  • PipPipPipPip
  • 803 posts
  • Joined: 14-March 10
  • Location:Canada
  • Name:Hop Bin

Posted 28 July 2012 - 04:11 PM

I just find it non-intuitive that there has to be a key frame to write an expression. That just seems wrong to me.
Posted Image
Come download the free open source asset library for Houdini.
Help us reach 1,000 fans on facebook!

#6 anim

anim

    Houdini Master

  • Members
  • PipPipPipPip
  • 910 posts
  • Joined: 23-August 07
  • Location:Slovakia, Bratislava
  • Name:Tomas Slancik

Posted 28 July 2012 - 05:34 PM

View Posthopbin9, on 28 July 2012 - 04:11 PM, said:

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, 28 July 2012 - 05:41 PM.

Tomas Slancik
Generalist
Slovakia

#7 hopbin9

hopbin9

    Houdini Master

  • Members
  • PipPipPipPip
  • 803 posts
  • Joined: 14-March 10
  • Location:Canada
  • Name:Hop Bin

Posted 28 July 2012 - 07:10 PM

View Postanim, on 28 July 2012 - 05:34 PM, said:

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.
Posted Image
Come download the free open source asset library for Houdini.
Help us reach 1,000 fans on facebook!

#8 anim

anim

    Houdini Master

  • Members
  • PipPipPipPip
  • 910 posts
  • Joined: 23-August 07
  • Location:Slovakia, Bratislava
  • Name:Tomas Slancik

Posted 28 July 2012 - 08:35 PM

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, 28 July 2012 - 08:41 PM.

Tomas Slancik
Generalist
Slovakia

#9 hopbin9

hopbin9

    Houdini Master

  • Members
  • PipPipPipPip
  • 803 posts
  • Joined: 14-March 10
  • Location:Canada
  • Name:Hop Bin

Posted 29 July 2012 - 07:21 AM

View Postanim, on 28 July 2012 - 08:35 PM, said:

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.
Posted Image
Come download the free open source asset library for Houdini.
Help us reach 1,000 fans on facebook!

#10 edward

edward

    Grand Master

  • Members
  • PipPipPipPipPip
  • 3,325 posts
  • Joined: 10-September 02
  • Name:e.d.w.a.r.d. .

Posted 30 July 2012 - 07:32 PM

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. :)
don't panic!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users