Search code examples
pythoninkscape

In an Inkscape extension, how do I set a path's control points?


I am trying to build an Inkscape extension for learning purposes, and would like to manipulate a path's control points.

I know I can list them like this:

inkex.utils.debug([ p for p in self.svg.selected[0].path.control_points ])

but I cannot set them - path.control_points is a generator.

Is there a way besides converting this to an SVG "d" attribute, manipulating it with svgpathtools and setting it back?


Solution

  • There is probably a way to do that if you know what is inside the path. If it is random path or any path, the code will be complicated.

    pel = self.svg.selected[0] # assuming selected is a path
    path_old = pel.path
    

    Assuming your path has those 2 components, or similar a few known components.

    [Move(80.3412, 87.9089), Curve(99.575, 67.2665, 
            103.398, 73.9063, 113.129, 83.6942)]
    

    You can recreate a new path with same components.

    path_new = Path()
    
    old_move = path_old[0]
    new_move = Move(old_move.x + 10, old_move.y + 11) # modify Move coordinates
    old_curve = path_old[1]
    new_curve = Curve(......)
    
    path_new.append(new_move)
    path_new.append(new_curve) 
    

    Depending on your needs, sometimes maybe it is easier to manipulate the d attribute directly.