Jump to content

JSON, PYTHON and Detail Attribs


Mdonovan

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

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...