2010-02-12 11:34:25 +00:00
|
|
|
# ##### BEGIN GPL LICENSE BLOCK #####
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software Foundation,
|
2010-02-12 13:34:04 +00:00
|
|
|
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2010-02-12 11:34:25 +00:00
|
|
|
#
|
|
|
|
# ##### END GPL LICENSE BLOCK #####
|
|
|
|
|
|
|
|
# <pep8 compliant>
|
|
|
|
|
|
|
|
import bpy
|
2010-03-07 09:23:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
def image_editor_guess(context):
|
|
|
|
image_editor = context.user_preferences.filepaths.image_editor
|
|
|
|
|
|
|
|
# use image editor in the preferences when available.
|
|
|
|
if not image_editor:
|
|
|
|
import platform
|
|
|
|
system = platform.system()
|
|
|
|
|
|
|
|
if system == 'Windows':
|
2010-03-07 11:41:26 +00:00
|
|
|
image_editor = "start" # not tested!
|
2010-03-07 09:23:57 +00:00
|
|
|
elif system == 'Darwin':
|
|
|
|
image_editor = "open"
|
|
|
|
else:
|
|
|
|
image_editor = "gimp"
|
|
|
|
|
|
|
|
return image_editor
|
2010-03-06 01:40:29 +00:00
|
|
|
|
2010-03-07 02:38:15 +00:00
|
|
|
|
2010-02-12 11:34:25 +00:00
|
|
|
class SaveDirty(bpy.types.Operator):
|
|
|
|
'''Select object matching a naming pattern'''
|
|
|
|
bl_idname = "image.save_dirty"
|
|
|
|
bl_label = "Save Dirty"
|
2010-03-01 00:03:51 +00:00
|
|
|
bl_options = {'REGISTER', 'UNDO'}
|
2010-02-12 11:34:25 +00:00
|
|
|
|
|
|
|
def execute(self, context):
|
|
|
|
unique_paths = set()
|
|
|
|
for image in bpy.data.images:
|
|
|
|
if image.dirty:
|
|
|
|
path = bpy.utils.expandpath(image.filename)
|
|
|
|
if "\\" not in path and "/" not in path:
|
|
|
|
self.report({'WARNING'}, "Invalid path: " + path)
|
|
|
|
elif path in unique_paths:
|
|
|
|
self.report({'WARNING'}, "Path used by more then one image: " + path)
|
|
|
|
else:
|
|
|
|
unique_paths.add(path)
|
2010-02-26 12:28:44 +00:00
|
|
|
image.save()
|
2010-02-12 11:34:25 +00:00
|
|
|
return {'FINISHED'}
|
|
|
|
|
2010-02-14 11:21:21 +00:00
|
|
|
|
2010-03-06 21:47:16 +00:00
|
|
|
class ProjectEdit(bpy.types.Operator):
|
|
|
|
'''Select object matching a naming pattern'''
|
|
|
|
bl_idname = "image.project_edit"
|
|
|
|
bl_label = "Project Edit"
|
|
|
|
bl_options = {'REGISTER'}
|
2010-03-07 09:23:57 +00:00
|
|
|
|
|
|
|
_proj_hack = [""]
|
2010-03-06 21:47:16 +00:00
|
|
|
|
|
|
|
def execute(self, context):
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
EXT = "tga" # until we have a way to save as another format!
|
2010-03-07 09:23:57 +00:00
|
|
|
image_editor = image_editor_guess(context)
|
|
|
|
|
2010-03-06 21:47:16 +00:00
|
|
|
for image in bpy.data.images:
|
|
|
|
image.tag = True
|
|
|
|
|
|
|
|
bpy.ops.paint.image_from_view()
|
|
|
|
|
|
|
|
image_new = None
|
|
|
|
for image in bpy.data.images:
|
|
|
|
if not image.tag:
|
|
|
|
image_new = image
|
|
|
|
break
|
|
|
|
|
|
|
|
if not image_new:
|
|
|
|
self.report({'ERROR'}, "Could not make new image")
|
|
|
|
return {'CANCELLED'}
|
|
|
|
|
|
|
|
filename = os.path.basename(bpy.data.filename)
|
|
|
|
filename = os.path.splitext(filename)[0]
|
|
|
|
|
|
|
|
if filename.startswith("."): # TODO, have a way to check if the file is saved, assuem .B25.blend
|
|
|
|
filename = os.path.join(os.path.dirname(bpy.data.filename), filename)
|
|
|
|
else:
|
|
|
|
filename = "//" + filename
|
|
|
|
|
|
|
|
obj = context.object
|
|
|
|
|
|
|
|
if obj:
|
|
|
|
filename += "_" + bpy.utils.clean_name(obj.name)
|
|
|
|
|
|
|
|
filename_final = filename + "." + EXT
|
|
|
|
i = 0
|
|
|
|
|
|
|
|
while os.path.exists(bpy.utils.expandpath(filename_final)):
|
|
|
|
filename_final = filename + ("%.3d.%s" % (i, EXT))
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
image_new.name = os.path.basename(filename_final)
|
2010-03-07 09:23:57 +00:00
|
|
|
ProjectEdit._proj_hack[0] = image_new.name
|
2010-03-06 21:47:16 +00:00
|
|
|
|
|
|
|
image_new.filename_raw = filename_final # TODO, filename raw is crummy
|
|
|
|
image_new.save()
|
|
|
|
|
2010-03-07 09:23:57 +00:00
|
|
|
subprocess.Popen([image_editor, bpy.utils.expandpath(filename_final)])
|
2010-03-06 21:47:16 +00:00
|
|
|
|
|
|
|
return {'FINISHED'}
|
|
|
|
|
|
|
|
|
|
|
|
class ProjectApply(bpy.types.Operator):
|
|
|
|
'''Select object matching a naming pattern'''
|
|
|
|
bl_idname = "image.project_apply"
|
|
|
|
bl_label = "Project Apply"
|
|
|
|
bl_options = {'REGISTER'}
|
|
|
|
|
|
|
|
def execute(self, context):
|
2010-03-07 09:23:57 +00:00
|
|
|
image_name = ProjectEdit._proj_hack[0] # TODO, deal with this nicer
|
2010-03-06 21:47:16 +00:00
|
|
|
|
|
|
|
try:
|
2010-03-07 09:23:57 +00:00
|
|
|
image = bpy.data.images[image_name]
|
2010-03-06 21:47:16 +00:00
|
|
|
except KeyError:
|
|
|
|
self.report({'ERROR'}, "Could not find image '%s'" % image_name)
|
|
|
|
return {'CANCELLED'}
|
|
|
|
|
|
|
|
image.reload()
|
|
|
|
bpy.ops.paint.project_image(image=image_name)
|
|
|
|
|
|
|
|
return {'FINISHED'}
|
|
|
|
|
|
|
|
|
|
|
|
classes = [
|
|
|
|
SaveDirty,
|
|
|
|
ProjectEdit,
|
|
|
|
ProjectApply]
|
|
|
|
|
|
|
|
|
2010-02-14 11:21:21 +00:00
|
|
|
def register():
|
2010-03-06 21:47:16 +00:00
|
|
|
register = bpy.types.register
|
|
|
|
for cls in classes:
|
|
|
|
register(cls)
|
2010-02-14 11:21:21 +00:00
|
|
|
|
2010-02-22 23:32:58 +00:00
|
|
|
|
2010-02-14 11:21:21 +00:00
|
|
|
def unregister():
|
2010-03-06 21:47:16 +00:00
|
|
|
unregister = bpy.types.unregister
|
|
|
|
for cls in classes:
|
|
|
|
unregister(cls)
|
2010-02-16 09:55:07 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
register()
|