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

    Apply images to terrain using ruby code

    Scheduled Pinned Locked Moved Developers' Forum
    6 Posts 2 Posters 582 Views 2 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.
    • S Offline
      sonjachr
      last edited by

      I found the following instruction in SU documentation:

      Apply images to terrain models:

      Import an image as an object by going to file > import and leave the "use as texture" box unchecked.

      Scale and position the image over the site plan so it is properly aligned. Right click on the image and choose Explode. Using the Paint Bucket, Alt-click on the image to pick up the material, then click on any part of the terrain to project the image down onto the terrain surface.

      I almost managed to code it in ruby, but I am stuck when it comes to the Alt-click.
      My question: how can I send the Alt-click using ruby or how can I sample the image using ruby

      Help is very much appreciated

      1 Reply Last reply Reply Quote 0
      • sdmitchS Offline
        sdmitch
        last edited by

        @sonjachr said:

        I found the following instruction in SU documentation:

        Apply images to terrain models:

        Import an image as an object by going to file > import and leave the "use as texture" box unchecked.

        Scale and position the image over the site plan so it is properly aligned. Right click on the image and choose Explode. Using the Paint Bucket, Alt-click on the image to pick up the material, then click on any part of the terrain to project the image down onto the terrain surface.

        I almost managed to code it in ruby, but I am stuck when it comes to the Alt-click.
        My question: how can I send the Alt-click using ruby or how can I sample the image using ruby

        Help is very much appreciated

        Scaling and positioning the image over the site plan isn't really necessary. When you explode the Image, a single face is created in the model with the Image as the applied material. The following code produced the results shown in the gif.

        mod = Sketchup.active_model
        ent = mod.active_entities
        sel = mod.selection
        img = ent.grep(Sketchup;;Image)[0]
        grp = ent.grep(Sketchup;;Group)[0]
        old = ent.grep(Sketchup;;Face)
        img.explode
        new = (ent.grep(Sketchup;;Face) - old)[0]
        mat = new.material;xdel=grp.bounds.width;ydel=grp.bounds.height
        grp.entities.grep(Sketchup;;Face).each{|f|
         mp = []
         f.outer_loop.vertices.each{|v|
          p = v.position
          u = p.x/xdel
          v = p.y/ydel
          mp<<p<<[u,v,1.0]
         }
         f.position_material(mat,mp,true)
        }
        
        

        Apply Image.gif

        Nothing is worthless, it can always be used as a bad example.

        http://sdmitch.blogspot.com/

        1 Reply Last reply Reply Quote 0
        • S Offline
          sonjachr
          last edited by

          Thanks a lot for the quick response.

          Your code is working perfectly.

          However I've got a further problem and thus a new question. I am sure it is easy for you, but it is hard for me to get the right solution. I searched a lot in the Forum but did not find an answer to my problem.

          When I load the below image and run your code to apply the image to the terrain I get what you see in the second image.
          image to be loadedTerrain with texture

          Using a different image works fine, so I guess my image export was not done correctly.
          Do you have any ideas?

          Many Thanks in advance

          1 Reply Last reply Reply Quote 0
          • sdmitchS Offline
            sdmitch
            last edited by

            @sonjachr said:

            Thanks a lot for the quick response.

            Your code is working perfectly.

            However I've got a further problem and thus a new question. I am sure it is easy for you, but it is hard for me to get the right solution. I searched a lot in the Forum but did not find an answer to my problem.

            When I load the below image and run your code to apply the image to the terrain I get what you see in the second image.
            [attachment=1:2vgsbols]<!-- ia1 -->Mer_Bie_01_new.png<!-- ia1 -->[/attachment:2vgsbols][attachment=0:2vgsbols]<!-- ia0 -->Mer_Bie_01_proj.png<!-- ia0 -->[/attachment:2vgsbols]

            Using a different image works fine, so I guess my image export was not done correctly.
            Do you have any ideas?

            Many Thanks in advance

            My code assumed that the origin of the terrain group was 0,0. It would appear yours is not and that will cause the shift.

            Perhaps this modified code will solve that problem.

            mod = Sketchup.active_model
            ent = mod.active_entities
            sel = mod.selection
            img = ent.grep(Sketchup;;Image)[0]
            grp = ent.grep(Sketchup;;Group)[0]; gt = grp.transformation
            old = ent.grep(Sketchup;;Face)
            img.explode
            new = (ent.grep(Sketchup;;Face) - old)[0]
            mat = new.material; grp.transform! gt.inverse
            xdel=grp.bounds.width;ydel=grp.bounds.height
            xmin=grp.bounds.min.x;ymin=grp.bounds.min.y
            grp.entities.grep(Sketchup;;Face).each{|f|
             mp = []
             f.outer_loop.vertices.each{|v|
              p = v.position
              u = (p.x-xmin)/xdel
              v = (p.y-ymin)/ydel
              mp<<p<<[u,v,1.0]
             }
             f.position_material(mat,mp,true)
            }
            grp.transform! gt
            
            
            
            

            Nothing is worthless, it can always be used as a bad example.

            http://sdmitch.blogspot.com/

            1 Reply Last reply Reply Quote 0
            • S Offline
              sonjachr
              last edited by

              Many Thanks,
              this works, I really appreciate your help!
              I feel ashamed as I should have found the solution by myself. I will try to work harder on my Sketchup/Ruby knowledge, but I will never become an expert as you are.

              1 Reply Last reply Reply Quote 0
              • sdmitchS Offline
                sdmitch
                last edited by

                @sonjachr said:

                Many Thanks,
                this works, I really appreciate your help!
                I feel ashamed as I should have found the solution by myself. I will try to work harder on my Sketchup/Ruby knowledge, but I will never become an expert as you are.

                Great, glad I could help.

                Nothing is worthless, it can always be used as a bad example.

                http://sdmitch.blogspot.com/

                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