Skip to content

Instantly share code, notes, and snippets.

@raganmd
Last active May 7, 2019 00:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raganmd/76aaf3414612908ec253f2e8f70508d0 to your computer and use it in GitHub Desktop.
Save raganmd/76aaf3414612908ec253f2e8f70508d0 to your computer and use it in GitHub Desktop.
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment