Inkscape.org
Creating New Extensions Responsive Grid Creator
  1. #1
    Tatudesigner Tatudesigner @tatudesigner
    *

    Hello everyone,

    I’m trying to develop an extension for Inkscape that creates responsive grids, similar to what can be done in FIGMA. I’m not a developer—just a designer looking to create a feature that could be useful for other designers. So far, I’ve used ChatGPT to achieve the current results. The extension works, but it needs a few adjustments:

    1. Whenever I change a parameter with the preview enabled, a dialog box pops up, requiring me to confirm the action to update the preview. This is quite inconvenient.
    2. For some reason, if I choose any page format other than A4, making changes to the parameters (with the preview enabled) reverts my page back to the A4 format.
    3. I’d like to add a parameter to allow users to choose the grid’s color and opacity.

    No matter how hard I try, I can’t seem to solve these issues. Could someone please help me?

    #!/usr/bin/env python3
    
    # coding=utf-8
    
    """
    Inkscape's responsive grid extension. Creates a grid of red rectangles
    with specified columns, column width, and gutter, adjusted to the active page size.
    """
    
    import inkex
    from lxml import etree
    
    
    class ResponsiveGrid(inkex.GenerateExtension):
        """
        Responsive Grid extension for Inkscape
        """
    
        def add_arguments(self, pars):
            pars.add_argument("--columns", type=int, default=12, help="Number of columns")
            pars.add_argument("--column_width", type=float, default=50, help="Width of each column (px)")
            pars.add_argument("--gutter", type=float, default=20, help="Gutter width (px)")
    
        def generate(self):
            # Get dimensions of the active page
            svg_root = self.document.getroot()
            canvas_width = float(svg_root.get("width", "0").replace("mm", "").replace("px", ""))
            canvas_height = float(svg_root.get("height", "0").replace("mm", "").replace("px", ""))
    
            # Get user parameters
            columns = self.options.columns
            column_width = self.options.column_width
            gutter = self.options.gutter
    
            # Validate parameters
            if columns < 1 or column_width <= 0 or gutter < 0:
                return  # Exit without generating anything if parameters are invalid
    
            # Calculate total width occupied by columns and gutters
            total_width = (column_width * columns) + (gutter * (columns - 1))
            margin = (canvas_width - total_width) / 2
    
            # Register namespaces explicitly
            namespaces = {**svg_root.nsmap, 'inkscape': inkex.NSS['inkscape']}
    
            # Remove existing grid group (if any)
            existing_grid = svg_root.find(".//svg:g[@inkscape:label='Responsive Grid']", namespaces)
            if existing_grid is not None:
                svg_root.remove(existing_grid)
    
            # Create a group for the columns
            grid_group = etree.Element(inkex.addNS("g", "svg"))
            grid_group.set(inkex.addNS("label", "inkscape"), "Responsive Grid")
            grid_group.set(inkex.addNS("groupmode", "inkscape"), "layer")
            grid_group.set(inkex.addNS("locked", "inkscape"), "true")
    
            # Draw the columns
            x = margin
            for _ in range(columns):
                column = etree.Element(inkex.addNS("rect", "svg"))
                column.set("x", str(x))
                column.set("y", "0")
                column.set("width", str(column_width))
                column.set("height", str(canvas_height))
                column.set("style", "fill:red;fill-opacity:0.1;stroke:none")
                grid_group.append(column)
                x += column_width + gutter
    
            # Add the group to the SVG document
            svg_root.append(grid_group)
    
    
    if __name__ == "__main__":
        ResponsiveGrid().run()
    

     

    <?xml version="1.0" encoding="UTF-8"?>
    <inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
      <!-- Extension name and ID -->
      <name>Responsive Grid Creator</name>
      <id>org.inkscape.render.responsive_grid</id>
    
      <!-- Extension parameters -->
      <param name="columns" type="int" gui-text="Number of Columns:" min="1" max="50" default="12" />
      <param name="column_width" type="float" gui-text="Column Width (px):" min="1" max="1000" default="50" />
      <param name="gutter" type="float" gui-text="Gutter Width (px):" min="0" max="500" default="20" />
    
      <!-- Menu and location -->
      <effect>
        <object-type>all</object-type>
        <effects-menu>
          <submenu name="Render">
            <submenu name="Responsive Grid" />
          </submenu>
        </effects-menu>
      </effect>
    
      <!-- Associated Python script -->
      <script>
        <command location="inx" interpreter="python">responsive_grid.py</command>
      </script>
    </inkscape-extension>