We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello
anyone know how I can clone an object PShape?
PShape s1;
PShape s2;
void setup()
{
size(1000, 500, P2D);
s1 = createShape();
s1.beginShape();
s1.fill(0, 0, 255);
s1.noStroke();
s1.vertex(0, 0);
s1.vertex(0, 50);
s1.vertex(50, 50);
s1.vertex(50, 0);
s1.endShape(CLOSE);
s2 = createShape(s1);
}
void draw()
{
shape(s2, 25, 25);
}
Answers
Are you sure you need to clone shape, doesn't seem to make sense when you can do above. For something a bit more radical check this out
I've got the same problem, I really need to copy a PShape. When you check the current version of Processing on Github, you can see that somebody already put some efford into it: https://github.com/processing/processing/blob/master/core/src/processing/core/PShape.java
Though this functionality, isn't tested yet and for some reason not in the newest version of the officially downloadable version of processing.
Plus, there's an issue on github rotting around, which adresses exactly this problem: https://github.com/processing/processing/issues/1710
If anybody is making any progress on this, please let us know here, I will do the same.
I came here looking for the same thing, unfortunately it seems impossible to copy a PShape instead of referencing it. I looked and looked to no end. The only thing I can think of is actually loading it one more time from file if it's an "svg" we're talking about. Posting this in the hopes that it gets bumped up and some of the wise elders might help.
I've been working on a method for copying shapes. I don't quite understood if this solution is going to be implemented on the future or not, but the thing is i need that functionality for a project i must finish this month, so i decided to work on an simple method that i could just paste and use.
Basically what it does is read the vertexes of a PShape argument, then read the vertexes codes (0 for normal vertex and 1 for bezierVertexes) and recreates this shape on a copy_shape variable. Then return it. It's been working perfectly for some time now. But right know it only works with vertex() and bezierVertex(). Someone should probably make it work with other kinds of vertexes (curveVertex for example)
If you can't copy the whole PShape, what you can do get its children.
Creating a new PShape and adding these children could work.
Never mind, it doesn't work this way. Children are still the same, so no real copy.
Does it has to be PShape? Or is a library an option?
Geomerative is great for working with SVGs.
Here is an example with geomerative:
I've just realized we were missing a really simple solution. Just use a function that returns the desired Shape. You will gave to type to lines to initialize the shape anyway. This works with absolutely every kind of shape.
If you already know how you're going to manipulate that shape you could even use this function inside a class along with it's specific methods and variables.
So simple! Can't believe i didn't thought about it before haha
Or an even simpler way to make a copy of a PShape
s
that you already have:that doesn't copy the fill or the noStroke state set in the original post. or any of the 100 other fields in the PShape object: https://github.com/processing/processing/blob/master/core/src/processing/core/PShape.java