Inkscape.org
Creating New Extensions Problem in Debugging m
  1. #1
    Mpx83 Mpx83 @Mpx83

    Hi,

    I'm trying to create an extension in inkscape that adds a gradient mesh to the select shape. (then it would do something else but I'm blocked at this step).

    I created a simple .inx and .py files

    my_test.inx

    <?xml version="1.0" encoding="UTF-8"?>
    <inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
        <name>Create Mesh Gradient</name>
        <id>org.example.inkex.create_mesh_gradient</id>
    	<param name="rows" type="int" gui-text="Rows">2</param>
    	<param name="cols" type="int" gui-text="Cols">3</param>
        <effect>
            <effects-menu>
                <submenu _name="Custom"/>
            </effects-menu>
        </effect>
        <script>
            <command location="inx" interpreter="python">my_test.py</command>
        </script>
    </inkscape-extension>

    and this is my python script.

    import inkex
    from inkex.elements._filters import MeshGradient
    
    class CreateMeshGradient(inkex.EffectExtension):
        def add_arguments(self, pars):
            pars.add_argument("--rows", type=int)
            pars.add_argument("--cols", type=int)
    
        def effect(self):
        # Retrieve user inputs
            myRows = self.options.rows
            myCols = self.options.cols
    
        # Create a new mesh gradient with a default position
            mesh_gradient = inkex.elements._filters.MeshGradient.new_mesh((0, 0), myRows, myCols)
            gradient_id = self.svg.get_unique_id("mesh_gradient")
            mesh_gradient.set('id', gradient_id)
            self.svg.defs.add(mesh_gradient)
            rect = inkex.Rectangle()
            rect.set('x', '10')
            rect.set('y', '10')
            rect.set('width', '100')
            rect.set('height', '100')
            rect.set('fill', 'url(#' + mesh_gradient.get('id') + ')')
            #rect.set('style', 'fill:red')
            self.svg.append(rect)
    		
    if __name__ == "__main__":
        CreateMeshGradient().run()

     

    The code runs without errors but It does not attach anything to the selected object. So to test it I also make the script create a rectangle and add it to the document. If I fill the rectangle in red it works. If I fill the rectangle with the mesh gradient the rectangle appears "trasparent" and it says that a mesh_gradient is attached it but I cannot edit it in no way with mesh tools, see attached image.

     

    Any idea what I'm making wrong?

  2. #2
    Kaalleen Kaalleen @Kaalleen

    You do not set any stops for your mesh gradient. They are the key to make it work.