Skip to content

trying to add a text geometry, stuck in JavaScript #28

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
shensquared opened this issue Nov 7, 2018 · 10 comments
Open

trying to add a text geometry, stuck in JavaScript #28

shensquared opened this issue Nov 7, 2018 · 10 comments

Comments

@shensquared
Copy link
Contributor

Hi Robin, I'm trying to add a text geometry where I can put short description of the scene like 'new initialization', so it'd be easier for audience to interpret my animation.

I added a TextGeometry in the geometry.py file, similar to all the box/sphere. By reading this https://threejs.org/docs/#api/en/geometries/TextGeometry documentation, not much is needed other than passing in params, and I'm almost sure I've done this part correctly.

But when declaring a new TextGeomotry, python throws error saying 'uuid' not defined. Looking into some threejs documentation, this seems to be potentially caused by missing of Three.FontLoader() dependency, but then I'm stuck. I have a vague sense the JS files under the viewer folder is what I should modify but I don't know exactly how?

@shensquared shensquared changed the title help: trying to add a text geometry, stuck in JavaScript trying to add a text geometry, stuck in JavaScript Nov 7, 2018
@rdeits
Copy link
Collaborator

rdeits commented Nov 7, 2018

Hey Shen! It would be much easier to figure out what's going on if you can show me your actual code and the full error message. Can you open a Pull Request with whatever you have now?

From what you've said, though, I'm guessing that your TextGeometry's __init__() method is missing a line like this: https://github.com/rdeits/meshcat-python/blob/8b24ac3a72ea56fbb64fca5fcdb1782daa0961bf/src/meshcat/geometry.py#L63 , which invokes the parent class constructor and automatically assigns self.uuid.

@shensquared
Copy link
Contributor Author

Thanks for the quick reply, Robin! I've opened a Pull Request #29, the __init__() method seems to be right?

@shensquared
Copy link
Contributor Author

shensquared commented Nov 8, 2018

Here's the JavaScript console error
screen shot 2018-11-08 at 5 36 38 am

I wasn't able to reproduce the exact uuid error on my home computer (will try reproduce on the lab computer later), instead I had a different uuid problem. But it seems so low level I feel it's probably not relevant to the issue.
screen shot 2018-11-08 at 5 49 33 am

Regarding this JS error, mrdoob/three.js#9905 seems very relevant.

On the MeshCat side, I'm guessing I should mimic https://github.com/rdeits/meshcat/blob/c7dcbca3af0e76f3c22573076a8d8e54b52dff67/src/index.js#L182? I tried a dumb

else if (geom.type == "TextGeometry") {
        var loader = new THREE.FontLoader();
        loader.load( 'fonts/helvetiker_bold.typeface.json', function ( font ) {

            } );

        let obj = loader.parse("data:text/plain," + geom.data);
        let loaded_geom = obj.children[0].geometry;
        loaded_geom.uuid = geom.uuid;
        let json = loaded_geom.toJSON();
        for (let child of obj.children) {
            dispose(child);
        }
        return json;
    } 

and sadly it didn't magically work...

@rdeits
Copy link
Collaborator

rdeits commented Nov 8, 2018

Huh, I think you've run into a python bug: python/cpython#5254 and https://bugs.python.org/issue32502 . It looks like we could use uuid4 instead of uuid1 to work around it.

As for the JS code, I think we should actually try not to mimic that function: the way I handle .OBJ meshes is kind of gross (loading them, then calling toJSON, then passing that json to another function that loads from json. Ideally we'd just return the output of loader.parse directly and then add it to the scene, without going back and forth through json.

@shensquared
Copy link
Contributor Author

Hi Robin, sorry for the delay in response, I'm having a few deadlines.

I kind of get it now. So loader.parse seems to be the right start, but instead of translating that to json, it should be directly fed into whatever JS function that's called when loading the scene? most likely the set_object? And probably I only need to play with index.js?

@shensquared
Copy link
Contributor Author

I think I've made some progress on the JS side. Created a PR meshcat-dev/meshcat#41 . Now the handle_special_geometry seems to return the correct objet, and font is correctly loaded too.

screen shot 2018-11-10 at 5 18 31 pm

I know the returned object is passed to set_object_from_json, but I'm confused by the syntax there, can't figure out what's going on in the highlighted green lines.

From the other end, when I trace down the error message, it seems in between the set_object_from_json and the parseGeometries call, somehow the geometry type gets lost.
screen shot 2018-11-10 at 5 19 11 pm

I first tried in the debug mode, manually force adding a TextGeometry to the .type field, but then realized even if there is this .type, parseGeometries still would complain, since it doesn't have a case for TextGeometry. I believe the parseGeometries is auto generated from three.js files so we shouldn't mess with it, right? If so, I suspect the call stack has to be changed, i.e., when the object type is TextGeometry, it should be passed to some other parsing method, or at least go through some preprocessing. But then I'm stuck again...

Btw, since this is now purely about JS, would you prefer we have the discussion in the meshcat repo?

@mhelmrei
Copy link

Hello,
is there any update on the TextGeometry in the latest meshcat version? Is there specific commit supporting it?
Thanks.Matthias

@the-raspberry-pi-guy
Copy link

+1

Would also love a text geometry feature in MeshCat - would be great to display text alongside animations for self contained demonstrations

@jstmn
Copy link

jstmn commented Sep 3, 2024

+1, this would be nice.

@jstmn
Copy link

jstmn commented Sep 3, 2024

Separately, i'm getting a similar error when trying to add a CapsuleGeometry

image

class JmCapsule(meshcat.geometry.Geometry):

    def __init__(self, length, radius):
        super(JmCapsule, self).__init__()
        self.length = length
        self.radius = radius
        self.capSegments = 4
        self.radialSegments = 4

    def lower(self, object_data):
        # entries copied from https://threejs.org/docs/#api/en/geometries/CapsuleGeometry
        return {
            u"uuid": self.uuid,
            u"radius": self.radius, #  — Radius of the capsule. Optional; defaults to 1.
            u"length": self.length, #  — Length of the middle section. Optional; defaults to 1.
            u"capSegments": self.capSegments, #  — Number of curve segments used to build the caps. Optional; defaults to 4.
            u"radialSegments": self.radialSegments,
            u"type": u"CapsuleGeometry",
        }
        
...
tf = meshcat.transformations.identity_matrix()
jm_capsule = meshcat_utils.JmCapsule(0.1, 0.05)
vis[f"jm_capsule"].set_object(jm_capsule, meshcat_utils.DEFAULT_MATERIAL)
vis[f"jm_capsule"].set_transform(tf)

@shensquared, @rdeits was there a solution to the issue with the textgeometry attempt? Otherwise, any ideas on how I can get this to work? Thanks

  • jeremy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants