I wasn't sure if I should answer since it's mostly a Python question, but it has a few small Blender specific aspects...
import bpy, json
from pathlib import Path
C = bpy.context
# define what identifiers mean:
shapes = {
"AA1": "Sphere",
"AA2": "Cylinder",
"AA3": "Cube",
"AA4": "Torus",
}
colors = {
"CC1": "White",
"CC2": "Blue",
"CC3": "Cyan",
"CC4": "Green",
"CC5": "Yellow",
"CC6": "Red",
"CC7": "Black",
}
blendpath = Path(C.blend_data.filepath) # create Path object based on absolute path to current .blend file
JSONpath = blendpath.with_suffix(".txt") # where JSON will be saved
fileformat = C.scene.render.image_settings.file_format.lower()
renderpath = blendpath.with_suffix(f".{fileformat}") # where render will be saved
if renderpath.exists() or JSONpath.exists():
raise FileExistsError("I don't want to override something!")
shapeid, colorid = blendpath.stem.split("_") # assumes only one underscore
shape = shapes[shapeid] # translate AA3 to Cube
color = colors[colorid] # translate CC6 to Red
output_dict = {"Shape": shape, "Color": color} # create a structure to be converted to JSON
# using "x" instead of "w" to error if the file already exists, though very unlikely due to line 29
with open(JSONpath, "x") as f:
json.dump(output_dict, f)
C.scene.render.filepath = str(renderpath) # Blender gets confused without explicitly converting to str
# thanks to "INVOKE_DEFAULT" the script will finish before the render is done and so will not hang the interface
bpy.ops.render.render("INVOKE_DEFAULT", write_still=True)
Quick way to get current opened filename in a script?
Is it possible to make a sequence of renders and give the user the option to cancel the process through the UI at any given time?