i_o.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # this is the I/O part of mantis. I eventually intend to make this a markup language. not right now tho lol
  2. # https://stackoverflow.com/questions/42033142/is-there-an-easy-way-to-check-if-an-object-is-json-serializable-in-python
  3. # thanks!
  4. def is_jsonable(x):
  5. import json
  6. try:
  7. json.dumps(x)
  8. return True
  9. except (TypeError, OverflowError):
  10. return False
  11. def export_to_json(tree, path):
  12. # takes a parsed mantis tree and returns a JSON
  13. tree_data = tree.parsed_tree
  14. # we have to prepare the tree data.
  15. # SO: we can't use tuples as keys in JSON
  16. # but the tree uses a lot of tuple keys in Python dicts
  17. # so we can map the tuples to UUIDs instead
  18. # then we can store the list of nodes mapped to the same UUIDs
  19. # the goal is to store the tree in a way that makes reproduction possible
  20. import json
  21. with open(path, "w") as file:
  22. print("Writing mantis tree data to: ", file.name)
  23. file.write( json.dumps(json_data, indent = 4) )
  24. def import_from_json(json_data):
  25. #reads a JSON file and makes a tree from it.
  26. pass
  27. def tree_to_b_nodes(tree):
  28. # creates a node tree in Blender from a mantis tree
  29. pass