I want to get a dict from a collection property which will be used for exporting a json file. I completely have no idea how I should do it, so I don't have any code for it.

Thanks for helping me.

I have a propertyGroup-

class Favourites(PropertyGroup):
    label: StringProperty(
    idname: StringProperty(
    icon_value: IntProperty(
        name="Icon value",
    index: IntProperty(
    is_preset: BoolProperty(
        name='Is preset',
    mode: StringProperty()

And this property group is registered as Type of a CollectionProperty in register def

bpy.types.Scene.favit = CollectionProperty(type=Favourites)

I want the list of data in bpy.context.scene.favit to be stored in a json.

  • 1
    $\begingroup$ dict(your_collection)? Recommend add your data structure to the question, also how you'd like to format your data... $\endgroup$
    – brockmann
    Oct 5 '20 at 11:20
  • $\begingroup$ Hi I added some codes from my script. Please check it. $\endgroup$
    – Me BMan
    Oct 5 '20 at 11:29
  • $\begingroup$ Have you tried dict(C.scene.favit)? $\endgroup$
    – brockmann
    Oct 5 '20 at 11:44
  • $\begingroup$ {'': <bpy_struct, Favourites("") at 0x000001A6DB344EC8>} It gives this result. When I try dict(C.scene.favit[0]) it gives me the properties {'label': 'Tweak', 'idname': 'builtin.select', 'icon_value': 986, 'index': 0, 'is_preset': 0} I have a list of those properties per fav $\endgroup$
    – Me BMan
    Oct 5 '20 at 11:48
  • 1
    $\begingroup$ No worries, just trying to figure out what you'd like to achieve @MeBMan Final question: How the json string should look like? $\endgroup$
    – brockmann
    Oct 5 '20 at 12:03

If you don't need to write out default value, just use items() like dictionary.

Unless you assign it, Blender will not create an entry for them.

import bpy
import json

data = [
    for prop in bpy.context.scene.favit

print(json.dumps(data))  # save this string to json file


[[["index", 1], ["label", "hello world"]], [["label", "cool world"]]]

The output format is same as [key, value] in a list, once you want to write back to blender,

json_string = '''[[["index", 1], ["label", "hello world"]], [["label", "cool world"]]]'''
data = json.loads(json_string)
for prop_list in data:
    prop = bpy.context.scene.favit.add()
    for key, value in prop_list:
        prop[key] = value
  • $\begingroup$ What is prop? I get error NameError: name 'props' is not defined $\endgroup$
    – Me BMan
    Oct 5 '20 at 12:06
  • $\begingroup$ props? I didn't write props, prop maybe? $\endgroup$
    – HikariTW
    Oct 5 '20 at 12:09
  • $\begingroup$ Oh my bad, I did a mistake. it should be ""prop"" not "props" $\endgroup$
    – Me BMan
    Oct 5 '20 at 12:11
  • $\begingroup$ Hi, I think this is what I want. If you could also parse it out, it would be really great, How do I read per favourite when I read from the saved json? Thank you so much $\endgroup$
    – Me BMan
    Oct 5 '20 at 12:16
  • $\begingroup$ Nice Thanks so much!. Working like charm! $\endgroup$
    – Me BMan
    Oct 5 '20 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.