fix [#29063] reprojetion painting : failed to create opengl offscreen buffer
[blender.git] / release / scripts / startup / bl_operators / image.py
1 # ##### BEGIN GPL LICENSE BLOCK #####
2 #
3 #  This program is free software; you can redistribute it and/or
4 #  modify it under the terms of the GNU General Public License
5 #  as published by the Free Software Foundation; either version 2
6 #  of the License, or (at your option) any later version.
7 #
8 #  This program is distributed in the hope that it will be useful,
9 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #  GNU General Public License for more details.
12 #
13 #  You should have received a copy of the GNU General Public License
14 #  along with this program; if not, write to the Free Software Foundation,
15 #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 # ##### END GPL LICENSE BLOCK #####
18
19 # <pep8-80 compliant>
20
21 import bpy
22 from bpy.types import Operator
23 from bpy.props import StringProperty
24
25
26 class EditExternally(Operator):
27     '''Edit image in an external application'''
28     bl_idname = "image.external_edit"
29     bl_label = "Image Edit Externally"
30     bl_options = {'REGISTER'}
31
32     filepath = StringProperty(
33             name="File Path",
34             description="Path to an image file",
35             maxlen=1024,
36             )
37
38     def _editor_guess(self, context):
39         import sys
40
41         image_editor = context.user_preferences.filepaths.image_editor
42
43         # use image editor in the preferences when available.
44         if not image_editor:
45             if sys.platform[:3] == "win":
46                 image_editor = ["start"]  # not tested!
47             elif sys.platform == "darwin":
48                 image_editor = ["open"]
49             else:
50                 image_editor = ["gimp"]
51         else:
52             if sys.platform == "darwin":
53                 # blender file selector treats .app as a folder
54                 # and will include a trailing backslash, so we strip it.
55                 image_editor.rstrip('\\')
56                 image_editor = ["open", "-a", image_editor]
57             else:
58                 image_editor = [image_editor]
59
60         return image_editor
61
62     def execute(self, context):
63         import os
64         import subprocess
65
66         filepath = self.filepath
67
68         if not filepath:
69             self.report({'ERROR'}, "Image path not set")
70             return {'CANCELLED'}
71
72         if not os.path.exists(filepath):
73             self.report({'ERROR'},
74                         "Image path %r not found, image may be packed or "
75                         "unsaved" % filepath)
76             return {'CANCELLED'}
77
78         cmd = self._editor_guess(context) + [filepath]
79
80         try:
81             subprocess.Popen(cmd)
82         except:
83             import traceback
84             traceback.print_exc()
85             self.report({'ERROR'},
86                         "Image editor not found, "
87                         "please specify in User Preferences > File")
88
89             return {'CANCELLED'}
90
91         return {'FINISHED'}
92
93     def invoke(self, context, event):
94         import os
95         try:
96             image = context.space_data.image
97         except AttributeError:
98             self.report({'ERROR'}, "Context incorrect, image not found")
99             return {'CANCELLED'}
100
101         filepath = bpy.path.abspath(image.filepath, library=image.library)
102
103         self.filepath = os.path.normpath(filepath)
104         self.execute(context)
105
106         return {'FINISHED'}
107
108
109 class SaveDirty(Operator):
110     """Save all modified textures"""
111     bl_idname = "image.save_dirty"
112     bl_label = "Save Dirty"
113     bl_options = {'REGISTER', 'UNDO'}
114
115     def execute(self, context):
116         unique_paths = set()
117         for image in bpy.data.images:
118             if image.is_dirty:
119                 filepath = bpy.path.abspath(image.filepath)
120                 if "\\" not in filepath and "/" not in filepath:
121                     self.report({'WARNING'}, "Invalid path: " + filepath)
122                 elif filepath in unique_paths:
123                     self.report({'WARNING'},
124                                 "Path used by more then one image: %r" %
125                                 filepath)
126                 else:
127                     unique_paths.add(filepath)
128                     image.save()
129         return {'FINISHED'}
130
131
132 class ProjectEdit(Operator):
133     """Edit a snapshot of the viewport in an external image editor"""
134     bl_idname = "image.project_edit"
135     bl_label = "Project Edit"
136     bl_options = {'REGISTER'}
137
138     _proj_hack = [""]
139
140     def execute(self, context):
141         import os
142
143         EXT = "png"  # could be made an option but for now ok
144
145         for image in bpy.data.images:
146             image.tag = True
147
148         # opengl buffer may fail, we can't help this, but best report it.
149         try:
150             ret = bpy.ops.paint.image_from_view()
151         except RuntimeError as err:
152             self.report({'ERROR'}, str(err))
153             return {'CANCELLED'}
154
155         image_new = None
156         for image in bpy.data.images:
157             if not image.tag:
158                 image_new = image
159                 break
160
161         if not image_new:
162             self.report({'ERROR'}, "Could not make new image")
163             return {'CANCELLED'}
164
165         filepath = os.path.basename(bpy.data.filepath)
166         filepath = os.path.splitext(filepath)[0]
167         # fixes <memory> rubbish, needs checking
168         # filepath = bpy.path.clean_name(filepath)
169
170         if bpy.data.is_saved:
171             filepath = "//" + filepath
172         else:
173             tmpdir = context.user_preferences.filepaths.temporary_directory
174             filepath = os.path.join(tmpdir, "project_edit")
175
176         obj = context.object
177
178         if obj:
179             filepath += "_" + bpy.path.clean_name(obj.name)
180
181         filepath_final = filepath + "." + EXT
182         i = 0
183
184         while os.path.exists(bpy.path.abspath(filepath_final)):
185             filepath_final = filepath + ("%.3d.%s" % (i, EXT))
186             i += 1
187
188         image_new.name = bpy.path.basename(filepath_final)
189         ProjectEdit._proj_hack[0] = image_new.name
190
191         image_new.filepath_raw = filepath_final  # TODO, filepath raw is crummy
192         image_new.file_format = 'PNG'
193         image_new.save()
194
195         try:
196             bpy.ops.image.external_edit(filepath=filepath_final)
197         except RuntimeError as re:
198             self.report({'ERROR'}, str(re))
199
200         return {'FINISHED'}
201
202
203 class ProjectApply(Operator):
204     """Project edited image back onto the object"""
205     bl_idname = "image.project_apply"
206     bl_label = "Project Apply"
207     bl_options = {'REGISTER'}
208
209     def execute(self, context):
210         image_name = ProjectEdit._proj_hack[0]  # TODO, deal with this nicer
211
212         try:
213             image = bpy.data.images[image_name]
214         except KeyError:
215             import traceback
216             traceback.print_exc()
217             self.report({'ERROR'}, "Could not find image '%s'" % image_name)
218             return {'CANCELLED'}
219
220         image.reload()
221         bpy.ops.paint.project_image(image=image_name)
222
223         return {'FINISHED'}