Importer Class, overriding #load_file for .png import


#1

I’m trying to write my own image (*.png) importer. My simple subclass of Sketchup::Importer is below (It’s just a test to figure out if I know what I’m doing, it doesn’t do anything :grinning: ).

When I use the UI to use my JmcImporter subclass, I see that my #description and #do_options methods fire successfully (i.e. put statements logged to the console). However, my #load_file method does not appear to fire when I select and load a *.png. Instead, SketchUp seems to default to it’s image importer.

If I alter my #file_extension to import text files the #load_file method fires ok. Anybody know why my #load_file wouldn’t work for images?

class JmcImporter < Sketchup::Importer
       def description
         p "description called"
         return "Jeff's Image Importer (*.png)"
       end

       def file_extension
         return "png"
       end

       def id
         return "com.sketchup.importers.jmc_png"
       end

       def supports_options?
         return true
       end

       def do_options
         my_settings = UI.inputbox(['Resolution:'], ['1'],
           "Import Options")
         p "do_options called"
       end

       def load_file(file_path, status)
          p 'load file called'
         return 0
       end
     end

     Sketchup.register_importer(JmcImporter.new)

#2

(1) rename the “Plugins” directory

(2) create a new empty “Plugins” directory with only your importer in it, for testing.
Eliminate the possibility of another ruby importer interfering.

(3) Only Ruby base classes should be defined at the toplevel.
Your class should be inside module Jmc or module JMC (your choice.)
Since you may wish to write more than one importer, it makes since to be more descriptive with the class name.
Perhaps class BMPImporter ?


#3

OK I’ve done a bit of testing on PC.

It seems there is an issue with the description() method.
I had my custom SKM importer loaded, and yours would not appear in the type picklist, until I switched mine off.
Nor would any of the standard images types (built-in importers.)

I was able to see the two of yours (I cloned yours to a Text importer, as well.) But only the first time after I switched mine off. Restarted SketchUp and I only saw the first PNG importer after.

It seems if SketchUp does not like the description of the first importer, it will not load any others afterward.
So I realized my custom importer had an invalid description string with pipe symbols in it. Fixed that and all importers now show up.

JMC_PngImporter.rb (1.1 KB) JMC_TextImporter.rb (976 Bytes)


#4

Thanks Dan!

The namespacing and debugging tips are super helpful - perfect for my stage (first plugin attempt). The behavior of proceeding to load the *.png using a built-in importer instead of calling my own JMC::PNGImportExtension#load_file() persists on my system through. I’ll keep fiddling with it later today.


#5

Interesting that does not happen on PC. All that happens (if you specifically choose the “JMC Image Importer”,) is the full path (of the picked file,) is displayed in the console.


#6

I can reproduce what @jcaves describes: the diagnostic printout never appears and the import falls back to the default png importer. So it isn’t just you, Jeff :grinning:. I’ll mess with it too and see if I can turn up anything.

[edit]
I have come to believe that this is a bug in the implementation of image imports in general on SketchUp Mac. I tried other extensions besides png, e.g. jpg, and the behavior is the same: the default importer’s load_file is called instead of mine.


#7

Glad to hear it’s not just me! Thanks for looking into it.