Tides are changing and so is the Blender Python API. I would like to know how to properly create, search for and delete an image as my own experiments show confusing results.
At the moment, I have troubles finishing my script for spritesheet generation. It renders animation frames, saves them, loads to actually have access to the pixels, inserts those pixels into a new pool of pixels which will finally be assigned as pixels of the spritesheet. This works fabulously, however, I have problems creating the spritesheet image from scratch while only maintaining one image of that name (older ones might exist from previous script runs for example).
In my version of Blender (2.72) bpy.ops.image.new
does not return the image object but a rather useless finilization-message. This means, I have have to grab the image from bpy.data.images
by name if I don't want iterate over all of them to check for more matching attributes beside the name. There is the catch: Blender automatically iterated the name of already existing image names and seemingly does this differently from time to time. How am I suposed to know exactly the way, the automatic names are numbered? And even if I know that, why must I iterate over all images with a similar name until I identify the one with the highes number behind it? This seems inpractical.
Ok, what if I check if an image of that name already exists and delete it accordingly? This mus free the desired name, though it bears some risks in complex working environments. I will have to clear its users as well as delete it completely to get it out off the system for good. And this is what I tried to do in my spritesheet generator (code snippet):
if spriteSheetName in bpy.data.images:
# delete previous spritesheet radically
spriteSheet = bpy.data.images[spriteSheetName]
if spriteSheet != None:
spriteSheet.user_clear()
bpy.data.images.remove(spriteSheet)
# create new one
bpy.ops.image.new(name = spriteSheetName,
width = shPxWidth, height = shPxHeight,
color = [1, 1, 1, 0], alpha = True)
spriteSheet = bpy.data.images[spriteSheetName]
spriteSheet.filepath = imgDirectory + "\\" + spriteSheetName + "." + imgType
spriteSheet.file_format = imgType.upper()
This results in errors taking place in the Blender Python Api itself:
Traceback (most recent call last):
File "B:\Programs\Blender Foundation\blender-2.72b-windows64\2.72\scripts\startup\bl_ui\space_image.py", line 414, in draw
MASK_MT_editor_menus.draw_collapsible(context, layout)
File "B:\Programs\Blender Foundation\blender-2.72b-windows64\2.72\scripts\modules\bpy_types.py", line 770, in draw_collapsible
cls.draw_menus(layout, context)
File "B:\Programs\Blender Foundation\blender-2.72b-windows64\2.72\scripts\startup\bl_ui\space_image.py", line 497, in draw_menus
if ima and ima.is_dirty:
AttributeError: 'ID' object has no attribute 'is_dirty'
location: <unknown location>:-1
Traceback (most recent call last):
File "B:\Programs\Blender Foundation\blender-2.72b-windows64\2.72\scripts\startup\bl_ui\space_image.py", line 414, in draw
MASK_MT_editor_menus.draw_collapsible(context, layout)
File "B:\Programs\Blender Foundation\blender-2.72b-windows64\2.72\scripts\modules\bpy_types.py", line 770, in draw_collapsible
cls.draw_menus(layout, context)
File "B:\Programs\Blender Foundation\blender-2.72b-windows64\2.72\scripts\startup\bl_ui\space_image.py", line 497, in draw_menus
if ima and ima.is_dirty:
AttributeError: 'ID' object has no attribute 'is_dirty'
location: <unknown location>:-1
So, I am asking you if you know proper image handling via the Blender Python API.
Thanks you in advance!