Here is a scipt that rotates a texture on a face by a custom angle.
For now, the script works just for faces in xy plane (red-green) but it can be a usefull example to understand how to position a texture on a face.
The UV helper retrieves the vertex coordinates. So, if you have a hexagonal face, you will have 12 points (6 vertexes). But position_material method uses maximum 8 points (4 vertexes)... This is because Open GL treats any surface as a composition of triangles or quadrilaterals (sorry if misswriten). This is why i don't know yet how to make the script working for faces oriented arbitrary (not normal on an axis). I use the entitie's boundingbox as a reference.
For better understanding of what I've done, see this image http://bayimg.com/bALMEaABJ
require 'sketchup.rb'
def rotate_texture
prompts = ["rotation angle "]
value = [0.0] #use float for better precission
result = inputbox prompts, value, "Texture Rotator"
return if not result
angle = result
#------------------------------------------------------------------------------------------------------
ox = Math.cos(angle[0].degrees) #projection coefficient
oy = Math.sin(angle[0].degrees) #projection coefficient
selected = Sketchup.active_model.selection[0]
if( selected.is_a? Sketchup;;Face ) #check if you selected a face
boundingbox = selected.bounds
material = selected.material
texture = material.texture
bw = boundingbox.width
proj_x = ox*bw
proj_y = oy*bw
tex_w = (texture.width).inch
ratio = bw/tex_w #calculate the ratio between image dimension and boundingbox dimension,
#to preserve same tiling
pts = []
pts[0] = [0, 0, 0]
pts[1] = [proj_x, proj_y, 0]
coords = [pts[0], [0, 0], pts[1], [ratio, 0]]
selected.position_material(material, coords, true) #here you put the material using new coordinates
else
UI.messagebox "You must select a face !"
end
end
#------------------------------------------------------------------------------------------------------
filename="texture_rotator.rb"
if !file_loaded?(filename)
plugins_menu = UI.menu("Plugins")
if plugins_menu
plugins_menu.add_separator
plugins_menu.add_item("Rotate texture by angle") {rotate_texture}
end
file_loaded(filename)
end
Hope it's helpfull not hellpful ![:stuck_out_tongue: π](https://community.sketchucation.com/assets/plugins/nodebb-plugin-emoji/emoji/android/1f61b.png?v=3orp1bvic7q)
regards!