Applying unique metadata to clones of imported mesh

Minimal example:

I am attempting to apply a piece of unique metadata to an imported mesh upon clicking it.

In the example, clicking either the original dummy or its clone creates the field and assigns the value lol to both.

Clicking the boxes, which are generated meshes and not imported, applies the metadata uniquely to either box upon click. (The clicking is a bit finicky if you’re not hovering your cursor over the ground when you click – sorry don’t know how to get to the bottom of that just yet.)

(Check the console for all logged values; you will have to click into the array of meshes a bit to get to each dummy’s metadata).

In my application (not shown – too large to include in the example) I have many clones of an imported mesh, and need to assign unique metadata to that specific clone when it is clicked. However, all clones are immediately assigned the same metadata, just like the dummy in the example.

Based on what I’ve read, I think clones don’t receive unique metadata (except in the case of generated meshes, I guess?) nor do instances. In my use case, the unique metadata is critical to assign as it needs to be referenced later via search to identify that specific mesh. Additionally, this metadata is dynamic so it must be changeable on each clone over time.

Is there any way to get unique metadata on these clones, or some alternative strategy to assign arbitrary dynamic metadata to cloned/instanced meshes?

Thanks for your attention on this matter.

I see no problem here, if you are able to assign your data to the desired mesh (clone or instance) metadata.
Example -

Thanks @labris, meshes created by the engine seem to work correctly. But imported meshes do not behave the same way. In my example, I have both created and imported meshes, and the created ones work as expected. Imported ones do not appear to work the same way.

I believe you need to select (or exclude them) either by name or id.


Works, thanks @mawa

1 Like

You’re welcome. Have a beautiful day :parasol_on_ground: :sun_with_face:

1 Like

@malwa Sorry, I think I accepted this solution too quickly as I was in haste. In my app, I won’t know the name or ID of the picked mesh ahead of time, so I can’t separate it out. This is how it’s displayed in my original example. Any ideas how to approach that? Thanks for your help.

sry, can’t see a link or pict?
And then, sry again but I don’t really get it.
In the end all meshes have a name and you are already picking them.
You would just go like get the name of the pickedMesh (no matter what it is) myCurrentPickedMeshName = and next set your metadata on this, no?

1 Like

Thanks @mawa - it wasn’t explicit in my example from the first post, but I have an updated example to display:

Selecting the mesh by referencing its name still duplicates the metadata to its clone, and vice versa if you click the clone the original gets the metadata.

The imported mesh’s metadata is copied over when cloning but the clone gets a reference to the same object (since it doesn’t have a clone function). You could assign the clone it’s own metadata object like on line 70 thou and then the metadata will be altered for just the clicked mesh…

Also here’s the source code that handles the clone’s metadata for reference. :slight_smile:

  // Metadata
if (source.metadata && source.metadata.clone) {
    this.metadata = source.metadata.clone();
} else {
    this.metadata = source.metadata;

Thanks @Blake for jumping in here. This has the effect of removing all existing metadata for the imported mesh, though. As you can see it has a fair bit of metadata when first loaded. Is it possible to add unique metadata without removing existing metadata? In my use case, I only wish to add to existing metadata, not replace all the original metadata.

It doesn’t remove anything from the imported mesh, it just assigns a new, empty metadata object to its clone. Could use Object.assign to do a shallow copy of the original mesh’s metadata thou, like below.


Meant to say “has the effect of removing all metadata from the clone” instead of “imported mesh.” But, thank you, clearing it out and then doing a shallow copy via Object.assign does appear to make this work the way I needed to. Thanks!

1 Like