2

I am trying to enable backdrop in Blender using Python and using Blender's Python API (no GUI) but it seems that it is not as straight-forward as I thought. I know how to enable Use Nodes via bpy.context.scene.use_nodes = True and just need to enable backdrop. The most relevant Python documentation for backdrop Here is a bit unclear and I do not know exactly how I can enable backdropping through the NodeSpaceEditor class. Can someone explain how I can do that?

Update: it turns out that the Python documentation is useful when you are running Blender with the GUI. I need to enable backdrop when I am running Blender in the background.

The hard way is to compile Blender from scratch and enable backdropping by modifying the source code as explained here but this is too much of work. I hope there is a way to do it using Blender's Python API.

Amir
  • 3,074
  • 2
  • 29
  • 55
  • Do you see the python in the Info window as you change an editor to Node Editor and then change that to use a Compositor Node tree, and then tell it to 'use nodes' and 'show backdrop'? It is all of those and not just a single command that is involved here to use the backdrop, so we have to tell it to change or use each of those. – Craig D Jones Mar 14 '18 at 04:06
  • @CraigDJones Sorry I was assuming that we have done all of those steps except the backdrop part. Do you know how one can enable backdrop using Python in this case? I'm gonna update my question to make it clear. – Amir Mar 14 '18 at 04:13
  • Did you see the python in the info editor? bpy.context.space_data.show_backdrop = True This is what pops up, so it tells me that if I am in the Node Editor already and in the Compositor Node tree, then this allows me to tell it to use the back drop. – Craig D Jones Mar 14 '18 at 04:34
  • I believe you will have to switch to the correct editor first and meet all those requirements first, since just running that as a command on its own causes an error since the text editor doesn't have that attribute. – Craig D Jones Mar 14 '18 at 04:37
  • @CraigDJones Sorry I'm a bit confused. I think I'm getting what you are saying but I don't know how I can do the switching the way you describe. Could you post an answer and describe how I should do that, kind of step-by-step? Really appreciate it. – Amir Mar 14 '18 at 04:41
  • Can you share what code you have so far so that I can see how to approach what you are doing? I am not the best, but I might help find the answers to fit the script you are building. – Craig D Jones Mar 14 '18 at 04:48
  • @CraigDJones I don't have a long script. I just do bpy.context.scene.use_nodes = True to use nodes and the next thing I need to do is to enable backdrop using one or more lines of Python code, which I don't know how to do as of now. – Amir Mar 14 '18 at 05:02
  • 4
    Loathe to answer your questions because there is always some caveat, like by python do you mean running bpy as a python module?. Quite simply the backdrop in your first link can be set via context.space_data.use_backdrop = True as long as the context.area.type == 'NODE_EDITOR' otherwise loop screen areas and if node editor type use area.spaces.active – batFINGER Mar 14 '18 at 10:55
  • @batFINGER Sorry that my questions have some caveats but I have to ask what I have in my mind somehow and I sometimes forget/cannot to explain everything the way everyone expect, maybe because of my wrong assumptions. I will try to be more clear from now onwards. Yes I meant running python as a module. When I do import bpy I do not have access to bpy.context.space_data.use_backdrop or bpy.context.area.type but I can set area.type to NODE_EDITOR when I am using the Desktop version of Blender. Any ideas on how I can do it when using the Python module? Thank you – Amir Mar 14 '18 at 16:36
  • 2
    Not sure I see the point of wanting a UI backdrop when running without a UI. – batFINGER Mar 14 '18 at 19:45
  • @batFINGER Sorry for the confusion. I didn't even know that the first link was pointing to a part of the documentation that is relevant to UI. I'm still newish to Blender so please bare with me :) – Amir Mar 14 '18 at 20:14

1 Answers1

1

You can set this property, you just have to know which area is the node editor. Like this:

import bpy

def get_node_editor_area():
    """
    Get the index of the area containing the compositor
    """
    for i in range(len(bpy.context.screen.areas)):
        area = bpy.context.screen.areas[i]
        if area.type == 'NODE_EDITOR':
            return i

area_index = get_node_editor_area()
bpy.context.screen.areas[area_index].spaces[0].show_backdrop = True

If this is for an addon and the user will have the mouse in the node editor area when this function is called, I think you could use bpy.context.space_data.show_backdrop = True instead.

doakey3
  • 1,946
  • 11
  • 24
  • This seems to be only working when you are running Desktop (with UI) version of Blender. I couldn't run this when using Blender as module or when running Blender in the background. Did you also try doing that? When I print area.type in the for loop I get the followings: INFO, PROPERTIES ,TIMELINE, OUTLINER, VIEW_3D. When running with the GUI I also get NODE_EDITOR. – Amir Mar 14 '18 at 20:38
  • 1
    Why do you want to show the backdrop without running Blender's user interface? For my script to work, one of the areas open in blender must be the node editor. – doakey3 Mar 14 '18 at 20:46
  • Well I do not really need Backdrop to see something (because I cannot). I need to enable backdrop so that I can update the contents of the Viewer Node image block. Maybe you can take a look at my problem here; this might give you an idea of what I'm trying to do. This is also a bit relevant. Any help would be really appreciated. – Amir Mar 14 '18 at 20:48
  • I don't know much about running python in Blender without a UI, but have you tried setting one of the areas of Blender's UI to the Node Editor, then saving the startup file, then trying my script? Or perhaps loading a .blend file with one of the areas set to the Node editor when you run blender from the terminal. – doakey3 Mar 14 '18 at 21:04