Plugin Configuration | Plugins | Export Filters |
Sketch uses import filters to read vector drawings from files.
For import filter configuration, Sketch uses the following variables:
type
The type must be Import
. This identifier is defined in the
globals() dictionary.
class_name
A string containing the name of a Python class defined in the module. See below for the interface this class has to provide.
rx_magic
A string containing a regular expression suitable for the re module. This regular expression is matched against the first line of the file. if it matches, the import mechanism assumes that the filter understands the file's format and then procedes to use that filter to load the file. So, if possible, this regular expression should match those files the filter can handle and only those.
While this method of identifying files is not really sufficient to handle all file types, it works well enough for now. In the future we will probably have to extend this mechanism.
tk_file_type
A tuple containing two elements. The first is the name of the file format presented to the user in the file open dialog. The second element is eithe a string or a tuple of strings. Each of the strings defines a file name extension used for that filename.
Examples:
#tk_file_type = ('Sketch Document', '.sk')
#tk_file_type = ('PostScript file', ('.ps', '.eps'))
unload
(optional)A boolean. True means that the filter module should be unloaded after use, false means that it shouldn't be unloaded. If the module is unloaded it will be imported again the next time the filter is needed. Infrequently used filters should set this to true, frequently used filters (like the filter for Sketch's own format) should set it to true.
If omitted, it defaults to false.
It's a good idea to set this variable to true at least while developing a filter because that way the filter will be automatically reloaded when the source file changes.
format_name
The name of the format as a string. This name is currently used internally to find out whether a document was loaded from a file in Sketch's format or from a different format (in the latter case `Save' is treated like `Save As...', i.e. the user has to provide a filename).
standard_messages
(optional)A boolean indicating whether the messages in the plugin are standard messages that are defined in Sketch's message catalogs. For third-party plugins this should be omitted.
The interface a loader class presents to the document loading mechanism is quite simple. The loading mechanism works roughly as follows:
To make things easier, Sketch defines some base classes that plugins can inherit, LoaderWithComposites and, derived from that, GenericLoader and SimplifiedLoader, derived in turn from GenericLoader.
The class GenericLoader provides a default implementation of the constructor, a mechanism for warning and error messages and methods to easily create standard objects like rectangles, ellipses and groups.
GenericLoader defines these methods:
Assign the arguments to self.file
, self.filename
and
self.match
respectively.
Start the construction of a document. This method must be called before any other of the object creation methods are called.
Start a new layer. This method must be called after calling document and before any of the other object creation methods.
name is the name of the layer. If omitted, it defaults to "Layer 1".
Start a new group. Groups can be arbitrarily nested. Each call to begin_group must have a corresponding call to end_group.
End the current group.
Create a new rectangle object and append it to the current compound object.
The first six positional parameters (m11, m21, m12, m22, v1, v2) define a transformation matrix that transforms the unit square (opposite corners (0,0) and (1,1)) onto the rectangle.
radius1 and radius2 define the rounded corners.
For more details see the documentation for the class Rectangle
Create a new ellipse object and append it to the current compound object.
The first six positional parameters (m11, m21, m12, m22, v1, v2) define a transformation matrix that transforms the unit circle (center (0,0) and radius 1) onto the ellipse.
start_angle and end_angle, if given, define the start end end angles for pie slices, arc and chords.
arc_type defines whether and how the arc of an incomplete
ellipse is closed. Valid values are ArcArc
,
ArcPieSlice
and ArcChord
. These constants are defined
in Sketch.const
For more details see the documentation for the class Ellipse
Create a new bézier object in the current compound object.
paths must be a tuple of curve objects.
Plugin Configuration | Plugins | Export Filters |