Prev Up Next
Plugin Configuration Plugins Export Filters

Import Filters

Sketch uses import filters to read vector drawings from files.

Configuration Variables

For import filter configuration, Sketch uses the following variables:


The type must be Import. This identifier is defined in the globals() dictionary.


A string containing the name of a Python class defined in the module. See below for the interface this class has to provide.


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.


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.


#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.


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 Class Interface

External Interface

The interface a loader class presents to the document loading mechanism is quite simple. The loading mechanism works roughly as follows:

  1. Read the first line of the file
  2. Match it in turn against all the regular expressions defined for import filter plugins (defined by the rx_magic variable).
  3. If a match is found, instantiate the plugin's class (defined by the class_name variable) with three positional parameters file, filename and match. file is the open input file object, positioned just after the first line. filename is the name of the file. match is the match-object returned from regular expression match. If possible, filters should read the input file sequentially and not use seeks. Sketch automatically pipes gzipped files through gzip and in that case the file object is actually a pipe, so seeks would be impossible. If a filter needs to access the first line it can do so by using match.string or by defing suitable groups in the regular expression.
  4. Call the instance's Load() method (without arguments). This method does the actual work and returns an instance of the document class.
  5. Call the instance's Messages() method without arguments. The return value is expected to be a list of strings with messages that are usually presented to the user by the UI.
  6. If the unload configuration variable is true, unload the plugin's module.

Common Base Classes

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:

__init__(file, filename, match)

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.

rectangle(m11, m21, m12, m22, v1, v2[, radius1 = 0][, radius2 = 0])

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

ellipse(m11, m21, m12, m22, v1, v2[, start_angle = 0.0][, end_angle = 0.0][, arc_type = ArcPieSlice])

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
Prev Up Next