Skip to content

saveObj() bug? #7905

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

Open
1 of 17 tasks
ErnestSuyver opened this issue Jun 12, 2025 · 5 comments
Open
1 of 17 tasks

saveObj() bug? #7905

ErnestSuyver opened this issue Jun 12, 2025 · 5 comments

Comments

@ErnestSuyver
Copy link

Most appropriate sub-area of p5.js?

  • Accessibility
  • Color
  • Core/Environment/Rendering
  • Data
  • DOM
  • Events
  • Image
  • IO
  • Math
  • Typography
  • Utilities
  • p5.strands
  • WebGL
  • DevOps, Build process, Unit testing
  • Internationalization (i18n)
  • Friendly Errors
  • Other (specify if possible)

p5.js version

2.0.3

Web browser and version

Firefox 139.0.4 (aarch64)

Operating system

macOS 15.5

Steps to reproduce this

intro

When trying to export an ".obj" file from a p5 sketch, the console throws an error. Claims it doesn't recognize a "fn" function.

Could this be related to line 415-416 in https://github.com/processing/p5.js/blob/v2.0.3/src/webgl/p5.Geometry.js?

const blob = new Blob([objStr], { type: 'text/plain' });
    fn.downloadFile(blob, fileName , 'obj');

Steps:

  1. WEBGL renderer
  2. buildGeometry() in setup()
  3. saveObj() in keyPressed()
  4. When pressing the key ("s" in this case), the console given an error
  5. Swapping the CNV version for a local minified one makes no difference. Also, buildGeometry does not seem to work for older p5 versions.

Snippet:

let myModel;

function setup() {
  createCanvas(400, 400, WEBGL);
  
  myModel = buildGeometry(() => {  

    blah blah

  });
 
}

function draw() {
  
  model(myModel);
}

function keyPressed() {
  if (key === 's') {
    myModel.saveObj(); 
  }
}

console error

Uncaught ReferenceError: fn is not defined
saveObj http://127.0.0.1:5500/p5.min.js:1
keyPressed http://127.0.0.1:5500/sketch.js:60
_onkeydown http://127.0.0.1:5500/p5.min.js:1
e http://127.0.0.1:5500/p5.min.js:1
p5 http://127.0.0.1:5500/p5.min.js:1
promise callback*p5< http://127.0.0.1:5500/p5.min.js:1
http://127.0.0.1:5500/p5.min.js:1
p5.min.js:1:179337
saveObj http://127.0.0.1:5500/p5.min.js:1
keyPressed http://127.0.0.1:5500/sketch.js:60
_onkeydown http://127.0.0.1:5500/p5.min.js:1
(Async: EventListener.handleEvent)
e http://127.0.0.1:5500/p5.min.js:1
p5 http://127.0.0.1:5500/p5.min.js:1
(Async: promise callback)
p5 http://127.0.0.1:5500/p5.min.js:1
http://127.0.0.1:5500/p5.min.js:1

Copy link

welcome bot commented Jun 12, 2025

Welcome! 👋 Thanks for opening your first issue here! And to ensure the community is able to respond to your issue, please make sure to fill out the inputs in the issue forms. Thank you!

@ErnestSuyver
Copy link
Author

ErnestSuyver commented Jun 12, 2025 via email

@ErnestSuyver
Copy link
Author

I should add that mousePressed with save() works fine in combination with WEBGL and buildGeometry.

@davepagurek
Copy link
Contributor

I'm guessing Kit's reply to the GitHub email didn't make it to GitHub so I don't have the full context, but looking at this code, this does strike me as suspicious:

fn.downloadFile(blob, fileName , 'obj');

Normally we only assign to fn when defining a function in an addon module, and don't use it when calling a function, because this will be undefined when we do. (fn.something = ... adds a new method to every p5 instance, but calling it on fn rather than on a p5 instance means it can't access anything that needs this.) I don't know that downloadFile actually needs this to work, but using fn out of an addon here is likely the source of the bug.

We can probably import downloadFile from here and call it directly (downloadFile(...)) rather than trying to call it on fn:

function downloadFile(data, fName, extension) {

@ksen0
Copy link
Member

ksen0 commented Jun 13, 2025

Hi @ErnestSuyver sorry about creating the confusion and thanks Dave for jumping in! I had a draft response that was focused on the keyboard event which was my first thought here, but that's not the issue overall in any case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Ready for Work
Development

No branches or pull requests

3 participants