Exporting JSON files with properties for NFTs

Hi Python friends :grinning:

I am creating random artworks for NFTs, with a script that selects random files, imports them in Blende, renders a picture, saves the picture with the names of the 3D files that have been used, repeat the process.
I need to update the script in order to export, besides the rendered pictures, also JSON files with properties.
Could someone help me?

Since my actual script saves the rendered pictures with the name of the 3D files that have been used, let’s imagine that the script randomly selects these files:
AA1.blend
BB3.blend
CC6.blend

The first rendered picture is automatically saved as 01_AA1_BB2_CC2.jpg

I would like to have also a json file exported, called 01_AA1_BB2_CC2.json

I would like to have a “source list”, that specify what the codes in the files names represent.
For example:

01 = Artwork N.01
02 = Artwork N.02
AA1 = Shape: Cube
AA2 = Shape: Cone
AA3 = Shape: Sphere
BB1 = Color: White
BB2 = Color: Red
CC1 = Background: Blue
CC2 = Background: Green
DD1 = Material: Plastic
DD2 = Material: Metal

The JSON files for NFTs would take information from that list, and have this structure (according to my example):

{
“description”: “Randomly generated artworks”,
“external_url”: “https://zzz.com/3”,
“image”: “https://yyy.com/01_AA1_BB2_CC2.jpg”,
“name”: “Artwork N.01”,
“attributes”: [
{
“trait_type”: “Shape”,
“value”: “Cube”
},
{
“trait_type”: “Color”,
“value”: “Red”
},
{
“trait_type”: “Background”,
“value”: “Green”
}
]
}

Note that some pictures don’t have all the properties (in the example the pictures doesn’t have the material property)

This is the script that I am using at the moment:

import bpy
import time
import random
import itertools
from pathlib import Path

NUMBER_OF_IMAGES = 3

WorkDir = bpy.path.abspath(‘//’)
RenderDir = Path(WorkDir).absolute().parent.joinpath(“renders”)

def ShowMessageBox(title, message, icon=“ERROR”):

def draw(self, context):
    self.layout.label(text = message)

if not bpy.app.background:
    bpy.context.window_manager.popup_menu(draw, title=title, icon=icon)

def main():
# – where are the assets
asset_dirs = [d for d in Path(WorkDir).glob(“*”) if d.is_dir()]
if not asset_dirs:
ShowMessageBox(“Asset Error”, “No assets found”)
return

# -- assets permutations
assets_perms = []
for d in asset_dirs:
    blend_files = list(d.glob("*.blend"))
    if not blend_files:
        continue

    assets_perms.append(
        blend_files
    )

generated = set()
for _ in range(NUMBER_OF_IMAGES):
    files = []
    while True:
        for perm in assets_perms:
            random.seed(time.time())
            file  = random.choice(perm)
            files.append(file)

        if files not in list(generated):
            generated.add(tuple(files))
            break

    imported_objs = []
    for file in files:

        with bpy.data.libraries.load(str(file), link=False) as (data_from, data_to):
            data_to.objects = data_from.objects

        collection = bpy.context.collection
        for obj in data_to.objects:
            collection.objects.link(obj)

        imported_objs.extend(data_to.objects)

    # -- render the current combination
    name = "_".join([f.stem for f in files if f])
    bpy.context.scene.render.filepath = str(RenderDir.joinpath(f"{name}"))
    bpy.ops.render.render(write_still=True)

    # -- clear 
    bpy.ops.object.select_all(action='DESELECT')
    [o.select_set(True) for o in imported_objs]
    bpy.ops.object.delete(use_global=False)


    # Clear orphan data blocks/meshes
    for block in bpy.data.meshes:
        if block.users == 0:
            bpy.data.meshes.remove(block)

ShowMessageBox("Done", "The Renders have been saved to {}".format(RenderDir), icon="INFO")

if name == ‘main’:
main()

Could someone tell me how it should be updated in order to generate also json files as described? :slight_smile: