raganmd
5/7/2019 - 12:03 AM

load_store_json

import json

def load_store_json(target_file, storage_op, target_key, storage_name):
    ''' 
        A Helper function that reads JSON from disk
    
        Args
        ---------------
        target_file (file path):
        > a path to a .json file on disk. This is where the file will
        > be read from.
        
        storage_op (TouchDesigner operator):
        > the target operator where we will store the dictionary.

        target_key (str):
        > the string key we want to pull from our JSON file.

        storage_name (str):
        > the string name we want to use for storage.
                                
        Returns
        ---------------
        None
    '''

    # open the json file
    json_file 				= open(target_file, 'r')

    # create a dictionary out of our json file
    json_dict               = json.load(json_file).get(target_key)

    # store our dictionary in the target op
    storage_op.store(storage_name, json_dict)

    # close the file
    json_file.close()

# example use
target_file		= '{}/cues.json'.format(project.folder)
storage_op      = op('base_example')
target_key 		= 'presets'
storage_name    = 'presets'

load_store_json(target_file, storage_op, target_key, storage_name)