-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds geometry to downloadable obj files functionality #6812
Conversation
@davepagurek Please take a look and let me know if this works and if further changes are needed, maybe unit tests, examples, or even how I've implemented this. Thanks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's looking good so far! Some user-facing documentation would be great.
For unit tests, some of our tests use this method to test downloads, maybe we can try using it too?
Line 27 in ef4647f
function testWithDownload(name, fn, asyncFn = false) { |
an example of its usage:
p5.js/test/unit/io/saveTable.js
Lines 99 to 112 in ef4647f
testWithDownload( | |
'should download a file with expected contents (tsv)', | |
async function(blobContainer) { | |
myp5.saveTable(myTable, 'filename', 'tsv'); | |
let myBlob = blobContainer.blob; | |
let text = await myBlob.text(); | |
let myTableStr = myTable.columns.join('\t') + '\n'; | |
for (let i = 0; i < myTable.rows.length; i++) { | |
myTableStr += myTable.rows[i].arr.join('\t') + '\n'; | |
} | |
assert.strictEqual(text, myTableStr); | |
}, | |
true | |
); |
|
||
|
||
// Vertices | ||
this.vertices.forEach(v => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work on this method, nice and concise!
src/webgl/p5.Geometry.js
Outdated
objStr += `f ${faceStr}\n`; | ||
}); | ||
|
||
save(objStr, fileName); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
save()
is a user-facing function, so if this method uses it, users could also technically use it, but they would have to know to pass an obj string through here. Maybe instead we could use the internal downloadFile
method?
Line 227 in 37d3324
p5.prototype.downloadFile(blob, filename, extension); |
src/webgl/p5.Geometry.js
Outdated
* @for p5.Geometry | ||
*/ | ||
|
||
geometryToObj (fileName='model.obj'){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can call this saveObj
so users would call it like geom.saveObj()
instead of geom.geometryToObj()
, which feels a tad redundant?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
saveObj
sounds much better :)
@davepagurek made some changes, let me know if these work.Thanks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work on the writing for the documentation! I just have a few things left for the example.
I also thought of one more thing for the exporter itself about handling cases where we don't have either normals or texture coordinates.
src/webgl/p5.Geometry.js
Outdated
* }); | ||
* | ||
* octa = endGeometry(); | ||
* octa.saveObj(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we do this on button click maybe? I think going to the reference page and immediately having the file immediately download might be a bit jarring.
src/webgl/p5.Geometry.js
Outdated
* let angleX = 0; | ||
* let angleY = 0; | ||
* function setup() { | ||
* createCanvas(400, 400, WEBGL); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/webgl/p5.Geometry.js
Outdated
// Faces | ||
this.faces.forEach(face => { | ||
// OBJ format uses 1-based indices | ||
const faceStr = face.map(index => index + 1).join(' '); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we have normals and texture coordinates, I think we might have to use f 1/1/1 2/2/2 3/3/3
to specify that the face does in fact have vertices and faces? We read it out from the slashes here:
Line 252 in 37d3324
const vertParts = vertString.split('/'); |
It might take a few if statements to do e.g. f 1//1 2//2 3//3
if we have normals but no texture coordinates.
Hi @diyaayay! I just wanted to check in to see whether you're still interested and have time to work on this? No problem at all of not, and would you be open to other contributors or myself continuing off of this branch to finish up the feature? |
@davepagurek I've been a bit occupied lately with multiple stuff. I'm okay with any other contributors taking this up further and finishing the feature. |
No problem, thanks for the great foundation you've made for this feature so far 🙂 I hope GSoC is going well! |
I've got a version of this that might work that can download both stl and obj filetypes, but I'm not sure how to submit it. |
Thanks @dgrantham01! How have you been working on it so far? Need any help making a PR? |
I made a little library type thing for my students to make generative sculptures last year and just modified it a bit to fit within the p5.Geometry class. It basically adds a save function to the class rather than going through the save function in the core library. I imagine that we could modify the save function in the core library similarly to the saveJson and saveStrings logic there. This is the code though I haven't tested it in this context.
|
Nice! Are you able to clone this existing branch to add your STL exporter into the existing code, maybe by renaming the method to be format agnostic? Might be able to through here: For the OBJ version, I wonder if we have to handle the same issue mentioned in this comment https://github.com/processing/p5.js/pull/6812/files#r1500085929 and make it output something like |
I got it checked out. Never really contributed to this kind of thing before, but I'll see if I can get it all together. One clarification about the Geometry data organization, are the indices of the vertexNormals and uvs the always the same as the vertices? The wiki on the obj format suggests that they could be different, but I just want to double check because I think that's not the case here. |
That's right, although in an arbitrary .obj file they might be different, in |
Ok. I fixed the obj and added stl and I think I committed it through the github online thing. Should be working. My test sketch was working and both stl and objs opened in blender no proble. A quick scan over the text inside looked like it was correct as well. |
ok nice! does it let you push the code to a new branch and make a PR? Also by online thing, is that Github Codespaces? |
Yes. Codespaces is the online thing. I believe I managed to get it to commit and create a PR. |
I don't see it yet unfortunately, do you have a link to the PR? |
I guess my commits aren't even going through because of a "husky" node version error, which I can't seem to resolve in codespaces, visualstudio, or github desktop. Here is a link to a forked version of the geometry file which should be correct. https://github.com/dgrantham01/p5.js/blob/main/src/webgl/p5.Geometry.js |
Thanks! If getting it all working through GitHub is a problem, this weekend I can try to coalesce everything together into this PR to get both of your changes in. |
Fix obj export: -included button in example to download the model instead of instantly downloading -included vertexNormal and uv index inclusion in face output added a saveStl function that saves an stl model
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your help everyone, I think this is good to go now!
@all-contributors please add @diyaayay for code |
@diyaayay already contributed before to code |
@all-contributors please add @dgrantham01 for code |
I've put up a pull request to add @dgrantham01! 🎉 |
Resolves #6769
Changes:
1- Added obj file support to
save()
.2- Created a method called
geometryToObj()
inp5.Geometry.js
.Screenshots of the change:
Sketch used:
Downloaded Obj File:
Sketch to check the downloaded obj file:
Downloaded obj file on canvas:
PR Checklist
npm run lint
passes