How do I get the x and y position of a child PShape of a loaded svg?

edited March 2016 in How To...

I'm working on a sketch where I load in a complex svg and I want to extract the positions of the child shapes of the loaded svg.

I have tried storing the childshape into a new PShape and then calling getVertexCount or getVertex() but these methods don't seem to work on childshapes. Example: When calling getVertexCount on the childshape I get the error message: "getVertexCount() only works with PATH or GEOMETRY shapes"

Anyone have an idea?



  • Hi. Looking a PShape source code, it looks like SVG child shapes can be loaded in as a group, as a primitive (e.g. circle, rectangle), a path, or as geometry (3D?). You can do something like this to go through the child shapes.

      for (int i = 0; i < shape.getChildCount(); i++) {
          PShape thisShape = shape.getChild(i);

    A group has no vertices, nor does a primitive. So you need to do something like thisShape.getFamily() to work out which kind of child you are dealing with (e.g. PShape.GROUP, PShape.PRIMITIVE etc).

    For primitives, you can do something like

    float[] p = thisShape.getParams();

    From memory, getParams will give you x, y and height and width. Using the example above, you'd get those values from p[0] to p[3].

    For non-primitives, you can iterate over the vertices using getVertexCount() and then average out all the x and y values to get an approximate location

    All x and y values seem to set relative to the drawing size of the SVG. Sadly, an SVG can have items that are outside the drawing size. Your SVG could also be rotated, scaled etc, which adds to the complexity.

    I have been trying something similar myself. It is possible to do, but not particularly straightforward, and is much easier if you aren't looking to do rotations/transforms etc

  • edited March 2016

    I'll try it out tonight! Thanks for answering;)

Sign In or Register to comment.