0

我有一个脚本,我试图在搅拌机中运行,它将遍历一个目录中的所有文件,然后一次打开一个文件,并将该文件的顶点数和面数打印到文件中。我遇到的一个问题是在移动到下一个网格之前从场景中删除一个网格。这是我到目前为止所拥有的

import bpy


# print all objects
for obj in bpy.data.objects:
    print(obj.name)


# print all scene names in a list
print(bpy.data.scenes.keys())


# remove mesh Cube
if "Cube" in bpy.data.meshes:
    mesh = bpy.data.meshes["Cube"]
    print("removing mesh", mesh)
    bpy.data.meshes.remove(mesh)


# write images into a file next to the blend
import os
import bmesh

file = open("C:\\Users\\Jon\\Documents\\Dataset\\ReadMe.txt", 'a')
file.write("Name \t Vertices \t Faces \t QuadOrTriangle \t \n")

#os.chdir("C:\\Users\\Jon\\Documents\\Dataset\\SingleObjects\\")

path = "C:\\Users\\Jon\\Documents\\Dataset\\SingleObjects\\"

for data in os.listdir(path):

    bpy.ops.import_mesh.ply(filepath="C:\\Users\\Jon\\Documents\\Dataset\\SingleObjects\\" + data)

    me = bpy.context.object.data
    bm = bmesh.new()   # create an empty BMesh
    bm.from_mesh(me)   # fill it in from a Mesh

    #filepath = bpy.data.filepath
    for t in bpy.data.meshes:
         directory = bpy.path.abspath(t.name)

    FaceCounter = 0
    VertCounter = 0
    #QuadOrTri = 0

    for f in bm.faces:
        QuadOrTri = 0
        for v in f.verts:
            QuadOrTri = QuadOrTri + 1

    for f in bm.faces:
        FaceCounter = FaceCounter +1

    for v in bm.verts:
        VertCounter = VertCounter + 1

    file.write("%s.ply \t %d \t %d \t %d \n" % (directory, VertCounter, FaceCounter, QuadOrTri))

    for item in bpy.data.meshes:
        bpy.data.meshes.remove(item)

    file.close()

这必须使用 ply 文件来完成。如果有人能给我一些提示或建议,那就太好了!谢谢!

4

1 回答 1

2

bpy.ops.object.delete()将删除选定的对象,包括它的网格数据。

你不需要 bmesh 来做你想做的事情,只需要 bmesh 来编辑网格数据。您可以从顶点列表的长度中获取顶点数object.data

VertCount = len(bpy.context.active_object.data.vertices)
EdgeCount = len(bpy.context.active_object.data.edges)
FaceCount = len(bpy.context.active_object.data.polygons)
于 2015-06-14T08:58:58.847 回答