I have a dataset, called ModelNet, containing *.off-files
.
I need to read each file, decimate the mesh and store it to another file.
Problem: For some reason the modifier I add to the model has no effect on the mesh.
What I did: On Blender 2.79b
I have included an add-on to read off files. It works.
Then I have set the object I loaded from the *.off-file to active.
Note that I have added to break
keywords, since I tried to make the decimation work for one mesh, until I let it loop through every file.
I have tried the code of this post before. Unfortunately I was not able to get that code work for me.
Now, the output of number of vertices, edges and polygons does not change through decimation.
Question: What am I doing wrong? I would appreciate help!
Code:
numberOfIteration=2
decimateRatio=0.3
modifierName='DecimateMod'
# Cleans all decimate modifiers
def cleanAllDecimateModifiers(obj):
for m in obj.modifiers:
if(m.type=="DECIMATE"):
obj.modifiers.remove(modifier=m)
# remove the mesh cube (the intial one from startup)
if "Cube" in bpy.data.meshes:
mesh = bpy.data.meshes["Cube"]
print("removing mesh", mesh)
bpy.data.meshes.remove(mesh)
datasetPath = '~/ModelNet10_Mesh/raw'
folders = next(os.walk(datasetPath))[1]
print(folders)
for folder in folders:
files = next(os.walk(datasetPath + '/' + str(folder) + '/test/'))[2]
for file in files:
bpy.ops.import_mesh.off(filepath=datasetPath + '/' + str(folder) + '/test/' + str(file), axis_forward='-Z', axis_up='Y', filter_glob="*.off")
print(file.split('.')[0])
bpy.data.objects[file.split('.')[0]].select = True # works until blender 2.79
# bpy.data.objects[file.split('.')[0]].select_set(True) # works for blender 2.8+
bpy.context.scene.objects.active = bpy.data.objects[file.split('.')[0]]
for i in range(0, numberOfIteration):
objectList = bpy.data.objects
for obj in objectList:
if(obj.type == "MESH"):
print("before decimation")
print(obj.data.vertices)
print(obj.data.edges)
print(obj.data.polygons)
cleanAllDecimateModifiers(obj)
bpy.ops.object.modifier_add(type='DECIMATE')
bpy.context.object.modifiers["Decimate"].use_collapse_triangulate = True
bpy.context.object.modifiers["Decimate"].ratio = 1-decimateRatio*(i+1)
bpy.context.scene.update()
print("after decimation")
print(obj.data.vertices)
print(obj.data.edges)
print(obj.data.polygons)
break
break
Console output:
Camera
Cube
Lamp
['Scene']
removing mesh <bpy_struct, Mesh("Cube")>
['bathtub', 'toilet', 'desk', 'night_stand', 'bed', 'dresser', 'sofa', 'monitor', 'chair', 'table']
bathtub_0141
before decimation
<bpy_collection[74637], MeshVertices>
<bpy_collection[75181], MeshEdges>
<bpy_collection[25241], MeshPolygons>
after decimation
<bpy_collection[74637], MeshVertices>
<bpy_collection[75181], MeshEdges>
<bpy_collection[25241], MeshPolygons>
before decimation
<bpy_collection[74637], MeshVertices>
<bpy_collection[75181], MeshEdges>
<bpy_collection[25241], MeshPolygons>
after decimation
<bpy_collection[74637], MeshVertices>
<bpy_collection[75181], MeshEdges>
<bpy_collection[25241], MeshPolygons>
EDIT: According to @batFINGER I have now used the following code:
# remove the mesh cube (the intial one from startup)
if "Cube" in bpy.data.meshes:
mesh = bpy.data.meshes["Cube"]
print("removing mesh", mesh)
bpy.data.meshes.remove(mesh)
context = bpy.context
scene = context.scene
datasetPath = '~/datasets/ModelNet10_Mesh/raw'
folder = 'bed'
file = 'bed_0534.off'
bpy.ops.import_mesh.off(filepath=datasetPath + '/' + str(folder) + '/test/' + str(file), axis_forward='-Z', axis_up='Y', filter_glob="*.off")
mesh_obs = [o for o in scene.objects if o.type == "MESH"]
def delMods(ob, mod_type="ALL"):
mods = (ob.modifiers[:] if mod_type == "ALL"
else [m for m in ob.modifiers if m.type == mod_type])
while(mods):
ob.modifiers.remove(mods.pop())
for ob in mesh_obs:
delMods(ob, 'DECIMATE')
dm = ob.modifiers.new("Xxx", type="DECIMATE")
dm.use_collapse_triangulate = True
me = ob.data
me_mod = ob.to_mesh(scene, True, 'RENDER')
# print(len(me.vertices), len(me_mod.vertices))
print("me - vertices, polygons, edges")
print(me.vertices)
print(me.polygons)
print(me.edges)
print("me_mod - vertices, polygons, edges")
print(me_mod.vertices)
print(me_mod.polygons)
print(me_mod.edges)
which results in the following console output:
removing mesh <bpy_struct, Mesh("Cube")>
me - vertices, polygons, edges
<bpy_collection[54904], MeshVertices>
<bpy_collection[30636], MeshPolygons>
<bpy_collection[73232], MeshEdges>
me_mod - vertices, polygons, edges
<bpy_collection[54904], MeshVertices>
<bpy_collection[30636], MeshPolygons>
<bpy_collection[73232], MeshEdges>
scene structure: