sketchucation logo sketchucation
    • Login
    πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Material names changed by SketchUp

    Scheduled Pinned Locked Moved Developers' Forum
    4 Posts 4 Posters 509 Views 4 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • OricAtmosO Offline
      OricAtmos
      last edited by

      Hi,

      I have a small problem with material names. Let's say I apply a material named 'Stonewall' from one of the Material Collections in the paint bucket tool and there already is a material with this name in the model, SketchUp will rename the new material to 'Stonewall1' and add it to the model under this name. My problem is that I want to know the original name of this material, because I need to load additional information from a file named 'Stonewall.mat'. Is there a way to get the original name? I suppose there isn't but it can't hurt to ask. πŸ˜„

      Thanks
      Ralf

      1 Reply Last reply Reply Quote 0
      • Dan RathbunD Offline
        Dan Rathbun
        last edited by

        Did you notice any difference between the strings returned by the name() and display_name() methods for the Material class ??

        Failing that... you can get the full path string for the material's Texture, thus:
        (... where matl is the reference to the Sketchup::Material instance in question.)

        if matl.texture # nil if material has no texture
          matl_path = matl.texture.filename.gsub(/\+/,'/')
          matl_ext  = File.extname( matl_path )
          matl_diskname = File.basename( matl_path, matl_ext )
        end
        

        gsub(/\+/,'/') just makes sure to change all occurances of one or more \ chars to a single / char.

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • thomthomT Offline
          thomthom
          last edited by

          There is no property that stores the original name. But you could trim of the trailing digits and assume the remainder is the original name.

          This regex captures everything but the trailing digits: /(.*?)\d+$/

          Thomas Thomassen β€” SketchUp Monkey & Coding addict
          List of my plugins and link to the CookieWare fund

          1 Reply Last reply Reply Quote 0
          • TIGT Offline
            TIG Moderator
            last edited by

            Recent versions of the API allow you to change a material's name in code.
            If you have a tool that's using a file of data that is material-name specific then clearly you need to know which of the possible materials with similar names it is to be applied to.
            There are several possibilities.

            1. You manually add a material from a library with the name 'Stonewall', there is NO preexisting material with that name, and therefore the new material gets called 'Stonewall'.
            2. You have a material already in the model named 'Stonewall' and you manually add a material from a library with the same name, and therefore the new material gets called 'Stonewall1'.
            3. You have a material already in the model named 'Stonewall' and you manually add a material from a library with the same name, and therefore because there's also a material in the model named 'Stonewall1' the new material gets called 'Stonewall2' [this will go on until 'Stonewall999' and beyond!].
            4. You have a material already in the model named 'Stonewall1' and you manually add one from a library with the name 'Stonewall', and because there's NO material in the model named exactly 'Stonewall' so the new material gets called 'Stonewall'.
              So how do you decide which is the real 'Stonewall' material you want?
              It's likely that the model.materials collection is in the order they are created [at least in a session].
              Therefore to find the 'true' 'Stonewall' material try this:
              name='Stonewall' ### or any other material name to be found... mat=Sketchup.active_model.materials.to_a.find_all{|m|m.name=~/^#{name}/}[-1]
              Thus the last match in the matches list is the material last added, so logically is the one you want?
              To get that material's name use mat.name...

            TIG

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Buy SketchPlus
            Buy SUbD
            Buy WrapR
            Buy eBook
            Buy Modelur
            Buy Vertex Tools
            Buy SketchCuisine
            Buy FormFonts

            Advertisement