瀏覽代碼

reorganised scripts

circular17 5 年之前
父節點
當前提交
21567315ef

+ 2 - 2
lazpaintscripts/lazpaint/colors.py

@@ -1,8 +1,8 @@
 import collections
-from lazpaint import command
+from lazpaint import dialog
 
 if __name__ == "__main__":
-  command.show_message("Library defining colors.")
+  dialog.show_message("Library defining colors.")
 
 CustomRGBA = collections.namedtuple("RGBA", "red, green, blue, alpha")
 class RGBA(CustomRGBA):

+ 1 - 5
lazpaintscripts/lazpaint/command.py

@@ -4,6 +4,7 @@ import ast
 print("LazPaint script\t")
 # wait for LazPaint response
 if input('') != chr(27) + 'LazPaint': 
+  print("Needs to be run from LazPaint.")
   exit()
 
 # sends a command to LazPaint
@@ -17,8 +18,3 @@ def send(command, **keywords):
   else:
     return
 
-def show_message(message):
-  send("ShowMessage?", Message=message)
-
-if __name__ == "__main__":
-  show_message("Library to communicate with LazPaint.")

+ 13 - 0
lazpaintscripts/lazpaint/dialog.py

@@ -0,0 +1,13 @@
+import ast
+from lazpaint import command
+
+def show_message(message):
+  # wait for validation before continuing script
+  command.send("ShowMessage?", Message=message)
+
+def input_text(prompt, default=None):
+  return command.send("InputBox?", Prompt=prompt, Default=default)
+
+def input_value(prompt, default):
+  return ast.literal_eval(input_text(prompt, str(default)))
+

+ 17 - 2
lazpaintscripts/lazpaint/image.py

@@ -1,7 +1,7 @@
-from lazpaint import command, colors
+from lazpaint import command, dialog, colors
 
 if __name__ == "__main__":
-  command.show_message("Library to act on the whole image.")
+  dialog.show_message("Library to act on the whole image.")
 
 def new(width, height, color=colors.TRANSPARENT, ignore_modified=False):
   command.send("FileNew", Width=width, Height=height, BackColor=color, IgnoreModified=ignore_modified)
@@ -14,3 +14,18 @@ def get_height():
 
 def get_layer_count():
   return command.send("GetLayerCount?")
+
+def open(file_name=None, ignore_modified=False):
+  command.send("FileOpen", FileName=file_name, IgnoreModified=ignore_modified)
+
+def save(skip_options=False):
+  return command.send("FileSave?", SkipOptions=skip_options)
+
+def save_as(file_name=None, validate=False, overwrite=False, skip_options=False):
+  return command.send("FileSaveAs?", FileName=file_name, Validate=validate, Overwrite=overwrite, SkipOptions=skip_options) 
+
+def reload(ignore_modified=False):
+  command.send("FileReload", IgnoreModified=ignore_modified)
+
+def get_name():
+  return command.send("GetFileName?")

+ 0 - 26
lazpaintscripts/lazpaint/io.py

@@ -1,26 +0,0 @@
-from lazpaint import command
-
-if __name__ == "__main__":
-  command.show_message("Library to load and save files.")
-
-def open(file_name=None, ignore_modified=False):
-  command.send("FileOpen", FileName=file_name, IgnoreModified=ignore_modified)
-
-def save(skip_options=False):
-  return command.send("FileSave?", SkipOptions=skip_options)
-
-def save_as(file_name=None, validate=False, overwrite=False, skip_options=False):
-  return command.send("FileSaveAs?", FileName=file_name, Validate=validate, Overwrite=overwrite, SkipOptions=skip_options) 
-
-def reload(ignore_modified=False):
-  command.send("FileReload", IgnoreModified=ignore_modified)
-
-def load_selection(file_name=None):
-  command.send("FileLoadSelection", FileName=file_name)
-
-def save_selection_as(file_name=None):
-  return command.send("FileSaveSelectionAs?", FileName=file_name)
-
-def get_name():
-  return command.send("GetFileName?")
-

+ 2 - 2
lazpaintscripts/lazpaint/layer.py

@@ -1,7 +1,7 @@
-from lazpaint import command, colors
+from lazpaint import command, dialog, colors
 
 if __name__ == "__main__":
-  command.show_message("Library to access layer content.")
+  dialog.show_message("Library to access layer content.")
 
 DM_DRAW = "dmDrawWithTransparency"
 DM_LINEAR = "dmLinearBlend"

+ 8 - 2
lazpaintscripts/lazpaint/selection.py

@@ -1,7 +1,13 @@
-from lazpaint import command
+from lazpaint import command, dialog
 
 if __name__ == "__main__":
-  command.show_message("Library to access selection.")
+  dialog.show_message("Library to access selection.")
+
+def load(file_name=None):
+  command.send("FileLoadSelection", FileName=file_name)
+
+def save_as(file_name=None):
+  return command.send("FileSaveSelectionAs?", FileName=file_name)
 
 def invert():
   command.send("EditInvertSelection")

+ 16 - 10
lazpaintscripts/test_file.py

@@ -1,25 +1,31 @@
-from lazpaint import image, io, colors, layer, selection, command
+from lazpaint import image, colors, layer, selection, dialog
 
-command.show_message("name is " + str(io.get_name()))
+current_file_name = image.get_name()
+if current_file_name is not None: 
+  dialog.show_message("Filename is \"" + current_file_name + "\"")
+else:
+  dialog.show_message("Image doesn't have a filename")  
 
 if selection.is_mask_empty():
   selection_name = None
+  dialog.show_message("There is no selection mask")
 else: 
-  selection_name = io.save_selection_as("script_test_selection.png")
-  command.show_message("Selection saved")
+  selection_name = selection.save_as("script_test_selection.png")
+  dialog.show_message("Selection saved")
 
 image.new(100, 100, colors.RED)
 
 if selection_name is not None:
-  io.load_selection(selection_name)
-  command.show_message("Selection restored")
+  selection.load(selection_name)
+  dialog.show_message("Selection restored")
 
-file_name = io.save_as("script_test_file.png", skip_options=True)
+wanted_file_name = dialog.input_text("Test file name:", "script_test_file.png")
+file_name = image.save_as(wanted_file_name, skip_options=True)
 image.new(100, 100, colors.LIME)
-io.save_as(file_name, validate=True, overwrite=True, skip_options=True)
+image.save_as(file_name, validate=True, overwrite=True, skip_options=True)
 
 layer.fill(colors.BLUE)
-io.reload(ignore_modified=True)
+image.reload(ignore_modified=True)
 
 layer.fill(colors.PURPLE)
-io.save(skip_options=True)
+image.save(skip_options=True)

+ 5 - 4
lazpaintscripts/test_pixel.py

@@ -1,17 +1,18 @@
-from lazpaint import image, layer, command, colors
+from lazpaint import image, layer, dialog, colors
 
 width = 256
 height = 256
 image.new(width, height)
+red = dialog.input_value("Red value (0..255)", 0)
 image = []
 for y in range(height):
-  scanline = [colors.RGB(0,x,y) for x in range(width)]
+  scanline = [colors.RGB(red,x,y) for x in range(width)]
   image.append(scanline)
 
 layer.put_image(0, 0, image, layer.DM_SET)
 if layer.get_pixel(192,64).green != 192:
-  command.show_message("The value of the pixel is not correct.")
+  dialog.show_message("The value of the pixel is not correct.")
 else:
-  command.show_message("Test successful.")
+  dialog.show_message("Test successful.")