Apply images to terrain using ruby code
-
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 rubyHelp is very much appreciated
-
@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 rubyHelp 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) }
-
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.
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
-
@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
-
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. -
@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.
Advertisement