How to I read a key-value dictionary from an expression?

   7538   7   2
User Avatar
Member
11 posts
Joined: 5月 2017
Offline
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
User Avatar
Member
1922 posts
Joined: 11月 2006
Offline
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
User Avatar
Member
77 posts
Joined: 2月 2017
Offline
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)

My Youtube Channel [www.youtube.com]
User Avatar
Member
11 posts
Joined: 5月 2017
Offline
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 (
chs("json")
) instead of just directly using a string in
json_loads(chs("json"), 0);
? just curious :-)

Cheers
Jacob
User Avatar
Member
77 posts
Joined: 2月 2017
Offline
JacobAndersen
one (unrelated) question zengchen, is there a deeper meaning why you use a string parameter

houdini 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
My Youtube Channel [www.youtube.com]
User Avatar
Member
19 posts
Joined: 7月 2017
Offline
Hi, do you know how to do the reverse? I mean write the dictionary with python?
John Fanny | Fx Apprentice
User Avatar
Member
1922 posts
Joined: 11月 2006
Offline
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
User Avatar
Member
19 posts
Joined: 7月 2017
Offline
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 Apprentice
  • Quick Links