How to add all SU materials in our list?

Instead of …

Dir.chdir("C:/ProgramData/SketchUp/SketchUp 2021/SketchUp")

… use the block form of #chdir so that after the block, the previous working directory is automatically restored.

Here is an example:

NOTE: The #each_child loops are not needed because Dir::glob can recursively “walk” the subdirectories if ** is used as a pattern prefix.

module AuthorNamspace
  module SomeExtension

    extend self

    WIN = Sketchup.platform == :platform_win
    MAC = !WIN

    MATL_FILENAMES = []

    USER_MATL_DIR = Sketchup.find_support_file("Materials")
    USER_DIR = File.dirname(USER_MATL_DIR)

    if WIN
      PROG_MATL_DIR = USER_MATL_DIR.gsub(
        ENV["APPDATA"].gsub(/\\\\|\\/,'/'),
        ENV["PROGRAMDATA"].gsub(/\\\\|\\/,'/')
      )
    else # Mac uses the user path
      PROG_MATL_DIR = USER_MATL_DIR.dup
    end

    def get_material_filenames
      Dir.chdir(PROG_MATL_DIR) do
        # Recursively list all SKM files in subdirectories:
        MATL_FILENAMES << Dir.glob("**/*.{skm,SKM}")
      end
      if WIN
        Dir.chdir(USER_MATL_DIR) do
          # Recursively list all SKM files in subdirectories:
          MATL_FILENAMES << Dir.glob("**/*.{skm,SKM}")
        end
      end
      MATL_FILENAMES.flatten!
    end

  end
end

Referring to the example above, your plugin must create a plugin directory in the user materials folder path. In the example … USER_MATL_DIR.

Then you copy SKM files from your plugin’s directory to the new materials subdirectory. (Ie, your RBZ will have a “matls” distribution subfolder beneath the extension’s folder.)

The user may need to restart SketchUp for the new materials to be listed in the Materials inspector. (Or your extension can try to call the UI.refresh_inspectors method.)

1 Like