Import a lot of svg

Hi everyone, I wanted to know if there is a solution to import a lot of svg at the same time. they are all in the same folder. I have like 100 and I can’t see myself writing “PShape bot, bot2, bot3 …;”

Because I wanted to overlap each svg.

Thank you in advance for your answer.

PShape bot, bot2, bot3 ;

void setup() {
fullScreen();
// The file “bot1.svg” must be in the data folder
// of the current sketch to load successfully
bot = loadShape(“frame-0052 17.55.11 17.56.26.svg”);
bot2 = loadShape(“frame-0053 17.55.11 17.56.26.svg”);
bot3 = loadShape(“frame-0056 17.55.11 17.56.26.svg”);
noLoop();

}

void draw(){

background(0);
stroke(255);
noFill();

bot.disableStyle(); // Ignore the colors in the SVG
shape(bot);

bot2.disableStyle(); // Ignore the colors in the SVG
shape(bot2);// Draw

bot3.disableStyle(); // Ignore the colors in the SVG
shape(bot3);// Draw
}

Hello and welcome to the forum!

It’s great to have you here!

you can get a list of all files in the folder easily: A music player that automatically adds .wav or .mp3 files in the data folder to list?

Chrisir

1 Like

Hi @Dimdimtri

Welcome to the forum! :slight_smile:

I believe that you can also look for an ArrayList to load the files and add the shapes. However, this will only work if the filenames are equal or at least follow the same format. In your example, they all have the following format:
“frame-00XX 17.56.26.svg”
So, a loop might be a good idea. Something like:

int listSize = 100; //number of files
int count = 0; //lets asssume that the first svg has the name frame-0000 17.56.26.svg
//If it does not have just change to whatever integer you want it to start

ArrayList<PShape> bots; //ArrayList of shapes

void setup() {
  bots = new ArrayList<PShape>(); 
 //Load all the .svg files
  for (int i = count; i < count + listSize; i++) {
    //println("frame-00" + i + " 17.56.26.svg");  //DEBUG THE FILENAME
    String filename = "frame-00" + i + " 17.56.26.svg";
    bots.add(loadShape(filename)); //Load shape and add
  }
}

void draw() {
  background(0);
  stroke(255);
  noFill();
  
  //For enhanced loop to disable and display shapes
  for(PShape bot : bots){
    bot.disableStyle();
    shape(bot);
  }
}

Find above some useful resources as well

Resources

ArrayList: ArrayList / Reference / Processing.org
The Coding train ArrayList: https://www.youtube.com/watch?v=HnSJZ4qTcwY&ab_channel=TheCodingTrain
For enhanced loop: Java For-each Loop | Enhanced For Loop - javatpoint

Best regards

1 Like

Grazie Mille !!!

Thanks, it works perfectly! I modified the code a bit, and I also changed my file saving mode to make things easier. Thanks again . It’s been a week since I discovered Processing3 and I find it really interesting :slight_smile:

thanks guys

2 Likes

Glad it worked and could help! :slight_smile:

Allows try to look for resources either on Processing documentation or java. It is your best tool to solve your problems.

2 Likes