Ruchi
December 1, 2020, 11:12am
1
import processing.svg.*;
PShape s;
float reso, dLen;
int counter = 0;
void setup() {
size(800, 800);
reso = width;
dLen =sqrt(pow(reso, 2)+pow(reso, 2));
shapeMode(CENTER);
beginRecord(SVG, "DL-"+nf(counter, 3)+".svg");
}
void draw() {
translate(width/2,height/2);
PShape s0 = createShape(GROUP);
s0.addChild(bgdRect());
float xoff = reso/2 + 30;
float yoff = dLen/2 - 05;
PShape s = createShape();
s.beginShape();
s.fill(255);
s.stroke(255);
s.vertex(-xoff, 0);
s.vertex(0, -yoff);
s.vertex(xoff, 0);
s.vertex(0, yoff);
s.endShape(CLOSE);
s0.addChild(s);
shape(s0,0,0);
endRecord();
}
PShape bgdRect() {
PShape rect = createShape(RECT, -width/2, -height/2, width, height);
rect.setFill(0);
return rect;
}
Code above is for creating svg.
PShape s;
PShape s1 ;
void setup() {
size(800, 800);
//background(127);
s = loadShape("DL-000.svg");
shapeMode(CENTER);
s1= s.getChild(0);
println(s.getChildCount());
}
void draw() {
translate(width/4,height/4);
shape(s, 0, 0, 300, 300);
translate(width/2,height/2);
shape(s1, 0, 0, 300, 300);
}
The above code displays svg as whole but not when one of the shape is extracted using getChild.
I don’t know why getchild is not working.
I think you should do something like :
PShape[] shapes;
shapes = s.getChildren(); // in setup()
shape(shapes[0], 0, 0, 300, 300); // in draw()
Ruchi
December 1, 2020, 2:55pm
3
Still doesn’t work. Same result
It’s maybe the way your origin file is written. Try to open another svg file, open a text editor and search the difference.
Ruchi
December 2, 2020, 9:56am
5
Other example svgs getChild works so the issue is with the svg coding. I drew the shapes without using Pshape and recorded the svg but same result. The code is simple,can’t figure out the issue.
Show us a part of your svg file with headers
Ruchi
December 2, 2020, 12:58pm
7
// Creating SVG using PShape
import processing.svg.*;
PShape s;
float reso, dLen;
int counter = 0;
PShape s0;
void setup() {
size(800, 800);
noLoop();
reso = width;
dLen =sqrt(pow(reso, 2)+pow(reso, 2)); // Diagonal Length
// Creating group shape to add shapes
s0 = createShape(GROUP);
//Creating & Adding first shape Rectangle
s0.addChild(bgdRect());
// Creating & Adding second shape Rhombus
s = createShape();
float xoff = reso/2 + 30;
float yoff = dLen/2 - 05;
pushMatrix();
s.beginShape();
s.fill(255);
s.stroke(255);
s.vertex(-xoff, 0);
s.vertex(0, -yoff);
s.vertex(xoff, 0);
s.vertex(0, yoff);
s.endShape(CLOSE);
popMatrix();
s0.addChild(s);
beginRecord(SVG, "DL-"+nf(counter, 3)+".svg");
}
void draw() {
translate(width/2, height/2);
shape(s0,0,0);
endRecord();
}
PShape bgdRect() {
PShape rect = createShape(RECT, -width/2, -height/2, width, height);
rect.setFill(0);
return rect;
}
// Creating SVG without using PShape, directly drawing the shapes
import processing.svg.*;
float reso, dLen;
int counter = 0;
void setup() {
size(800, 800);
noLoop();
reso = width;
dLen =sqrt(pow(reso, 2)+pow(reso, 2)); // Diagonal Length
beginRecord(SVG, "DL-"+nf(counter, 3)+".svg");
}
void draw() {
pushMatrix();
translate(width/2, height/2);
// Drawing first shape Rectangle
bgdRect();
// Drawing second shape Rhombus
float xoff = reso/2 + 30;
float yoff = dLen/2 - 05;
beginShape();
fill(255);
stroke(255);
vertex(-xoff, 0);
vertex(0, -yoff);
vertex(xoff, 0);
vertex(0, yoff);
endShape(CLOSE);
popMatrix();
endRecord();
}
void bgdRect() {
fill(0);
stroke(0);
rectMode(CENTER);
rect(0,0,width,height);
}
Ruchi
January 31, 2021, 10:38am
8
I figured out that the getChild() only works with name and not index in processing. removeChild() works with both index and name. So for getChild() to work, shapes have to be named manually.