Jump to content
Sign in to follow this  
Mdonovan

JSON, PYTHON and Detail Attribs

Recommended Posts

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 by Mdonovan

Share this post


Link to post
Share on other sites
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.

61b98919cb4f1_untitled.hip-HoudiniFX19.0.383-Python312_15_202112_15_38AM.thumb.png.fecd781cdbd1178de0f6896c0a740f0e.png

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.

61b989bbb699f_untitled.hip-HoudiniFX19.0.383-Python312_15_202112_18_06AM.thumb.png.5961bbd9bee381cf1f0e3f67b83e7998.png

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 by JXS

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×