Created
September 22, 2017 06:57
-
-
Save a-nakanosora/be7e2ec9adf764b302c4b863a530ed04 to your computer and use it in GitHub Desktop.
blenderscript - surfacepoint_from_uv_coord.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
surfacepoint_from_uv_coord() | |
forked from: | |
[SOLVED] Get 3D location of mesh surface point from UV Parameter? | |
https://blenderartists.org/forum/showthread.php?379027-Get-3D-location-of-mesh-surface-point-from-UV-Parameter | |
''' | |
import bpy | |
import bmesh | |
from mathutils.geometry import barycentric_transform, intersect_point_tri_2d | |
from mathutils import Vector | |
def map_flat_pt_to_face(flat_pt, uv_layer, face): | |
pa, pb, pc = [l[uv_layer].uv.to_3d() for l in face.loops] | |
pd, pe, pf = [v.co for v in face.verts] | |
return barycentric_transform(flat_pt, pa, pb, pc, pd, pe, pf) | |
def surfacepoint_from_uv_coord(uv, ob, uv_layer=None, worldSpace=True): | |
puv = Vector([uv[0], uv[1], 0.0]) | |
bm = bmesh.new() | |
bm.from_object(ob, bpy.context.scene) | |
bmesh.ops.triangulate(bm, faces=bm.faces, quad_method=1, ngon_method=1) | |
uv_layer = uv_layer or bm.loops.layers.uv['UVMap'] | |
for face in bm.faces: | |
pa, pb, pc = [l[uv_layer].uv.to_3d() for l in face.loops] | |
if intersect_point_tri_2d(puv, pa, pb, pc): | |
localPos = map_flat_pt_to_face(puv, uv_layer, face) | |
return ob.matrix_world*localPos if worldSpace else localPos | |
return None | |
def drv_surfacepoint_from_uv_coord(uv, objectName, uv_layer=None, worldSpace=True): | |
ob = bpy.data.objects[objectName] | |
a = surfacepoint_from_uv_coord(uv, ob, uv_layer, worldSpace) | |
return a if a is not None else Vector((0,0,0)) | |
# Make this function available to drivers | |
bpy.app.driver_namespace['drv_surfacepoint_from_uv_coord'] = drv_surfacepoint_from_uv_coord | |
def test(): | |
uv = (0.123, 0.456) | |
ob = bpy.data.objects['Suzanne'] | |
a = surfacepoint_from_uv_coord(uv, ob) | |
print('uv', uv) | |
print('res', a) | |
if a is not None: | |
bpy.context.scene.cursor_location = a | |
test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment