ObiNon Posted December 10, 2021 Share Posted December 10, 2021 (edited) Hi Guys, I am reading data in from a JSON file and attempting to store it in a detail attribute and then read back a lookup value in a wrangle. The JSON data looks like this. { "DOM_IDS":[ { "DOM_ID": "00", "PATTERN": "" }, { "DOM_ID": "01", "PATTERN": "13" }, { "DOM_ID": "02", "PATTERN": "11,15" } ] } The code to push that to a detail attribute looks like this for me ... node = hou.pwd() geo = node.geometry() import csv, json file = node.evalParm("data_dir")+node.evalParm("json") dom_dict = {} with open(file, 'r') as json_file: dom_dict = json.load(json_file) geo.addAttrib(hou.attribType.Global, "dom_dict", {}, 0, 0) geo.setGlobalAttribValue("dom_dict", dom_dict) This works .. but I have no idea if this is formatted correctly The detail attribute has this value ... {"DOM_IDS": [{"DOM_ID": "00", "PATTERN": ""}, {"DOM_ID": "01", "PATTERN": "13"}, {"DOM_ID": "02", "PATTERN": "11,15"}]} Can anyone tell me how to access a given PATTERN using the DOM_ID as a key (using an attribute wrangle) ?? Thanks a lot !!! Mike Edited December 10, 2021 by Mdonovan Quote Link to comment Share on other sites More sharing options...
JXS Posted December 15, 2021 Share Posted December 15, 2021 (edited) On 12/9/2021 at 9:55 PM, Mdonovan said: Can anyone tell me how to access a given PATTERN using the DOM_ID as a key (using an attribute wrangle) ?? Not sure if this is what you're looking for but here goes. Upon importing your JSON file, I "cleaned it up" a bit and created a new dictionary called "process" using DOM_ID/PATTERN as key/value pairs. I retained the original dict for clarity's sake. Then in an attribute wrangle running in Detail(any mode will do, but I have no geometry here in this scene so detail is the appropriate mode), just import your dictionary using the detail() function, then access the values using whatever DOM_ID you wish. I cleaned it up because basically, in VEX terms, you have a single key pointing to an array of dictionaries and each dictionary's DOM_ID is separate from its pattern. If anything you wouldn't even need the DOM_ID to access the PATTERN. Something like the following in the attribute wrangle would get you your PATTERN: dict list_of_dicts[] = detail(0, "dom_dict")["DOM_IDS"]; foreach(dict dct; list_of_dicts){ string pat = dct["PATTERN"]; printf("%s\n", pat); } Edited December 15, 2021 by JXS Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.