1

I written some plugins for sketch-up. I want to know that is it possible to know the texture position that is vertical or horizontal programmatically in ruby ?

Ex: I'm using Sketch-up for wood working and whenever I apply material to the model I should take care of grains. So that I want to know that the wood grains are in horizontal or vertical. Selecting face then clicking Texture->position we can make horizontal grains to vertical and vice-verse. After applying materials programmatically how should I know the grains are horizontal or vertical.

Is there any solution?

Uri Agassi
  • 36,848
  • 14
  • 76
  • 93

2 Answers2

0

When you say horizontal and vertical that implies from general direction - do you have that via Ruby code some how? Do you determine via the bounds of the group/component?

You can use face.getUVHelper to extract the position of the texture on the face. (You can also do so via PolygonMesh - but that's only really useful if you are interested in all the other data you get from that.)

Once you have the UV position you can use that to compare against your desired direction. (This would also assume that all your textures have the grain in the same direction.) Based on that you can then reposition the texture using face.position_texture.

Edit: a very native version can be seen in my UV Toolkit plugin. It's a very old plugin and not the greatest example - it enforces restrictions that the face must be a quad and only rotates in 90 degrees. But it shows how to get UV data and change the orientation of the texture. A better generic version would be to get four points of the plane of the face (not colinear) and get the UV data for each point. Then transform the UV data by the rotation needed to get the direction you want and set the new UV data.

thomthom
  • 2,854
  • 1
  • 23
  • 53
-1

Disclaimer

I am horrible with 3-Dimensions (so please don't ask for help with the appropriate vectors) and I have never used sketchup ever. Not even once until right now but...

Information

You must select the Face object.

model = Sketchup.active_model
entities = model.active_entities
face_entities = entities.find_all{|e| e.class == Sketchup::Face}
#=>[#<Sketchup::Face:0xea645d8>]
face_entities.each{|e| puts e.get_texture_projection(false)}
#=> output of each texture projection for the back side
#nil means that the projection is unchanged from it's initial position 

So here is what you can do

#true returns front, false returns the back texture as a Vector object
get_texture_projection(true || false)

#submit a vector [0,1,0] || nil , true sets front || false sets the back
#nil will reset the projection back to it's initial position
set_texture_projection([0,1,0],true || false)
#texture is now rotated 90 degrees on the Y axis 
set_texture_projection([1,0,0],true || false)
#texture is now rotated 90 degrees on the X axis

you can also access the material through material and back_material

face.material.name
#=> [Wood_ Floor]
face.material.texture.filename
#=>Wood_Floor.jpg

Hope this helps you out it took a bit of digging but based on using the built in console in Sketchup it works.

P.S. I love that this program has a built in Ruby API that's is awesome.

engineersmnky
  • 25,495
  • 2
  • 36
  • 52
  • That gets and sets the projection direction of the texture, but not the position. – thomthom May 20 '14 at 13:56
  • @thomthom Thanks for the down vote I appreciate it sincerely. Although since he wanted to know what direction the projection was my answer in not wrong. He did not ask for the position he stated that he wanted to know the direction of the grain. Which can be set through Texture > position. – engineersmnky May 20 '14 at 16:50
  • He asked for the orientation of the texture - vertical or horizontal. You get that via the position. You figure out how the UV data is oriented on the face in relationship to a vector direction. If you have a face with normal [0,0,1] and then set the texture projection to a perpendicular one [1,0,0] you end up with a few pixels of the texture stretched across the face in an even blur. It would have worked with 3d textures, but not with 2d. – thomthom May 21 '14 at 09:29
  • `face.position_material` is the same as `FaceContextMenu > Texture > Position` in the UI. `face.set_texture_projection` is the same as `FaceContextMenu > Texture > Projected` You can see the effect of the projected property in this image: http://images.shapeways.com/model/picture/674x501_825059_702506_1355255796.jpg?key=ef1406b57007fe279dc9349b56af57a9 – thomthom May 21 '14 at 09:33