We are about to switch to a new forum software. Until then we have removed the registration on this forum.
When using getChild( index )
to retrieve a child of a PShape loaded from an SVG file, I've found that the child's height and width attributes are not set. However, when I get the same child using getChild( name )
the height and width are set as expected. Similarly, and probably related, if I use shape(child, x, y, w, h)
to draw a child shape I retrieved by index, it won't draw at all. But, if I do the same with a child retrieved by name, it works fine.
Once a child shape has been accessed by name, however, accessing it by index no longer presents these problems. So, as a workaround I've been using code like this in setup()
:
parent = loadShape("myshape.svg");
int n = parent.getChildCount();
for (int i = 0; i < n; i++) {
child = parent.getChild(i);
String name = child.getName();
child = parent.getChild(name);
}
After that, in draw()
, I can access child shapes by index and they work as expected. This doesn't seem ideal to me. Is there a reason it was implemented like this or is it a bug?
Answers
we've seen this recently. apparently the 'by name' and the 'by index' structures are two separate things. but you'd think that the objects contained within would be the same.
https://github.com/processing/processing/blob/eb0429ae162c1dc82109feee69fd2a8e7b475f13/core/src/processing/core/PShape.java#L82
vs
https://github.com/processing/processing/blob/eb0429ae162c1dc82109feee69fd2a8e7b475f13/core/src/processing/core/PShape.java#L203
because that name table is a map anything with the same name as an existing entry will replace it. that is a potential problem but i'm not sure if it's the case here.
You are not wrong to be confused. It may not be a bug, but it can be surprising behavior.
I summarize some of the differences between PShape indexing and naming behavior in this thread comment, with an example sketch:
https://forum.processing.org/two/discussion/comment/74520/#Comment_74520
Note that some of what you are struggling with may be specific to SVG import, not PShape in general.
Essentially, SVG imports named objects, which may be indexed (as you are doing), and will then be automatically drawn once indexed. If instead of doing SVG import you build a PShape on your own by adding vertices, those vertices are auto-indexed (and thus auto-drawn) but not named.