uv_texture_atlas/bake to vcols: moved to contrib: update class names: T63750
[blender-addons-contrib.git] / amaranth / node_editor / display_image.py
1 #  This program is free software; you can redistribute it and/or
2 #  modify it under the terms of the GNU General Public License
3 #  as published by the Free Software Foundation; either version 2
4 #  of the License, or (at your option) any later version.
5 #
6 #  This program is distributed in the hope that it will be useful,
7 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
8 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9 #  GNU General Public License for more details.
10 #
11 #  You should have received a copy of the GNU General Public License
12 #  along with this program; if not, write to the Free Software Foundation,
13 #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
14 """
15 Display Active Image Node on Image Editor
16
17 When selecting an Image node, it will show it on the Image editor (if
18 there is any available). If you don't like this behavior, you can
19 disable it from the Amaranth Toolset panel on the Scene properties.
20 Coded by the awesome Sergey Sharybin. This feature only works on Blender
21 2.68 and newer. Select an Image Node in the Compositor or Cycles nodes
22 editor, there must be at least one image editor available.
23 """
24
25 import bpy
26
27
28 KEYMAPS = list()
29
30 image_nodes = ("CompositorNodeRLayers",
31                "CompositorNodeImage",
32                "CompositorNodeViewer",
33                "CompositorNodeComposite",
34                "ShaderNodeTexImage",
35                "ShaderNodeTexEnvironment")
36
37
38 class AMTH_NODE_OT_show_active_node_image(bpy.types.Operator):
39     """Show active image node image in the image editor"""
40     bl_idname = "node.show_active_node_image"
41     bl_label = "Show Active Node Node"
42     bl_options = {"UNDO"}
43
44     def execute(self, context):
45         return {'FINISHED'}
46
47     def invoke(self, context, event):
48         mlocx = event.mouse_region_x
49         mlocy = event.mouse_region_y
50         select_node = bpy.ops.node.select(mouse_x=mlocx, mouse_y=mlocy, extend=False)
51
52         if 'FINISHED' in select_node:  # Only run if we're clicking on a node
53             get_addon = "amaranth" in context.preferences.addons.keys()
54             if not get_addon:
55                 return {"CANCELLED"}
56
57             preferences = context.preferences.addons["amaranth"].preferences
58             if preferences.use_image_node_display:
59                 if context.active_node:
60                     active_node = context.active_node
61
62                     if active_node.bl_idname in image_nodes:
63                         # Use largest image editor
64                         area = None
65                         area_size = 0
66                         for a in context.screen.areas:
67                             if a.type == "IMAGE_EDITOR":
68                                 size = a.width * a.height
69                                 if size > area_size:
70                                     area_size = size
71                                     area = a
72                         if area:
73                             for space in area.spaces:
74                                 if space.type == "IMAGE_EDITOR":
75                                     if active_node.bl_idname == "CompositorNodeViewer":
76                                         space.image = bpy.data.images[
77                                             "Viewer Node"]
78                                     elif active_node.bl_idname in ["CompositorNodeComposite", "CompositorNodeRLayers"]:
79                                         space.image = bpy.data.images[
80                                             "Render Result"]
81                                     elif active_node.image:
82                                         space.image = active_node.image
83                                 break
84                         else:
85                             return {'CANCELLED'}
86
87             return {"FINISHED"}
88         else:
89             return {"PASS_THROUGH"}
90
91
92 def register():
93     bpy.utils.register_class(AMTH_NODE_OT_show_active_node_image)
94     kc = bpy.context.window_manager.keyconfigs.addon
95     km = kc.keymaps.new(name="Node Editor", space_type="NODE_EDITOR")
96     kmi = km.keymap_items.new("node.show_active_node_image",
97                               "LEFTMOUSE", "DOUBLE_CLICK")
98     KEYMAPS.append((km, kmi))
99
100
101 def unregister():
102     bpy.utils.unregister_class(AMTH_NODE_OT_show_active_node_image)
103     for km, kmi in KEYMAPS:
104         km.keymap_items.remove(kmi)
105     KEYMAPS.clear()