sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    [PLUGIN] Add Texture from MaterialColor

    Scheduled Pinned Locked Moved Plugins
    10 Posts 4 Posters 11.4k 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.
    • rami_lpmR Offline
      rami_lpm
      last edited by rami_lpm

      This plugin checks for any material without a texture, then assigns one(4x4 px bmp) based on the color of the material.
      This is useful when importing in Lumion. It makes editing material properties easier(no need to generate the texture bitmap).

      This is my first ruby published, so I welcome any comments


      v0.2 - better error message.

      1 Reply Last reply Reply Quote 0
      • A Offline
        Aerilius
        last edited by

        Thanks for posting!

        As for file paths, I don't know whether backslash paths will work for OS X users. Ruby usually abstracts filepaths and uses internally the standard unix paths.
        Instead of doing:
        matPath=File.dirname( Sketchup.active_model.path ) + '\mat\\' thumbnail_fn = matPath + e.name + '.bmp'
        you could do:
        mat_path = File.join(File.dirname(Sketchup.active_model.path), 'mat') thumbnail_fn = File.join(mat_path, e.name + '.bmp')
        This way, Ruby will care about using the correct delimiter.
        In addition to that, active_model.path is empty if the model has not been saved yet. In that case we either have to let the user choose a location, or we fallback to a default directory:
        Sketchup.active_model.path || ENV["HOME"] || ENV["HOMEPATH"]

        1 Reply Last reply Reply Quote 0
        • rami_lpmR Offline
          rami_lpm
          last edited by

          well, testing under windows vista
          mat_path = File.join(File.dirname(Sketchup.active_model.path), 'mat')
          returns
          c:\folder\subfolder/mat
          so ruby can't create the folder

          Sketchup.active_model.path || ENV["HOME" || ENV["HOMEPATH"]]
          does nothing for me(afaik).
          I prevent the user from running the script unless the model is saved.
          cmd.set_validation_proc

          I'd have to make a separate .rb for mac, and i can't test it.

          1 Reply Last reply Reply Quote 0
          • A Offline
            Aerilius
            last edited by

            Sorry, I oversaw that:
            File.expand_path expands all truncated path (or links) and translates Windows into unix delimiters. For unsaved models, active_model.path is an empty string (not nil), so we add an explicit conditional:
            model_path = File.expand_path(Sketchup.active_model.path) model_path = ENV["HOME"] || ENV["HOMEPATH"] || return if model_path.empty?

            @unknownuser said:

            I'd have to make a separate .rb for mac, and i can't test it.

            I don't think so. As long as we can avoid introducing platform-specific calls, we should keep one single version (which is also much easier to maintain). Most scripts (that use the SketchUp API or standard Ruby) are cross-platform anyways. Here are a lot of helpful people that help testing.

            1 Reply Last reply Reply Quote 0
            • rami_lpmR Offline
              rami_lpm
              last edited by

              I think I can make that work, I'll upload the corrected version as soon as I have it. Thanks for the advise.

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

                The hybrid c:\folder\subfolder/mat should still work when checking/making the folder - it just looks odd...
                Aerilius's File.expand_path(...) will simply make all \ into /.
                The only time you need to keep a \ as a delimiter in a path is if you are on a PC and you are going to use it in a system/DOS call like a .bat or .cmd file...
                PCs can otherwise cope with either \ OR / MACs always want /, so .expand_path or .tr("\\","/") will both fix any path to suit most things, and at least display neater if it's printed out...
                To make the path PC AND DOS suited you can reverse the path delimiter logic as .tr("/","\\") 😕

                TIG

                1 Reply Last reply Reply Quote 0
                • rami_lpmR Offline
                  rami_lpm
                  last edited by

                  Done, I used File.expand_path(...) and modified to allow running the plugin without saving.
                  Anyways, it's a good idea to save before.

                  1 Reply Last reply Reply Quote 0
                  • A Offline
                    Aerilius
                    last edited by

                    Simply add after line 97:

                        e.texture.size = [1.m, 1.m]
                    

                    Ramiro, the rescue block gives out an error assuming an error with non-ASCI characters. But there could be sooooo much else that causes errors (that's why it is sometimes good to output the original error message). If you add this in line 110, the folder deletes successfully:

                      Dir.entries(matPath).each{|f|
                        file = File.join(matPath, f)
                        next unless File.file?(file)
                        File.delete(file)
                      }
                      Dir.rmdir(matPath)
                    
                    1 Reply Last reply Reply Quote 0
                    • rami_lpmR Offline
                      rami_lpm
                      last edited by

                      Hi Aerilius, thanks for the tip.
                      I modified the block to include the original error message. I also added your corrections, but the problem (in my PC) is that in a folder with non-ASCII chars, I can't even create the temporary mats subfolder.

                      1 Reply Last reply Reply Quote 0
                      • deaneauD Offline
                        deaneau
                        last edited by

                        Ok is an older thread however this is what i need however i would have a given name of material for the texture and don´t touch material this have textures.

                        the plugin give the noname Material Name and Texture Name

                        Snap_2015.10.29_00h31m17s_003_Materials.png

                        i would have this: Material Name and Texture Name (both the same)

                        Snap_2015.10.29_00h32m07s_004_Materials.png

                        this Material have a Texture

                        Snap_2015.10.29_00h33m35s_005_Materials.png

                        the Plugin change the name of the material, this i doesn´t want

                        Snap_2015.10.29_00h36m35s_006_Materials.png

                        MADE, BORN AND LIVING IN BERLIN
                        Big Thank You to all Programmers
                        Some German words are so long that they have a perspective. M.Twain

                        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