Unable to get the image fills from user selection. "This VM has been destroyed"

Hi, I’m new to Figma Plugin, and I want to get the image filles from user selection.

Here’s my code’s logic.

  1. Get a parameter which is a selection
  2. Check if the selection.fills[0].type is “IMAGE”
  3. Get imageHash from it and call Figma.getImageByHash(imageHash)
  4. call getSizeAsync() and getBytesAsync() methods

Right after step 3, I can see that I got an image object-not null-so I think I get a right image.
But If I call getSizeAsync() or getBytesAsync(), an error occurs.

If I call getSizeAsync(), the error message is,

Image dimensions not available

If I call getBytesAsync(), the error message is,

Uncaught (in promise) Error: This VM has been destroyed

I couldn’t find about these errors in internet or forum at all.

Here’s my code.

  if (
    selection.type === "RECTANGLE" &&
    Array.isArray(selection.fills) &&
    selection.fills[0].type === "IMAGE"
  ) {
    const { imageHash } = selection.fills[0];

    if (!imageHash) {
      // If no imageHash is available.
      const message = "Please check if you selected images only.";
      throw new Error(message);
    }

    const image = figma.getImageByHash(imageHash);

    if (!image) {
      // If no imageHash is available.
      const message = "Failed to load an image from your selection.";
      throw new Error(message);
    }

    console.log(image);
    const { width, height } = await image.getSizeAsync();
    // const imageData = await image.getBytesAsync();

Would you share your insights please?
Any information about this would be really helpful, thanks.

Right click and go to Plugins → Development, now check and select Use developer VM. Now it should not show any error for getBytesAsync.

1 Like

This error was because I tried to get the image fills after I closed plugin.

I’m so ashamed for being so novice but I hope someone like me would get helped from this.