Inkscape.org
Creating New Extensions Extension from a .py file
  1. #1
    meetdilip meetdilip @meetdilip

    Hi, I am trying to create an extension to duplicate the page. The Python file receives the page index, selects all objects of the page, copy, paste " on page " on the target page (using the page index).

    I am trying to do this using inkex. I do not know how to connect the .py file to Inkscape though. Any help will be great.

  2. #2
    meetdilip meetdilip @meetdilip
    *

    Hi, I managed to create an .inx file and I can see my Extension under Extension. But it is greyed out

    Am I doing something wrong here?

     

    <?xml version="1.0" encoding="UTF-8"?>
    <inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">

    <!-- 1. Replace this with the title of your extension. This will Show in the extensions menu-->
    <_name>Replicate</_name>

    <!-- 2. Fill this in. It ensures that your extension has a unique ID -->
    <id>com.dilip.replicate</id>

    <!-- 3. Show the relative path to your extension. For example, `~/.config/inkscape/extensions/template.py` would be `template.py`-->
    <dependency type="executable" location="extensions">/home/user/.var/app/org.inkscape.Inkscape/config/inkscape/extensions/Replicate/replicate.py</dependency>
    <dependency type="executable" location="extensions">inkex.py</dependency>

    <effect>
        <object-type>all</object-type>
        <effects-menu>

        <!-- 4. Where is your extension located? (You can nest menus) -->
        <submenu _name="submenu"/>

        </effects-menu>
    </effect>
    <script>

        <!-- 5. Same as #4 -->
        <command reldir="extensions" interpreter="python">/home/user/.var/app/org.inkscape.Inkscape/config/inkscape/extensions/Replicate/replicate.py</command>

    </script>
    </inkscape-extension>

     

     

  3. #3
    inklinea inklinea @inklinea⛰️

    Have a look at the example .inx on this page https://inkscape.gitlab.io/extensions/documentation/tutorial/my-first-text-extension.html

    It's probably better to just put your extension in the normal extension folder to start with.

    so on Ubuntu ( or similar )

    /home/USER/.config/inkscape/extensions/

    put the .inx and .py in its own subfolder to prevent it getting lost :)

  4. #4
    meetdilip meetdilip @meetdilip

    Hi, thanks. I was using path, changing to simple replicate.py solved the issue. Now I need to fix my code. It is not reading the number of pages. :(

  5. #5
    meetdilip meetdilip @meetdilip

    Traceback (most recent call last):
      File "/home/user/.var/app/org.inkscape.Inkscape/config/inkscape/extensions/Replicate/replicate.py", line 32, in <module>
        main()
      File "/home/user/.var/app/org.inkscape.Inkscape/config/inkscape/extensions/Replicate/replicate.py", line 19, in main
        doc = inkex.document.get_root()
              ^^^^^^^^^^^^^^
    AttributeError: module 'inkex' has no attribute 'document'

     

     

  6. #6
    meetdilip meetdilip @meetdilip

    This is what I have

     

     

    import inkex

    def select_and_copy(page_index):
      # Select all objects on the given page
      inkex.selection.set_path(inkex.xpath.get_descendants(inkex.document.get_root(), "svg:g[@id='layer*'][@inkscape:page='%d']" % page_index))

      # Copy selected objects
      inkex.selection.copy()

    def paste_on_page(page_index):
      # Select the last page
      inkex.selection.set_path(inkex.xpath.get_element_by_id(inkex.document.get_root(), "svg:g[@id='layer*'][@inkscape:page='%d']" % (page_index + 1)))

      # Paste copied objects
      inkex.selection.paste()

    def main():
      # Get document and number of pages
      doc = inkex.document.get_root()
      num_pages = len(inkex.xpath.getelembytagname(doc, "svg:g", {"inkscape:page": "*"}))

      # Select and copy from second-to-last page
      select_and_copy(num_pages - 2)

      # Paste on last page
      paste_on_page(num_pages - 1)

      # Apply changes to document
      inkex.effect.apply(inkex.Effect("internal", "**.select-all**"))

    if __name__ == "__main__":
      main()

  7. #7
    meetdilip meetdilip @meetdilip

    Is there any way to copy everything in page 2 to page 3? I will be able to manually change the page numbers and then be able to duplicate pages at will. Thanks

  8. #8
    inklinea inklinea @inklinea⛰️

    There are a couple of extensions I have written you could look at

    Create an array of new blank pages:

    https://gitlab.com/inklinea/page_array

    Duplicate or clone content selected on page 1 across pages.

    https://gitlab.com/inklinea/page-watermark

    Page watermark does not handle 2D page arrays ( I do have a version that does that, but not released yet )

     

  9. #9
    meetdilip meetdilip @meetdilip

    Thanks, checking now :)