Insert Model 3D

:information_source: Summary Insert Model 3D adds a composer button that simplifies adding 3D models to a Discourse post.
:hammer_and_wrench: Repository https://github.com/Beholder-Vision/discourse-insert-model-3d
:open_book: New to Discourse Themes? Beginner’s guide to using Discourse Themes

Install this theme component

Hi all,
This component lets your users add 3D models to their posts using the Google model viewer component. I created this component to let users of my Photogrammetry SaaS Beholder share models that they create on our shiny new Discourse forum and thought I’d also share it here in case it’s of use to other people. I couldn’t see another similar component, but then I only did a very quick search…

Instructions and screenshots showing how to use the component are below.

Any feedback or thoughts on this component are much appreciated. The model viewer component that I use to display the models has quite a lot of functionality so there’s likely quite a bit of scope to expand this component by exposing some of that functionality to users. I created this component by copy, pasting and modifying the insert video and mermaid components so I’d also really appreciate it if someone with more experience of writing Discourse component could cast their eye over the code and let me know what mistakes I’ve made. :slight_smile:

Regards
Alan

Usage

When the component is installed users will be able to add 3D models to their
posts by clicking on the new cube icon in the composer.

composer_toolbar

Currently the 3D viewer will not download and display the 3D model straight away, instead it will display a poster (that can be customised) and will only load the model when the user clicks the ‘Load 3D Model’ button. The thinking behind this is to help users with limited bandwidth.

Admin Settings

In order for users to be able to upload models with this component you’ll need to add .gltf and .glb to the list of authorized extensions.

You’ll also likely need to up the limit for the max attachment size.

settings_max_attachment_size

This is the probably the main limitation for using this component on hosted Discourse as there, on the lower subscription plans, the max size is limited to 30MB. To be fair though you probably don’t want your users to be uploading massive files and .glb and .gltf files can be greatly reduced in size if they’re generated by a program that supports Draco mesh compression.

19 Likes

Thanks, it worked for a few days but now the load button does not work anymore (tested on Firefox, Chrome and Firefox mobile)

2 Likes

Oh no! This is not good. Thank you for letting me know.

The same had happened on my forum. Looking at the post, what I think has happened is that after a few days Discourse has decided that the uploaded poster image and the 3D model are no longer needed so it has deleted them from the server. :frowning:

I have ‘fixed’ this temporarily on my site by reuploading the model and the poster but I’m now off to read through the Discourse theme docs to see if I can fix it properly. From the quick scan I did when writing the theme component I saw the docs say that as long as an asset is referenced by a post then it will be kept on the server but obviously the URLs that this component puts in the post are not doing that for some reason. If any other plugin writer can offer advice on this it would be much appreciated… :slight_smile:

2 Likes

That’s a weird behavior.

1 Like

Yeah, my current theory is that when I create the markdown to reference the model viewer, then in some way the URL for the uploaded model is not in the correct form for whatever logic that scans post to recognise used uploads to see.

Currently I’m looking at 2 other places in Discourse where files are uploaded. In the image/file upload modal, a reference is kept to a ‘short_url’ that has the form upload:// rather than the final url that will be used by the browser to download the resource. So perhaps I’ll need to work out how to use that? Alternatively, the GitHub - discourse/discourse-insert-video component, which I based my component on uses the resource url directly. Either that component suffers from the same bug (unlikely :slight_smile: ) or those urls are recognised because they’re inserted into the markdown message as HTML tags (I can’t do that with the model-viewer tag as it’s not on the HTML tag whitelist).

That’s my current thinking, or it could be something else entirely. Now off to see if I can find the code that identifies orphan uploads on a Discourse site…

2 Likes

Hi there,

Sorry for the delay in replying to this. I was tied up with contract work last week.

Anyway, I’ve had another look just now, and I think that I’ve identified the code that decides if an upload lives or dies. In the server side code for processing a post there is a routine called ‘each_upload_url’ that will generate a list of uploaded files that are referenced in a post.

Now I’m not a Ruby dev, but from what I can make out it’s looking for URLs referenced by a predefined list of HTML tags. So for the insert video theme component that I copied, the src attribute of any <source> HTML element is recognised as an upload, and the poster attribute of any <video> HTML element is recognised as an upload. For my insert model 3d component though,the <model-viewer> HTML element is not in the list and so the assets it references are not recognised as uploads and so after a few days they will be purged.

The long term fix for this might to be to change the server side code so that model-viewer attributes are recognised as a source of uploads, but I think that it should also be possible to work around this for now by adding in explicit HTML <a> tags linking to the uploads.

I’m going to post in the Bug category to check that my understanding is correct, but on the off-chance that my understanding is correct I’ve submitted a fix to the Insert Model 3D theme component that inserts some hidden <a> tags into the post that reference the uploaded model and poster. @patrickemin If you have some time, could you please check to see if this resolves the issue for you? You’d need to update the component and then reupload any 3D model assets that have been purged.

Alan, not sure if this will solve the riddle, but it has impacted us in the past where there have been bugs in this space.

Orphaned uploads are deleted periodically. As part of an upload, you should end up with an entry in the table uploads. If for any reason that is missing, the upload will be removed eventually.

Might be a line of enquiry.

to check the Upload model you can go into the rails console from Discourse directory and for example:

./launcher enter app
rails c
Upload.last(3)

Cheers

1 Like

Ah that’s useful to know, thanks Robert. Do you know if it’s possible to perform that check or something equivalent on hosted Discourse? I’m assuming not so @patrickemin if your server happens to be self hosted then this could be a good thing to check.
Alternatively, I have a docker based local setup of Discourse that I was working on until I gave up and ran to hosted Discourse. This could be a good excuse to return to it. :slight_smile:

1 Like

Something you’d need to raise with support.

But if it’s clear this is happening to several users, worth investigating on a self-hosted instance before raising a ticket, I suspect.