Inkscape.org
Creating New Extensions use save as function in an extension
  1. #1
    FrankI4466 FrankI4466 @FrankI4466

    Hi,

    Before my extension modify my svg, i want to save this file with a new name (based on the older) (like "file > save as" do).

    Is it possible? if yes how to?

    Thank's

  2. #2
    inklinea inklinea @inklinea⛰️

    Do you want a dialogue to popup to allow the user to choose a foolder and save before the extension runs / updates the svg on canvas ? 

    Or do you just want to save to a predefined folder each time the extension is run ( for example using a path field in the extension inx gui )

    Or a copy to the folder from which the existing original svg is saved ? using python.

     

  3. #3
    FrankI4466 FrankI4466 @FrankI4466
    *

    This one : copy to the folder from which the existing original svg is saved with a new name based on the older name. Now i have this :

                current_file_name = self.document_path()
                with open(current_file_name, 'wb') as output_file:
                    self.save(output_file)
                base_name, extension = os.path.splitext(current_file_name)
                new_file_name = base_name + " - decoupe" + extension
                with open(new_file_name, 'wb') as output_file:
                    self.save(output_file)

    but i want something like "save as" command. With the last save file be the actual open file.

  4. #4
    inklinea inklinea @inklinea⛰️

    There is a hidden warning message (uppercase) in the self.options.document_path method.

    Try this code to see it :)

    def get_attributes(self):
    
        attribute_string = ''
        for att in dir(self):
            try:
                attribute = (att, getattr(self, att))
                attribute_string = attribute_string + str(attribute) + '\n'
            except:
                None
                
        return attribute_string

     

    inkex.errormsg(get_attributes(self.document_path))

     

  5. #5
    FrankI4466 FrankI4466 @FrankI4466

    Yes i had seen it but I save the document next line. Is it ok ?

    What about a save_as function ?

  6. #6
    inklinea inklinea @inklinea⛰️

    As you will know the sequence for an effect extension is:

    Inkscape Main program sends svg to extension system

    svg is processed

    Extension system sends the svg back.

    I would not tamper with the document path that the extension system sends back.

    You can save an svg at any point in an effect extensions execution.

    It's as simple as writing svg.tostring().decode('utf-8') 

    Please note, that the svg does not have to be self.svg

    It can be any svg that you have contructed, deepcopied, altered in the extension system.

    I often create several svgs in the extension system ( using deepcopy or saving the current state of the self.svg) using the tostring() method.

    --------------------------------------------

    I've asked a question to the developers to see if it's possible to save the processed svg for an output extension, something I've never tried.


    ---------------------------------------------
     

  7. #7
    inklinea inklinea @inklinea⛰️

    I got a reply.

    Inkscape does have output extensions - which show in the File>Save and File>Save As dropdown.

    However it is not possible to pass back an SVG to the canvas from those extensions, just save.

    So not possible to both change the document filepath and process an svg at the same time.

  8. #8
    FrankI4466 FrankI4466 @FrankI4466

    Thank's inkilinea for your search an reply.

  9. #9
    FrankI4466 FrankI4466 @FrankI4466
    *

    And is it possible to an extension to open a svg file into inkscape ?

    A the momenti made this :

    subprocess.Popen(["inkscape", new_file_name])

    But is there a betterway ?

  10. #10
    inklinea inklinea @inklinea⛰️

    For opening a new instance of Inkscape, I would use the Inkscape command module.

    https://inkscape.gitlab.io/extensions/documentation/source/inkex.command.html

    If you just want to open an svg in the extension system ( to extract objects from it for example )

    You can use load:

    https://inkscape.gitlab.io/extensions/documentation/source/inkex.base.html?highlight=load#inkex.base.InkscapeExtension.load

    svg_element = inkex.load_svg('/home/name/Pictures/test_svg/cassette-tape.svg').getroot()

  11. #11
    FrankI4466 FrankI4466 @FrankI4466
    *

    Thank's inklinea. But The command inkex.command.inkscape(new_file_name) is blocking.

  12. #12
    inklinea inklinea @inklinea⛰️

    I'm not an expert on subprocesses 

    However I do use subprocess.Popen for windows and ubuntu in one of my extensions.

    https://gitlab.com/inklinea/selection-plus/-/blob/main/selection_plus.py

    There is a function called 'pass_ids_to_dbus'

    To get it to work on Windows, I had to do platform detection and mark it as a detached process.

    There is also a function to switch off stderr and stdout, because launching the subprocess made an annoying popup appear when the extension returned.

  13. #13
    FrankI4466 FrankI4466 @FrankI4466

    Thank's

  14. #14
    FrankI4466 FrankI4466 @FrankI4466
    inklinea

    There is also a function to switch off stderr and stdout, because launching the subprocess made an annoying popup appear when the extension returned.

    For this popup I used : 

                with warnings.catch_warnings():

                    warnings.simplefilter("ignore")

                    subprocess.Popen(["inkscape", new_file_name])

Inkscape Inkscape.org Inkscape Forum Creating New Extensions use save as function in an extension