Skip to content

Commit

Permalink
palette_creator
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-brajer committed May 6, 2020
1 parent 9d7be52 commit a0ac218
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"cSpell.words": [
"cardassembler"
],
"python.pythonPath": "C:\\Program Files\\GIMP 2\\bin\\python.exe"
"python.pythonPath": "C:\\Program Files\\GIMP 2\\bin\\python.exe",
"codemap.maxNestingLevel": 3,
}
36 changes: 26 additions & 10 deletions CardAssembler_Data/CardAssembler_Definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""
# ---IMPORTS---
import xml.etree.ElementTree as ET
import operator

# ---CONSTANTS---

Expand All @@ -15,10 +16,9 @@

def main():
""" Testing area. """
import os.path

blueprint = Blueprint(os.path.dirname(__file__) + '/Blueprint.xml')
layout = blueprint.generate_layout_dict("unique item example")
palette = blueprint.generate_palette("color")
pass


Expand Down Expand Up @@ -136,18 +136,34 @@ def _nextDataset(self, nextSteps):

return data

# def generate_palette(self, name):
# """ Make palette out of used colors. Then import it into Gimp.
def generate_palette(self, startBy):
""" Make palette out of used colors.
To be used in another mini plug-in to import palette into Gimp.
"""
palette = self._harvest_leaves(self._nextDataset(startBy))
palette.sort()
palette.sort(key=lambda x: x[0].count(' '))
return palette

# To be used in another mini plug-in.
# """
# palette = []
# for item in self.data['color']:
# pass
def _harvest_leaves(self, colorDict):
""" X """
palette = []
for key, value in colorDict.items():
if isinstance(value, dict):
subpalette = self._harvest_leaves(value)
for subKey, subValue in subpalette:
palette.append(
(key + (" " if subKey else "") + subKey, subValue))

# return palette
elif key == 'color':
palette.append(("", value))

return palette


# ---MAIN---
if __name__ == "__main__":

import os.path
main()
80 changes: 69 additions & 11 deletions card-assembler.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

# ---FUNCTIONS---
def default_data_folder():
""" Gimp in user folder. """
""" Gimp's folder in user folder. """
dataFolder = os.path.expanduser('~')
# Rest of the Gimp plug-in path contains gimp version
# and therefore can change. Any folder can be chosen.
Expand All @@ -26,7 +26,37 @@ def default_data_folder():


def card_creator(dataFolder, xmlFile, cardIDs, save):
""" Registered function by GF.register. I.e. Main(). """
""" Creates board-game cards.
Registered function by GF.register. Main functionality.
"""
toolbox = initialize_toolbox(dataFolder, xmlFile)

if not cardIDs:
raise ValueError('No card IDs inserted!')

for cardID in cardIDs.split('\n'):
toolbox.create_image(cardID)
toolbox.display_image()
if save:
toolbox.save_image()


def palette_creator(dataFolder, xmlFile, paletteID, name):
""" Creates palette.
Registered function by GF.register. Supplemental functionality.
"""
toolbox = initialize_toolbox(dataFolder, xmlFile)

if not paletteID:
raise ValueError('No palette ID inserted!')

toolbox.create_palette(paletteID, name)


def initialize_toolbox(dataFolder, xmlFile):
""" Common initialization used by all registered functions. """
# This weird import is forced by Gimp running this script through
# eval(...) function inside its installation folder and direct
# import from different folder raises 'access denied' error.
Expand All @@ -38,17 +68,12 @@ def card_creator(dataFolder, xmlFile, cardIDs, save):
xmlFile,
blueprintClass=cardassembler_definitions.Blueprint)

if not cardIDs:
raise ValueError('No card IDs inserted!')

for cardID in cardIDs.split('\n'):
toolbox.create_image(cardID)
toolbox.display_image()
if save:
toolbox.save_image()
return toolbox


# ---CLASSES---


class Toolbox(object):
""" Blueprint-to-image manipulation tool.
Expand Down Expand Up @@ -231,8 +256,41 @@ def save_image(self):
filename = directory + GF.pdb.gimp_image_get_name(self.image) + '.xcf'
GF.pdb.gimp_xcf_save(0, self.image, None, filename, filename)

def create_palette(self, paletteID, name):
"""Blueprint to palette.
Colors are sorted by their branch hight and then alphabetically.
"""
palette = GF.pdb.gimp_palette_new(name)
GF.pdb.gimp_palette_set_columns(palette, 1)

for name, color in self.blueprint.generate_palette(paletteID):
GF.pdb.gimp_palette_add_entry(palette, name, color)


# ---REGISTER---

GF.register(
"MB_palette_assembler", # Name registered in Procedure Browser (blurb).
# Widget title (proc_name).
"Creates palette.\n\nGimp plug-in folder:\n<user>/AppData/Roaming/GIMP/<version>/plug-ins/",
"Creates palette", # Help.
"Martin Brajer", # Author.
"Martin Brajer", # Copyright holder.
"May 2020", # Date.
"Palette Assembler", # Menu Entry (label).
"", # Image Type - no image required (imagetypes).
[ # (params).
(GF.PF_DIRNAME, "dataFolder", "Data folder:", default_data_folder()),
(GF.PF_STRING, "xmlFile", "XML file:", "Blueprint.xml"),
(GF.PF_TEXT, "paletteID", "Palette ID:", "color"),
(GF.PF_TEXT, "name", "Name:", "Card Assembler Palette"),
],
[], # (results).
palette_creator, # Matches to name of function being defined (function).
menu="<Image>/Card Assembler" # Menu item location.
)

GF.register(
"MB_card_assembler", # Name registered in Procedure Browser (blurb).
# Widget title (proc_name).
Expand All @@ -252,6 +310,6 @@ def save_image(self):
[], # (results).
card_creator, # Matches to name of function being defined (function).
menu="<Image>/Card Assembler" # Menu item location.
) # End register.
)

GF.main()

0 comments on commit a0ac218

Please sign in to comment.