从文本文件编码网格
Posted
技术标签:
【中文标题】从文本文件编码网格【英文标题】:Encode Mesh from text-file 【发布时间】:2015-07-20 16:01:04 【问题描述】:我想用这个文本语法结构将 3D 模型编码到 Blender:
<p>
c(255,84,22)
fs(-1)
p(-9,-8,27)
p(-9,-23,27)
p(-7,-24,63)
p(-7,-11,63)
</p>
与创建网格相关的是:
<p>
标记一个人脸容器(就像在 html 中一样)
c
标记颜色
p
标记顶点坐标(每个面容器中最少 3 个)
</p>
标志着脸的尽头
fs
:忽略这个
显然,网格包含很多这样的面部容器,所以手动创建这个网格真的很浪费时间。 我已经编写了一个工作脚本,但这仅提取坐标并且不包含人脸。
现在我想实现这些面孔,所以我扩展了脚本,但它不起作用:
faces = [] #Array, where all the vertex Indecies from curFace are stored
verts = [] #Array, where all the vertex coordinates from coords are stored
vertIndex = 0
with open(filepath, 'r') as file:
for line in file:
if not line.startswith('<p>'):
continue #Skip lines which don't contain a <p>
if line.startswith('<p>'):
curFace = []
for container in line: #Do a loop while </p> is reached
if container.startswith('p'):
coords = container.strip('\np()/ ').split(',') #Remove certain characters and split line into 3 parts (XYZ)
coords = list(map(float, coords))
verts.append(coords) #Send the vertex coordinates from the current face to verts[]
curFace.append(vertIndex)
vertIndex += 1
elif container.startswith('</p>'):
faces.append(curFace) #Send the vertex Indecies from the current face to faces[]
break #Stop the face-for-loop and return to main line-for-loop
elif not container.startswith('p') or not container.startswith('</p>'):
continue #Skip lines in the container which don't start with 'p' or '</p>'
mesh.from_pydata(verts,[],faces) #Create the entire mesh with the arrays
P.S.:这只是主要部分的摘录。 控制台没有显示错误。有人可以帮我吗?
【问题讨论】:
【参考方案1】:如果您的示例数据与您的文件匹配,那么您的循环的基本逻辑就不存在了。
循环的逻辑取决于所有面部数据都在一行上。
with open(filepath, 'r') as file:
for line in file:
# the following is done for each line
if not line.startswith('<p>'):
# if the line doesn't start with <p> go to next line
continue
if line.startswith('<p>'):
# if line starts with <p> do the following with
# the rest of the line
curFace = []
for face in line: # here your are still on the line with <p>
if line.startswith('p'): # your still on the first line
# before this you need to get the next line from the file
coords = line.strip('\np()/ ').split(',')
【讨论】:
以上是关于从文本文件编码网格的主要内容,如果未能解决你的问题,请参考以下文章
将带有 CRLF 行终止符的 Little-endian UTF-16 Unicode 英文文本文件转换为 Ascii 编码