Unable to retrive face texture uv properly for certain objects

Hi I am trying to develop a sketchup exporter for my custom file format using ruby api. Most of the uv are showing correctly. But some objects uvs are not showing properly, But if I explode the group I am getting proper uv coordinates.

the following code I am using to get the uv.

def self.process_entity_mesh(entity, front_material, back_material)

	front_material = entity.material.nil? ? front_material : entity.material
	back_material = entity.back_material.nil? ? back_material : entity.back_material

	mesh = entity.mesh(7)
	
	#texture_writer = Sketchup.create_texture_writer
	uv_helper = entity.get_UVHelper(true, true)
	
	vertices = []
	uv = []

	# Process each polygon (triangle)
	mesh.polygons.each do |polygon|
		next if polygon.length != 3 # Skip if it's not a triangle
	 
		# Get the front vertices and uv
		polygon.each do |index|
			vertex = mesh.point_at(index)

			uvq = uv_helper.get_front_UVQ(vertex)
			uv = [uvq.x/ uvq.z, uvq.y/ uvq.z]          

			vertices.concat(vertex.to_a.map(&:to_f))
			uvs.concat(uv.to_a.map(&:to_f))
		end
	end
end

Your code does not show checking if a material assignment has been made at the group or instance level.

model = Sketchup.active_model
instance = model.active_path.last
if instance && instance.material
  # There is a material assignment at instance level.
  # If no face material use instance material.
  # If ALL edges of face are NOT soft, use identity UVs
  # If SOME of face's edges are softened, then it is part of a surface.
  # In the latter case you may need to adjust the UVs so that the texture
  #   appears contiguous across the surface.
else
  # At model level OR no assignment at instance level.
  # If face has material use face material's UVs
end

Secondly, you code does not handle quads or higher order polygons. (SketchUp does not divide everything into triangles.)


Lastly, check the official API Issue Tracker at GitHub for open issues.

Hi,

Thank you for your kind reply. I still do not understand the issue clearly, as I am very new to SketchUp. I am attaching a file with this message. In the file, the UV mapping for the vertical plane is showing correctly with the code above, but the UV mapping for the horizontal plane is not correct. It seems like there is some extra UV scaling applied to it, possibly from a parent entity. I am unable to figure out where this extra UV scaling is being applied.
ExampleUVIssue.skp (4.3 MB)

A group or component instance can have a transformation applied which affects how SketchUp displays the texture for materials.

The scaling factor is part of the transformation matrix.

trans  = group.transformation
xscale = X_AXIS.clone.transform!(trans).length.to_f
yscale = Y_AXIS.clone.transform!(trans).length.to_f
zscale = Z_AXIS.clone.transform!(trans).length.to_f

NOTE: X_AXIS, Y_AXIS, and Z_AXIS are normalized vectors defined globally by the SketchUp Ruby API. (Do not change them. Notice how I clone them in the example before modifying them.)

As said, there are some open issues with material texture UVs. Check the issue tracker.