Inkscape.org
Creating New Extensions Passing arguments from Extension to Python
  1. #1
    lukason lukason @lukason

    Hi, I'm trying to pass some arguments from the Inkscape extension to my Python script. The .inx file is looking like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
    	<_name>ex_test</_name>
    	<id>lukas.free.edraw</id>
    	<!--test for pass parameters from inkscape extension to python script-->
    		
    	<param name="test_path" type="path" mode="file_new" filetypes="ely" gui-text="Test path">~/drawing.ely</param>
    	<param name="test_bool" type="bool" gui-text="Test Boolean">false</param>
    	<param name="test_color" type="color" appearance="colorbutton" gui-text="Test Color">#00FF00FF</param>
    		
    	<effect needs-live-preview="false">
    		<object-type>all</object-type>
    		<effects-menu>
    			<submenu _name="Export"/>
    		</effects-menu>
    	</effect>
    	
    	<script>
    		<command location="inx" interpreter="python">ex_test.py</command>
    	</script>
    		
    </inkscape-extension>

    However the boolean seems to be not right passed. It just passes True, no matter if I check or uncheck the boolean. My second problem is that I cannot set the default color. For example in my code I want the default color to be green, #00FF00FF in RGBA representation. But Inkscape doesn't accept the default. How can I change this? And can I get the color string in python?

    My python code looks like the following. It will just return the passed parameters.

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    
    
    import inkex
    
    class ex_test(inkex.EffectExtension):
    	def add_arguments(self, pars):
    		pars.add_argument('--test_path', type=str, default='~/drawing.ely')
    		pars.add_argument('--test_bool', type=bool, default=False)
    		pars.add_argument('--test_color', type=str, default='#00FF00FF')
    		
    		
    	def effect(self):		
    		inkex.errormsg(self.options.test_path)
    		inkex.errormsg(self.options.test_bool)
    		inkex.errormsg(self.options.test_color)
    
    		
    if __name__ == '__main__':
    	ex_test().run()

    Can somebody help with the problems?

    Extension Gui
  2. #2
    lukason lukason @lukason

    There was a mistake of the types in my python code. The type of boolen has to be "inkex.Boolean" and of color "inkex.Color" respectively.

    pars.add_argument('--test_bool', type=inkex.Boolean, default=False)
    pars.add_argument('--test_color', type=inkex.Color, default='#00FF00FF')

    However I'm still not able to set a default color in the .inx file. What is the color representation?

  3. #3
    Martin Owens Martin Owens @doctormo🌹🧀

    I've not seen a default colour being set. It might not be possible (but I hope it is!)

  4. #4
    lukason lukason @lukason

    Me neither. I experimented a bit. And sometimes I was able to set a default color, which was kind of random. I just set in some big interger number (like in the code snippet), assuming that the default color is in RGBAint format. But the resulting color was different from the expected one. The example should return red #FF0000 as default.

    <param name="test_color" type="color" appearance="colorbutton" gui-text="Test Color">16711680</param>

     

  5. #5
    Martin Owens Martin Owens @doctormo🌹🧀

    Does the number include alpha as the first 8 bits?

  6. #6
    lukason lukason @lukason

    No the example above doesn't include the alpha value. But this int does: 4294967040. It should be red with full alpha.

    In further tests, I was not able to change the color via the .inx file anymore. The behavior was unreproducible. So maybe your are right and a default value for the color parameter is not included (yet).

Inkscape Inkscape.org Inkscape Forum Creating New Extensions Passing arguments from Extension to Python