Undefined method `definition' for Sketchup::Image Since SketchUp 2024

Why this method is removed in 2024. And how to get the definition of an image?

It was never there, so it wasnā€™t removed. :wink:

However the ComponentDefinition #image? method can be used to determine if a component definition is used to define an image.

So a method to find the image definition can be done like e.g.:

def image_definition(image)
  return unless image.is_a?(Sketchup::Image)
  Sketchup.active_model.definitions.find { |definition|
    definition.image? && definition.instances.include?(image)
  }
end

Manipulating an image objectā€™s definition can crash SketchUp.
Why do you need to get a reference to an imageā€™s definition?

I am sure that the ā€˜definitionā€™ method for image is avaliable in SketchUp 2023 and before. But maybe not in the API docs. Also I can understand an image is treated like a component instance. But I can not directly get the definition of image in 2024 now, itā€™s weird for me. Thanks anyway.

You are not supposed to directly modify Ruby or SketchUp API classes.
Instead, use a refinement that only your code can see.

Again, ā€¦

Manipulating an image objectā€™s definition can crash SketchUp.
Why do you need to get a reference to an imageā€™s definition?

To determine if two image the same, and reuse their mesh data.

I didnā€™t modified. The method is just there. You can use ruby ā€˜methodsā€™ to see the ā€˜definitionā€™ method in SketchUp 2023 and before. Besides there is ā€˜SUImageGetDefinitionā€™ in C API.

In this case, there is an extension (orphan rb file or whatever) in your SU2023 Plugin directory(ies) that modified the Sketchup::Image class, when SU loaded, or you manually loaded such a modification, but forgot aboutā€¦
The C API nothing to do with this.

SU2021:

Thank you very much. I found the ā€˜criminalā€™. Itā€™s one of the installed plugin on my machine.

2 Likes

ā€œCulpritā€ not ā€œcriminalā€ ā€¦ haha

Mesh data? An image is a flat face with 4 vertices.

You can get the origin, height and width from the instance to compare the size if not scaled. If scaled you can get the unscaled pixel width and height from the instanceā€™s image_rep.

You can compare the pixel data gotten from the instanceā€™s image_rep to determine if they use the same graphic.

image1.image_rep.data == image2.image_rep.data

Or you could also compare the file path from the instances.

You can iterate over the modelā€™s DefinitionList and check the instances of each definition to find the one used by your Image. That said, Image objects are not designed to function like components. Their definition is really an implementation detail leaking out.

1 Like