4 Gimp plugin "Hexaflexagon"
8 Copyright (C) 2018 Antonio Ospite <ao2@ao2.it>
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 # The plugin is inspired to flexagon.scm by Andrea Rossetti:
25 # http://andrear.altervista.org/home/gimp_flexagon.php
27 # It has been rewritten in python in the hope to simplify it and attract more
32 from diagram.gimp_diagram import GimpDiagram
33 from flexagon import HexaflexagonDiagram
35 gettext.install("gimp20-python", gimp.locale_directory, unicode=True)
38 def build_plan(template_layer, hexaflexagon_layer, diagram):
39 for hexagon in diagram.hexaflexagon.hexagons:
40 for triangle in hexagon.triangles:
41 verts = diagram.get_triangle_verts(triangle)
43 matrix = diagram.get_triangle_transform(triangle)
44 diagram.backend.copy_polygon(template_layer, verts, hexaflexagon_layer, matrix)
47 def hexaflexagon_main(image):
48 x_border = image.width / 50
49 font_size = image.width / 80
50 stroke_width = image.width / 480
52 template_layer_name = "HexaflexagonTemplate"
53 content_layer_name = "Hexagons"
54 hexaflexagon_layer_name = "Hexaflexagon"
56 message = "Draw the hexagons content into the '%s' layer.\n" % content_layer_name
57 message += "Then call this script again."
59 pdb.gimp_image_undo_group_start(image)
60 pdb.gimp_context_push()
61 pdb.gimp_context_set_defaults()
63 template_layer = pdb.gimp_image_get_layer_by_name(image,
66 content_layer = pdb.gimp_image_get_layer_by_name(image,
69 content_layer = pdb.gimp_layer_new(image, image.width, image.height,
70 RGBA_IMAGE, content_layer_name,
73 template_layer_position = pdb.gimp_image_get_item_position(image, template_layer)
74 content_layer_position = template_layer_position - 1
75 pdb.gimp_image_insert_layer(image, content_layer, None, content_layer_position)
77 pdb.gimp_message(message)
79 pdb.gimp_context_pop()
80 pdb.gimp_image_undo_group_end(image)
83 content_layer_position = -1
84 pdb.gimp_image_insert_layer(image, content_layer, None, content_layer_position)
86 if not template_layer:
87 template_layer = pdb.gimp_layer_new(image, image.width, image.height,
88 RGBA_IMAGE, template_layer_name,
90 pdb.gimp_image_insert_layer(image, template_layer, None, -1)
92 gimp_backend = GimpDiagram(image.width, image.height,
93 image, template_layer,
94 font_size=font_size, stroke_width=stroke_width)
95 diagram = HexaflexagonDiagram(x_border, backend=gimp_backend)
96 diagram.draw_template()
98 pdb.gimp_message(message)
99 pdb.gimp_context_pop()
100 pdb.gimp_image_undo_group_end(image)
103 hexaflexagon_layer = pdb.gimp_image_get_layer_by_name(image,
104 hexaflexagon_layer_name)
105 if hexaflexagon_layer:
106 pdb.gimp_message("There is already a generated hexaflexagon.")
107 pdb.gimp_context_pop()
108 pdb.gimp_image_undo_group_end(image)
111 hexaflexagon_layer = pdb.gimp_layer_new(image, image.width,
112 image.height, RGBA_IMAGE,
113 hexaflexagon_layer_name, 100,
115 pdb.gimp_image_insert_layer(image, hexaflexagon_layer, None, -1)
117 gimp_backend = GimpDiagram(image.width, image.height,
118 image, content_layer,
119 font_size=font_size, stroke_width=stroke_width)
120 diagram = HexaflexagonDiagram(x_border, backend=gimp_backend)
121 build_plan(content_layer, hexaflexagon_layer, diagram)
123 pdb.gimp_context_pop()
124 pdb.gimp_image_undo_group_end(image)
127 if __name__ == "__main__":
129 "python_fu_hexaflexagon",
130 N_("Create Hexaflexagons"),
131 "Create Hexaflexagons",
132 "Antonio Ospite <ao2@ao2.it>",
133 "Copyright (C) 2018 Antonio Ospite <ao2@ao2.it>",
135 N_("Hexaflexagon..."),
138 (PF_IMAGE, "image", "Input image", None),
142 menu="<Image>/Filters/Render",
143 domain=("gimp20-python", gimp.locale_directory)