[code] Renaming Materials by their filename in bulk by directory

[code] Renaming Materials by their filename in bulk by directory


EDIT3: version 3 is FIXED

UPDATED to also change the “title” element in "documentProperties.xml" file. (Thanks to TIG)

The materials were invalidated in version 2 because the ruby was running faster than the file system and the Zip archive undpates. Basically both xml documents were the same as the second because Ruby was changing the one reference too fast.

In version 3 I created two separate update paths, strings etc., so that the xml data would not get “crossed”.

rename_my_matls_3.rb (1.8 KB)


@sustainablebuilders You’d fire it off via …

rename_my_matls("D:/Storage Documents/SketchUp")

The contents of the ruby file … FIXED ver 3

# Might need to do ...
# Gem::Install("rubyzip")
# ... then ...
require 'zip'

# This method will rename the SKM Material's
# display name to match that of the filename.
# Suggested this be done on a cloned folder of SKM
# files first, and then test the results !
#
# ver: 3
#
def rename_my_matls(
  start_dir = ENV["HOME"]
)
  path = UI.select_directory(
    directory: start_dir,
    title: 'Select Materials Folder'
  )
  return unless path
  Dir::chdir(path) do |dir|
    #
    matls = Dir["*.skm"]
    temp  = Sketchup::temp_dir
    path1 = File.join(temp,"document.xml")
    path2 = File.join(temp,"documentProperties.xml")
    #
    matls.each do |skm|
      #
      matl_name = File.basename(skm,".*")
      Zip::File.open(skm) do |zipfile|
        #
        modes = File::CREAT|File::TRUNC|File::RDWR
        #
        doc1 = zipfile.find_entry("document.xml")
        next if doc1.nil?
        xml_str1 = zipfile.read( doc1 )
        xml_str1.gsub!(
          /name="[\d\s\w]+"/,
          %[name="#{matl_name}"]
        )
        File::open( path1, modes ) { |file| file.write(xml_str1) }
        zipfile.replace( doc1, path1 )
        #
        doc2 = zipfile.find_entry("documentProperties.xml")
        next if doc2.nil?
        xml_str2 = zipfile.read( doc2 )
        xml_str2.gsub!(
          /<dp:title>[\d\s\w]+<\/dp:title>/,
          %[<dp:title>#{matl_name}</dp:title>]
        )
        File::open( path2, modes ) { |file| file.write(xml_str2) }
        zipfile.replace( doc2, path2 )
        #
      end # zipfile
      #
    end # each matls
    #
    File.delete(path1) if File.exist?(path1)
    File.delete(path2) if File.exist?(path2)
    #
  end # change dir (old working dir is restored automatically)
  #
end ### rename_my_matls()
3 Likes

Sorry but I have no idea how to run it. Put that ruby in plugins folder bu get error messages on sketchup start.
Tryied to run it off console but to no avail. Maybe You could write some simpler explanation by any chance. I use Sketchup 2020 Pro.

Thank You in advance
2023-01-20_12h21_18

Please post error text not images.

This topic is a code example for programmers. It is not a plugin that is “run”.
Ie, it really is meant to be a method within a proper extension, not a file dropped into the “Plugins” folder. But it still would work if …

Most likely because (as the example says at the top) you need to have the ruby-zip gem installed.
The likely error would be a LoadError with a message like "cannot load such file - zip.rb".

When this error is encountered, Ruby stops parsing the file so the method is never defined. This is why your attempts to execute the method result in a NoMethodError.

1 Like


Is this something one can rename within the Ruby Code Editor? Needing to rename >500 jpg files within existing skm files that didn’t seem to inherit the correct file name.

I do not use the Ruby Code Editor, myself.

I am not sure I understand your question … You want to rename the method given above?

This code example is seven years old. I did not even remember that I posted this.

It looks (after a quick look) like it renames a couple of XML attributes that are used by SketchUp to display the name of the materialnot the name(s) of the texture used by the material.

So, use it as an example. What it is meant for. To learn. If it doesn’t do exactly what you want, study it and Ruby and modify it for your own use.

2 Likes

AI have taken your advice and dived into the developers world :sweat_smile:
Initially, I got a working code real fast, but then went into the UX/UI design and decided to use the modus bootstrap and spent many hours on learning how javascript, html and ruby interacts with each other…
I wanted to use nokogiri for extracting the node’s inside of the xml’s, instead of directly writing, because I think it will benefit my other extension (LayOutToPDF).
Still some questions about the other node’s inside the document.xml.
I also wanted to integrate your example of how to set up a multiClass extension, with a loader file and everything.

Anyway’s here are some screenshots:





Here is the .rbz:
mw_material_skimmer.rbz (12,9 MB)
tested on Windows, SketchUp 2024 and 2025.
New PBR’s are being read, but the bitmaps won’t be shown in the material tab,
it is on the roadmap!

:thinking:

Don’t be to harsh on me now:)

2 Likes

So that’s where you’ve been off ! We’ve not seen you around much lately :wink:

1 Like