How to I read a key-value dictionary from an expression?
8349
9
2
2020年11月6日 8:51
As the title says, I am trying to use a key-value dictionary parameter to drive another parameter via an expression but I can't seem to make it work?
ch ( "../Parms/Dict" )[ ch("../Parms/Key")]
I haven't been able to find any information regarding this, I'm afraid.
Cheers
Jacob
Edited by JacobAndersen - 2020年11月7日 11:22:45
graham
Member
1925 posts
Joined: 11月 2006
Offline
2020年11月6日 11:08
The best you can do currently via Hscript Expressions is to use the
chs function to get the entire value:
/ -> echo `chs("/obj/geo1/newparameter")`
{ foo:bar, hi:bye }
There is no way to directly get the value of a particular key.
In order to do that you would have to use a
pythonexprs call to evaluate it as a dictionary in Python:
/ -> echo `pythonexprs("hou.parm('/obj/geo1/newparameter').eval()['foo']")`
bar
Graham Thompson, Technical Artist @ Rockstar Games
zengchen
Member
77 posts
Joined: 2月 2017
Offline
2020年11月7日 6:45
I have a method to read dict parm but must use
Houdini 18.5 's dict attribute and json function.
dict attr help [
www.sidefx.com ]
Now houdini does not have expression to deal with JSON, so you need a wrangle to convert JSON to dict attr…
And then use dict attr to drive another parm.
However, there are no JSON-parsing expression functions, and limited string manipulation functions, to deal with such a string in an expression. JSON loads vex function [
www.sidefx.com ]
Edited by zengchen - 2020年11月7日 06:51:21
Attachments:
dict_test.hip (95.5 KB)
2020年11月7日 11:22
Thank you both of you for the answers.
I ended up using the pythonexprs solution but tested out the JSON one too just to learn some more :-)
one (unrelated) question zengchen, is there a deeper meaning why you use a string parameter (
) instead of just directly using a string in
json_loads ( chs("json"), 0);
? just curious :-)
Cheers
Jacob
zengchen
Member
77 posts
Joined: 2月 2017
Offline
2020年11月7日 21:30
JacobAndersen one (unrelated) question zengchen, is there a deeper meaning why you use a string parameterhoudini expression can only recognise two type - string(chs) and float(ch).
dict parm can not be interpreted as a float type, so it need to be treat as a string parameter, the result is a JSON format string, can be loads(loads is a function) as a dict attribute use vex function.
Edited by zengchen - 2020年11月8日 00:00:23
JohnFanny
Member
20 posts
Joined: 7月 2017
Offline
2023年6月15日 14:44
Hi, do you know how to do the reverse? I mean write the dictionary with python?
John Fanny | Fx Artist
graham
Member
1925 posts
Joined: 11月 2006
Offline
2023年6月15日 15:03
JohnFanny Hi, do you know how to do the reverse? I mean write the dictionary with python?You can set the hou.Parm directly with a dictionary, however all the keys and values will need to be strings.
hou . parm ( '/obj/subnet1/newparameter' ) . set ({ "key1" : "value1" , "key2" : "value2" })
Graham Thompson, Technical Artist @ Rockstar Games
JohnFanny
Member
20 posts
Joined: 7月 2017
Offline
2023年6月15日 20:32
Wow! Thanks a lot, it works perfectly! One last thing: Does it have a way to select one of the entries like in this example?
John Fanny | Fx Artist
2025年2月3日 0:02
found you can also do this expression in parm:
hou . node ( "/stage/sopcreate1/sopnet/create/IN_pt1" ) . geometry () . point ( 0 ) . attribValue ( 'dict' )[ 'key' ]
hou . node ( "/stage/sopcreate1/sopnet/create/IN_pt1" ) . geometry () . attribValue ( 'stuff' )[ 'it' ]
Edited by GrahamDClark - 2025年2月3日 00:19:24
tamte
Member
9078 posts
Joined: 7月 2007
Offline
2025年2月3日 1:28
GrahamDClark found you can also do this expression in parm:hou . node ( "/stage/sopcreate1/sopnet/create/IN_pt1" ) . geometry () . point ( 0 ) . attribValue ( 'dict' )[ 'key' ]
hou . node ( "/stage/sopcreate1/sopnet/create/IN_pt1" ) . geometry () . attribValue ( 'stuff' )[ 'it' ]
if you mean reading dictionary attribute, then you can also use hscrip which I think has a cleaner syntax
point ( "/stage/sopcreate1/sopnet/create/IN_pt1" , 0, "dict.key" , 0)
detail ( "/stage/sopcreate1/sopnet/create/IN_pt1" , "stuff.it" , 0)
and of course for string values points(), details(), ...
Edited by tamte - 2025年2月3日 01:29:05
Tomas Slancik FX Supervisor Method Studios, NY