How can i import files while the sketch is running?

Hi Guys!

Is there a way to import files while the sketch is currently beeing excecuted? E.g.: I have a button(controlp5) and when pressed it opens my filemanager and lets me pick a picture for instance.

For my application i want to change an 3D object, so a .mtl - File. My hopes aren't high, that this is possible but maybe someone has an idea. :D

Answers

  • No problem.

    Look at selectInput

    Inside the call back function, copy the file path inside a global String variable loadPath that you declared before setup ().

    Then use loadStrings together with loadPath to open the mtl.

    I always use a state system to make sure I only load once loadPath is set. So once the load command gets in I tell draw() to be in a state waitForLoad (integer variable / constant) and I then start selectInput. Once loadPath is set, I load and set the state back to stateNormal (integer variable as well).

  • here is an example to demonstrate the states.

    This full sketch just displays the content of a text or mtl file after you loaded it.

    I hope this helps.

    Chrisir ;-)

    // editor path and file extension
    final String pathFolder="texts";
    final String fileExtension = ".mtl";
    
    // states of the program:
    // unique constants: 
    final int normal = 0;
    final int load   = 1;
    ///current state (must be one of them) 
    int state=normal;
    
    // Path 
    String loadPath=""; 
    
    String str="nothing loaded\nuse button in the lower right corner ";
    
    // ------------------------------------------------
    // Core functions of processing 
    
    void setup() {
      size(900, 900);
    } // func 
    
    void draw() {
    
      switch (state) {
    
      case normal:
        drawForStateNormal() ;
        break; 
    
      case load:
        // wait for Load Dialog 
        waitForLoadDialog();
        break;
    
      default:
        //Error 
        println("Fail");
        exit();
        break; 
        //
      } // switch
    } // func
    
    // ------------------------------------------------
    
    void drawForStateNormal() {
    
      background(0);
    
      textSize(14);
    
      // title 
      fill(255, 2, 2);
      text("Demo for selectInput", 
        width-123, 20, 100, 422);
    
      // show the text the user loaded 
      fill(255);
      text(str, 
        20, 20, width-170, height-20);
    
      // ----------------------
      // button
      fill(128);
      if ( overLoad() ) {
        fill(196);
      }
      rect(width-40, height-50, 40, 20);
      fill(255); 
      text("Load", 
        width-40+5, height-50+9+5);
      //
    }//func
    
    //----------------------------------------------------------------------------
    // functions to register if mouse is over buttons 
    
    boolean overSave() {
      return( mouseX > width-40 && 
        mouseY > height-20 );
    }
    
    boolean overLoad() {
      return( mouseX > width-40 && 
        mouseY > height-50  && 
        mouseY < height-50+25 );
    }
    
    boolean overNew() {
      return( mouseX > width-40 && 
        mouseY > height-80  && 
        mouseY < height-80+25 );
    }
    
    // ---------------------------------------------------------------------------
    // Inputs 
    
    void keyPressed() {
    
      if (state!=normal)
        return;
    }
    
    void mousePressed() {
    
      if (state!=normal)
        return;
    
      // for the buttons 
      if ( overLoad() ) {
        initLoad();
      }
      //
    }//func
    
    // -------------------------------------------------
    // load functions 
    
    void initLoad() {
      // init load process 
      // reset
      loadPath="";
      // prepare fileDescription which occurs in the dialogue
      File fileDescription = new File( sketchPath()+"//"+pathFolder+"//"+"*" + fileExtension );
      // open the dialog
      selectInput("Select a file to load", "fileSelectedLoad", fileDescription);
      // set state to wait
      state=load;
    }
    
    void fileSelectedLoad(File selection) {
      // the 'callback' function
      if (selection == null) {
        // println("Window was closed or the user hit cancel.");
        // go back 
        state=normal;
      } else {
        // println("User selected " + selection.getAbsolutePath());
        loadPath=selection.getAbsolutePath();
      }
    }
    
    void waitForLoadDialog() { 
      if (!loadPath.equals("")) {
        // waiting is over
        loadIt();
        // go back 
        state=normal;
      }
    }
    
    void loadIt() {
      // load
      String[] strs = loadStrings( loadPath );
      str = join(strs, "\n");
    }
    //
    
Sign In or Register to comment.