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.
<!-- 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>
<!-- 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>
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'
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**"))
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
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.
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>
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 :)
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. :(
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'
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()
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
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 )
Thanks, checking now :)