|
| 1 | +# ##### BEGIN GPL LICENSE BLOCK ##### |
| 2 | +# |
| 3 | +# GNU GPLv3, 29 June 2007 |
| 4 | +# |
| 5 | +# Examples from Ch7 of the book "Blender Scripting with Python" by Isabel Lupiani. |
| 6 | +# Copyright (C) 2024 Isabel Lupiani, Apress. |
| 7 | +# |
| 8 | +# This program is free software: you can redistribute it and/or modify |
| 9 | +# it under the terms of the GNU General Public License as published by |
| 10 | +# the Free Software Foundation, either version 3 of the License, or |
| 11 | +# (at your option) any later version. |
| 12 | +# |
| 13 | +# This program is distributed in the hope that it will be useful, |
| 14 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +# GNU General Public License for more details. |
| 17 | +# |
| 18 | +# You should have received a copy of the GNU General Public License |
| 19 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 20 | +# |
| 21 | +# ##### END GPL LICENSE BLOCK ##### |
| 22 | + |
| 23 | +__all__ = ( |
| 24 | + "create_image_data_block", |
| 25 | + "save_image_to_file" |
| 26 | + ) |
| 27 | + |
| 28 | +import bpy |
| 29 | +import sys |
| 30 | +import os |
| 31 | + |
| 32 | +script_dir = '' |
| 33 | +if bpy.context.space_data and bpy.context.space_data.text: |
| 34 | + script_filepath = bpy.context.space_data.text.filepath |
| 35 | + if script_filepath: |
| 36 | + script_dir = os.path.dirname(script_filepath) |
| 37 | + if not script_dir in sys.path: |
| 38 | + sys.path.append(script_dir) |
| 39 | + |
| 40 | +from split_screen_area import split_screen_area |
| 41 | +from uv_settings import get_image_editor, get_uv_editor |
| 42 | +from view_fit import get_context_override |
| 43 | + |
| 44 | +def create_image_data_block(context, name, image_type='UV_GRID', color=(0, 0, 0, 1), image_filepath='', display_image=True, area_ui_type='IMAGE_EDITOR'): |
| 45 | + image_block = None |
| 46 | + if bpy.data.images.find(name) < 0 or not bpy.data.images[name].has_data: |
| 47 | + if os.path.isfile(image_filepath): |
| 48 | + image_block = bpy.data.images.load(image_filepath) |
| 49 | + image_block.name = name |
| 50 | + else: |
| 51 | + bpy.data.images.new(name=name, width=1024, height=1024, alpha=True, float_buffer=False, stereo3d=False) |
| 52 | + image_block = bpy.data.images[name] |
| 53 | + else: |
| 54 | + image_block = bpy.data.images[name] |
| 55 | + |
| 56 | + image_block.generated_color = color |
| 57 | + image_block.generated_type = image_type |
| 58 | + |
| 59 | + if not display_image: |
| 60 | + return image_block, None |
| 61 | + |
| 62 | + [image_editor_area, image_editor_space, uv_editor] = [None, None, None] |
| 63 | + if area_ui_type=='IMAGE_EDITOR': |
| 64 | + [image_editor_area, image_editor_space] = get_image_editor(context, name) |
| 65 | + else: |
| 66 | + [image_editor_area, image_editor_space, uv_editor] = get_uv_editor(context, name) |
| 67 | + |
| 68 | + if image_editor_space: |
| 69 | + image_editor_space.image = image_block |
| 70 | + else: |
| 71 | + image_editor_area = split_screen_area(context, 'VERTICAL', 0.5, 'IMAGE_EDITOR', area_ui_type, False) |
| 72 | + for s in image_editor_area.spaces: |
| 73 | + if s.type == 'IMAGE_EDITOR': |
| 74 | + image_editor_space = s |
| 75 | + image_editor_space.image = image_block |
| 76 | + if area_ui_type == 'UV': |
| 77 | + uv_editor = s.uv_editor |
| 78 | + break |
| 79 | + |
| 80 | + image_editor_context_override, override_successful = get_context_override(context, 'IMAGE_EDITOR', 'WINDOW', image_editor_area.x, name) |
| 81 | + if override_successful: |
| 82 | + with bpy.context.temp_override(**image_editor_context_override): |
| 83 | + bpy.ops.image.view_all(fit_view=True) |
| 84 | + |
| 85 | + return image_block, uv_editor |
| 86 | + |
| 87 | +def save_image_to_file(context, name, dirpath): |
| 88 | + if bpy.data.images.find(name) < 0: |
| 89 | + print("No image by the name " + name + " exists.") |
| 90 | + return |
| 91 | + |
| 92 | + image = bpy.data.images[name] |
| 93 | + image.filepath_raw = dirpath + '\\' + name + '.png' |
| 94 | + image.file_format = 'PNG' |
| 95 | + image.save() |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + create_image_data_block(bpy.context, "test_image_block_uv_grid", image_type='UV_GRID', color=(0, 0, 0, 1), \ |
| 99 | + image_filepath='', display_image=True, area_ui_type='UV') |
| 100 | + save_image_to_file(bpy.context, "test_image_block_uv_grid", script_dir) |
| 101 | + |
| 102 | + create_image_data_block(bpy.context, "test_image_block_from_file", image_type='BLANK', color = (0, 0, 0, 1), \ |
| 103 | + image_filepath = os.path.join(script_dir, "modeling_reference_front.jpg"), display_image = True, area_ui_type='IMAGE_EDITOR') |
| 104 | + save_image_to_file(bpy.context, "test_image_block_from_file", script_dir) |
0 commit comments