0
$\begingroup$

I'm making an add-on that does some calculations based on the image you have open in the image editor. Due to the fact that you can have multiple image editors open at a time with different images, it would be great to be able to say "Get image in the current editor."

How would I go about doing this? I'm pretty experienced with python and programming in general, but a complete noob at writing Blender add-ons

$\endgroup$
1
  • 1
    $\begingroup$ i think there is no "current editor". Only one where the mouse cursor is "over". $\endgroup$
    – Chris
    Commented Jun 25, 2021 at 3:09

1 Answer 1

2
$\begingroup$

Context.area returns the area in context (and its attributes). Test using the python console:

>>> C.area
bpy.data.screens['Scripting']...Area

>>> C.area.     
           as_pointer(
           bl_rna
           bl_rna_get_subclass(
           ...

>>> C.area.type
'CONSOLE'

Get the active space of the Image Editor and use SpaceImageEditor.image (in this case the same for all spaces). Example based on the Operator Simple template. Run the script, in the Image Editor press F3 and type "Simple Image...":

import bpy

class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "image.simple_operator"
    bl_label = "Simple Image Editor Operator"

    @classmethod
    def poll(cls, context):
        return context.area.type == 'IMAGE_EDITOR'

    def execute(self, context):
        image = context.area.spaces.active.image
        if image:
            # print (dir(image)) # Print all image attributes
            self.report({'INFO'}, "{}, {}".format(image.name, image.filepath))
        else:
            self.report({'INFO'}, "No active image")
        return {'FINISHED'}


def register():
    bpy.utils.register_class(SimpleOperator)


def unregister():
    bpy.utils.unregister_class(SimpleOperator)


if __name__ == "__main__":
    register()

Related: Active image of UV/Image Editor

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .