1
$\begingroup$

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!

$\endgroup$
1
$\begingroup$

This: bpy.data.images.new(name, width, height, alpha=False, float_buffer=False, stereo3d=False) returns the image created.

bpy.data.images.remove(image) removes the image.

$\endgroup$
  • $\begingroup$ bpy.data.images.new is what I sought after. It's easy to release the users and remove the image, now. Frankly, some unexpected bugs (?) in my surrounding scripts occured, but - hey - human error knows no limits! When in comes to naming $\endgroup$ – Florian R. Klein Feb 10 '16 at 18:14
  • $\begingroup$ Cool, so this can be marked as answered? $\endgroup$ – cmomoney Feb 10 '16 at 18:51
  • $\begingroup$ Not yet. Maybe someone has more to add. If it's like in stackoverflow, I would have to wait 2 days anyway to give anybody the final answered mark. $\endgroup$ – Florian R. Klein Feb 10 '16 at 19:57

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.