1
$\begingroup$

I think it should be simple, but could not find correct way ore related document then hope to know way.

Now I have active armature as "amt" , and it have many ID properties for shape keys and drive some bones. eg amt["prop1"] = 1.0, amt["prop2"] = 0.2 etc,,

by bpy, I can get amt ID property keys and values of current frame, which I only need to export, as python dictionary.

eg preset = {"prop1":1.0, "prop3" :0.2,,,}

Now I hope to export the dictionary as json, then import the saved json , and apply these ID property values for current active armature, when I need. (of course it have same ID property keys) (I prefer to export as json,,)

$\endgroup$
1
  • 1
    $\begingroup$ You might want to change the word ID to Key since in Python, ID is a unique special thing which is different from dictionary key in general. $\endgroup$
    – HikariTW
    May 11, 2019 at 9:28

2 Answers 2

2
$\begingroup$

This code get some data from Blender scene, save them as Python dict and write a JSON file. Then read that file and uses the inner data:

import bpy
import json
import os

# 1 - Export data as JSON file

# dict with all your data
dict = {
    "name": "Cube 1",
    "material": "Cube MAT"
}

# encode dict as JSON 
data = json.dumps(dict, indent=1, ensure_ascii=True)

# set output path and file name (set your own)
save_path = 'E:\\'
file_name = os.path.join(save_path, "export_data.json")

# write JSON file
with open(file_name, 'w') as outfile:
    outfile.write(data + '\n')


# 2 - Import data from JSON file

# read JSON file
with open(file_name, 'r') as fp:
    data_file = json.load(fp)

# get data
cube_name = data_file['name']
cube_material = data_file['material']

print(cube_name + ', ' + cube_material)
$\endgroup$
2
  • $\begingroup$ Can you give an explanation of why use json.dumps() instead of json.dump()? I know the later is seldom used in practice, but still don't know why. $\endgroup$
    – HikariTW
    May 11, 2019 at 9:55
  • $\begingroup$ J.Aron Really thanks , it is what I need I think. I am now edit blender python template with compare your answer for json. and I use ID properties of armature obj, not use bpy.props, then your answer much for my question. $\endgroup$
    – tokikake
    May 11, 2019 at 13:45
0
$\begingroup$

As you said, use json module: Reference

You will need to handle the conversion between properties and Dictionary by yourself, maybe two bpy.type.Operator with a PropertiesGroup.

And load/save the dictionary by json.dump() and json.load()

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .