从文本文件编码网格
Encode Mesh from text-file
我想将具有此文本语法结构的 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.: 这只是主要部分的摘录。
控制台没有显示错误。有人可以帮我吗?
如果您的样本数据与您的文件匹配,那么您的循环的基本逻辑就失效了。
您的循环逻辑取决于所有面部数据都在一条线上。
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(',')
我想将具有此文本语法结构的 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.: 这只是主要部分的摘录。 控制台没有显示错误。有人可以帮我吗?
如果您的样本数据与您的文件匹配,那么您的循环的基本逻辑就失效了。
您的循环逻辑取决于所有面部数据都在一条线上。
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(',')