Reputation: 998
Using Processing 3-0-1 (not updated yet), I have a problem with the dataPath() function.
My code :
final String path = dataPath("");
//...
void setup(){
//...
println(path);
//...
}
This displays in the console :
/media/ubuntu/Expansion Drive/Programmation/Java/Processing/Ubuntu/processing-3.0.1/null/data
but it should display (according to the Documentation and my tries in other projects) :
/media/ubuntu/Expansion Drive/Programmation/Java/Processing/Projets/Time_Fighter/data
So it seems it's returning a path the Processing instead of a path to my project ? Plus, why is there a 'null' ?
The question is, do you know a way that would work to have the path of my sketch ?
PS. A friend of mine proposed to use the File class instead, I had this result :
File file = new File("data");
//...
void setup(){
//...
file.getAbsolutePath();
//...
}
Which returned :
/media/ubuntu/Expansion Drive/Programmation/Java/Processing/Ubuntu/processing-3.0.1/data
PPS. I'm using Ubuntu (Mate)...
Upvotes: 2
Views: 7177
Reputation: 11
sketchPath()
only works after setup()
, you can't do
String path = sketchPath();
setup() {
}
Instead, you need to do:
String path;
setup() {
path = sketchPath();
}
(Processing 3.5.4)
Upvotes: 1
Reputation: 42174
You shouldn't use the dataPath()
function. You should use the sketchPath()
function.
From the Processing source for dataPath()
:
/**
* <b>This function almost certainly does not do the thing you want it to.</b>
* The data path is handled differently on each platform, and should not be
* considered a location to write files. It should also not be assumed that
* this location can be read from or listed. This function is used internally
* as a possible location for reading files. It's still "public" as a
* holdover from earlier code.
* <p>
* Libraries should use createInput() to get an InputStream or createOutput()
* to get an OutputStream. sketchPath() can be used to get a location
* relative to the sketch. Again, <b>do not</b> use this to get relative
* locations of files. You'll be disappointed when your app runs on different
* platforms.
*/
public String dataPath(String where) {
return dataFile(where).getAbsolutePath();
}
And here's sketchPath()
:
/**
* Prepend the sketch folder path to the filename (or path) that is
* passed in. External libraries should use this function to save to
* the sketch folder.
* <p/>
* Note that when running as an applet inside a web browser,
* the sketchPath will be set to null, because security restrictions
* prevent applets from accessing that information.
* <p/>
* This will also cause an error if the sketch is not inited properly,
* meaning that init() was never called on the PApplet when hosted
* my some other main() or by other code. For proper use of init(),
* see the examples in the main description text for PApplet.
*/
public String sketchPath(String where) {
if (sketchPath() == null) {
return where;
}
// isAbsolute() could throw an access exception, but so will writing
// to the local disk using the sketch path, so this is safe here.
// for 0120, added a try/catch anyways.
try {
if (new File(where).isAbsolute()) return where;
} catch (Exception e) { }
return sketchPath() + File.separator + where;
}
Upvotes: 3