Skip to content

Commit 99f7310

Browse files
committed
Adding Ch7 source
1 parent ca74e45 commit 99f7310

9 files changed

+684
-0
lines changed

SourceCode/Ch7/__init__.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
"apply_modifiers",
25+
"create_and_save_images",
26+
"maximize_screen_area",
27+
"split_screen_area",
28+
"unwrap_model",
29+
"uv_settings",
30+
"view_fit"
31+
)

SourceCode/Ch7/apply_modifiers.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
"apply_all_modifiers",
25+
"apply_given_modifier",
26+
)
27+
28+
import bpy
29+
import os, sys
30+
31+
script_dir = ''
32+
if bpy.context.space_data and bpy.context.space_data.text:
33+
script_filepath = bpy.context.space_data.text.filepath
34+
if script_filepath:
35+
script_dir = os.path.dirname(script_filepath)
36+
if not script_dir in sys.path:
37+
sys.path.append(script_dir)
38+
39+
from view_fit import get_context_override
40+
41+
def apply_all_modifiers(context, obj_to_apply_modifier):
42+
for obj in context.view_layer.objects:
43+
obj.select_set(False)
44+
context.view_layer.objects.active = obj_to_apply_modifier
45+
obj_to_apply_modifier.select_set(True)
46+
47+
context_override, override_succesful = get_context_override(context, 'VIEW_3D', 'WINDOW')
48+
if override_succesful:
49+
with bpy.context.temp_override(**context_override):
50+
bpy.ops.object.mode_set(mode='OBJECT')
51+
52+
context_override, override_succesful = get_context_override(context, 'VIEW_3D', 'WINDOW')
53+
if override_succesful:
54+
with bpy.context.temp_override(**context_override):
55+
for m in obj_to_apply_modifier.modifiers:
56+
bpy.ops.object.modifier_apply(modifier=m.name)
57+
58+
def apply_given_modifier(context, obj_to_apply_modifier, modifier_type, modifier_name):
59+
for obj in context.view_layer.objects:
60+
obj.select_set(False)
61+
context.view_layer.objects.active = obj_to_apply_modifier
62+
obj_to_apply_modifier.select_set(True)
63+
64+
context_override, override_succesful = get_context_override(context, 'VIEW_3D', 'WINDOW')
65+
if override_succesful:
66+
with bpy.context.temp_override(**context_override):
67+
bpy.ops.object.mode_set(mode='OBJECT')
68+
69+
context_override, override_succesful = get_context_override(context, 'VIEW_3D', 'WINDOW')
70+
if override_succesful:
71+
with bpy.context.temp_override(**context_override):
72+
for m in obj_to_apply_modifier.modifiers:
73+
if m.type==modifier_type and m.name==modifier_name:
74+
bpy.ops.object.modifier_apply(modifier=m.name)
75+
return
76+
77+
if __name__ == "__main__":
78+
# Apply all modifiers to the current active object.
79+
#apply_all_modifiers(bpy.context, bpy.context.view_layer.objects.active)
80+
81+
if bpy.context.scene.objects.find("test_cube") >= 0:
82+
apply_given_modifier(bpy.context, bpy.context.scene.objects["test_cube"], 'BEVEL', "b")
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
"maximize_screen_area",
25+
)
26+
27+
import bpy
28+
29+
def maximize_screen_area(context, type_of_area_to_maximize):
30+
for window in context.window_manager.windows:
31+
for area in window.screen.areas:
32+
if area.type == type_of_area_to_maximize:
33+
for region in area.regions:
34+
if region.type == 'WINDOW':
35+
# Create a context override (a Python dictionary) containing
36+
# the target area, so the operator call knows which area to
37+
# maximize.
38+
override = {'window': window, 'screen': window.screen, \
39+
'area': area,'region': region}
40+
41+
with bpy.context.temp_override(**override):
42+
bpy.ops.screen.screen_full_area()
43+
break
44+
45+
if __name__ == "__main__":
46+
maximize_screen_area(bpy.context, 'VIEW_3D')
2.21 MB
Loading

SourceCode/Ch7/split_screen_area.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
"split_screen_area",
25+
)
26+
27+
import bpy
28+
29+
def split_screen_area(context, split_dir, split_ratio, type_of_new_area, ui_type_of_new_area, check_existing=False):
30+
existing_areas = list(context.screen.areas)
31+
32+
if check_existing:
33+
for area in existing_areas:
34+
# Found an existing area of matching type
35+
if area.type == type_of_new_area and area.ui_type == ui_type_of_new_area:
36+
return area
37+
38+
bpy.ops.screen.area_split(direction=split_dir, factor=split_ratio)
39+
40+
for area in context.screen.areas:
41+
if area not in existing_areas:
42+
area.type = type_of_new_area
43+
area.ui_type = ui_type_of_new_area
44+
return area
45+
46+
if __name__ == "__main__":
47+
# Split the screen area vertically to create a new UV Editor.
48+
new_uv_editor_vertical = split_screen_area(bpy.context, 'VERTICAL', 0.5, 'IMAGE_EDITOR', 'UV')
49+
50+
# Split the screen area vertically to create a new Image Editor.
51+
new_image_editor_vertical = split_screen_area(bpy.context, 'VERTICAL', 0.5, 'IMAGE_EDITOR', 'IMAGE_EDITOR')
52+
53+
# Split the screen area horizontally to create a new UV editor.
54+
new_uv_editor_horizontal = split_screen_area(bpy.context, 'HORIZONTAL', 0.5, 'IMAGE_EDITOR', 'UV')
55+
56+
# Split the screen area horizontally to create a new Image editor.
57+
new_image_editor_horizontal = split_screen_area(bpy.context, 'HORIZONTAL', 0.5, 'IMAGE_EDITOR', 'IMAGE_EDITOR')

0 commit comments

Comments
 (0)