gpt4 book ai didi

python - 如何在 UV 贴图边缘获取数据并进行编辑?

转载 作者:行者123 更新时间:2023-12-02 16:45:04 26 4
gpt4 key购买 nike

我有一张 UV 贴图: enter image description here

我想像这样在连接的网格边周围获取纹理像素(以红色选中): enter image description here

如何使用 Python Blender API 做这样的事情?

最佳答案

制作“UV”bmesh

enter image description here

给定上面带有 UV 的网格对象,根据 uv 制作一个 bmesh

  • 循环脸
  • loop face loops 在 uv.x, uv.y, 0 处向新的 bmesh 添加顶点
  • 从顶点创建一张脸
  • 删除 double
  • (可选)溶解非边界边缘
  • (可选)删除面孔。
  • 使用原始顶点和面的索引向顶点和面添加一个整数层。

测试脚本,以对象模式运行

import bpy
import bmesh

context = bpy.context
ob = context.object
me = ob.data
bm = bmesh.new()
bm.from_mesh(me)
uvbm = bmesh.new()

uv_layer = bm.loops.layers.uv.verify()
vert_index = uvbm.verts.layers.int.new("index")
face_index = uvbm.faces.layers.int.new("index")
# adjust uv coordinates
for face in bm.faces:
fverts = []
for loop in face.loops:
uv = loop[uv_layer].uv
v = uvbm.verts.new((uv.x, uv.y, 0))
v[vert_index] = loop.vert.index
fverts.append(v)
f = bmesh.ops.contextual_create(uvbm, geom=fverts)["faces"].pop()
f[face_index] = face.index

# remove doubles

bmesh.ops.remove_doubles(uvbm, verts=uvbm.verts, dist=1e-7)

'''

# ignore face indices of original if using any option here
# optionally disolve non boundary edges

bmesh.ops.dissolve_edges(uvbm,
edges=[e for e in uvbm.edges if not e.is_boundary],
)


# optionally remove faces

faces = uvbm.faces[:]
while faces:
uvbm.faces.remove(faces.pop())
'''
#make an object to see it
me = bpy.data.meshes.new("UVEdgeMesh")
uvbm.to_mesh(me)
ob = bpy.data.objects.new("UVEdgeMesh", me)
bpy.context.collection.objects.link(ob)
ob.show_wire = True


# make a LUT based on verts of original
from collections import defaultdict

edge_pairs = defaultdict(list)
boundary_edges = [e for e in uvbm.edges if e.is_boundary]

for e in boundary_edges:
key = tuple(sorted(v[vert_index] for v in e.verts))
edge_pairs[key].append(e)

# print result, add text object to show matching edges
# Make sure to remove code below before running on detailed UV as in question,
# adding that many text objects via operator
# will slow code down considerably.

uvbm.verts.ensure_lookup_table()
for key, edges in edge_pairs.items():

print(key, [e.index for e in edges])

for e in edges:
if not e.is_boundary:
continue
f = e.link_faces[0]
p = (e.verts[0].co + e.verts[1].co) / 2
p += (f.calc_center_median() - p) / 4
bpy.ops.object.text_add(radius=0.04, location=p)
bpy.context.object.data.body = f"{key}"

创建一个bmesh,这里转成mesh

enter image description here

“UV”bmesh 的 xy 坐标是 UV 坐标。唯一的边是 uv 边界边。使用数据转换为像素坐标。

使用此处的方法使用此处的方法将verts与original相关联https://blender.stackexchange.com/a/70729/15543将原始顶点/边缘的索引存储在“UV”bmesh 的数据层中。

上面示例的输出。第一行,由原始网格的顶点 0 和 1 构成的两条边是“UV”bmesh 中的边 0 和 14。

(0, 1) [0, 14]
(0, 2) [1, 17]
(2, 3) [2, 4]
(1, 3) [3, 22]
(2, 6) [5, 16]
(6, 7) [6, 8]
(3, 7) [7, 23]
(4, 6) [9, 19]
(4, 5) [10, 12]
(5, 7) [11, 20]
(0, 4) [13, 18]
(1, 5) [15, 21]

编辑:为了进一步可视化这一点,从每个边向每个面添加一个文本对象。例如,查看由顶点 (i, j) 构成的边在两个面上的匹配位置。每个文本对象都位于从边缘中心到面中心的四分之一处。

enter image description here或者可以简单地在顶点处打印原始顶点索引

请记住,uvbmesh 在 UV 坐标中映射到 U 和 V 中的实数范围 [0, 1]。像素坐标只是将其映射到基于图像维度的离散整数范围。

关于python - 如何在 UV 贴图边缘获取数据并进行编辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60713817/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com