3
$\begingroup$

Is there any possibility for that? Modal operator seems to be not suitable for that

$\endgroup$
6
+25
$\begingroup$

You can achieve this by using a scene update handler that monitors the state of the active image in the image editor.

For this technique to work you need to:

  1. Create or open an image in the image editor window on the active screen.
  2. Save your image to an external file once.
  3. Also: the script will save any change to file immediately. If you don't care about that, this solution can work for you.

The example below illustrates how you can set a new random paintbrush color after every stroke.

enter image description here

import bpy, random

def run_if_img_changed( scene ):
    im = None
    # References the active image in the image editor
    for a in bpy.context.screen.areas:
        if a.type == 'IMAGE_EDITOR':
            for s in a.spaces:
                if s.type == 'IMAGE_EDITOR':
                    im = s.image
                    break

    if im and im.is_dirty:
        ## Replace the bit in this if clause with your code
        print( "Image changed!" )

        # Set paintbrush to random color
        bpy.data.brushes['TexDraw'].color =  [ random.random() for c in 'rgb' ]

        ## End of replaceable sample code

        # Save image so it will no longer be dirty
        im.save() # This line will crash the script if you haven't saved the image to a file yet

bpy.app.handlers.scene_update_post.append( run_if_img_changed )
$\endgroup$
4
  • $\begingroup$ Getting image by name is cumbersome. (im = bpy.data.images['Untitled.001']) Is there a way to obtain currently editing image? $\endgroup$
    – wilddev
    Nov 3 '16 at 16:04
  • $\begingroup$ @wilddev, You can reference any image there, this is simply the example I used above. This is the name of your image, which you can set in the image editor. $\endgroup$
    – TLousky
    Nov 3 '16 at 16:07
  • $\begingroup$ handler function receives just a scene, but not a context of image editor. I don't know how to receive such a context inside a handler function =( $\endgroup$
    – wilddev
    Nov 3 '16 at 16:13
  • $\begingroup$ @wilddev, updated to read image from image editor. Lemme know if works as expected. $\endgroup$
    – TLousky
    Nov 3 '16 at 16:27

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.