Skip to content

Instantly share code, notes, and snippets.

@thebusytypist
Created August 31, 2015 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thebusytypist/672bfd018864b023f76c to your computer and use it in GitHub Desktop.
Save thebusytypist/672bfd018864b023f76c to your computer and use it in GitHub Desktop.
Transform UV coordinates to world space
bl_info = {
"name": "InverseUVMapping",
"description": "Inverse UV coordinate",
"author": "TheBusyTypist",
"category": "Object"
}
import bpy
import bmesh
import mathutils
from mathutils.geometry import barycentric_transform, intersect_point_tri_2d
class InverseUVMapping(bpy.types.Operator):
"""Inverse UV coordinate"""
bl_idname = "object.inverse_uv"
bl_label = "Inverse UV coordinate"
bl_options = {"REGISTER"}
def execute(self, context):
for area in context.screen.areas:
if area.type == "IMAGE_EDITOR":
image_editor = area.spaces.active
uv_cursor = image_editor.cursor_location.to_3d()
uv_cursor.x /= image_editor.image.size[0]
uv_cursor.y /= image_editor.image.size[1]
bm = bmesh.new()
bm.from_mesh(context.object.data)
bmesh.ops.triangulate(bm, faces=bm.faces)
uv_layer = bm.loops.layers.uv.active
for face in bm.faces:
u, v, w = [l[uv_layer].uv.to_3d() for l in face.loops]
if intersect_point_tri_2d(uv_cursor, u, v, w):
x, y, z = [v.co for v in face.verts]
co = barycentric_transform(uv_cursor, u, v, w, x, y, z)
world_matrix = context.object.matrix_world
p = world_matrix * co
print(p)
context.scene.cursor_location = p
break
return {"FINISHED"}
def register():
bpy.utils.register_class(InverseUVMapping)
def unregister():
bpy.utils.unregister_class(InverseUVMapping)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment