简体   繁体   中英

Blender Python Script Deleting Meshes

I have a script that I am trying to run in blender that will go through all of the files in one directory and then open them one at a time and print to a file the number of vertices and faces to that file. The one problem that I am having is deleting a mesh from the scene before I move onto the next mesh. Here it is what I have so far

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()

This has to be done using ply files. If anyone can give me some tips or advice that would be great! Thanks!

bpy.ops.object.delete() will delete selected objects, including the mesh data for it.

You don't need bmesh for what you are trying to do, bmesh is only needed for editing mesh data. You can get a vertex count from the length of the vertices list in 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM