Thank you very much for your time, any help would be appreciated.
I have been using the code below to shrinkwrap an Icosphere onto my 3D models, then find the vertices coordinates of the Icosphere and export them into an excel file.
import bpy
thelist = open('The List.txt', 'r')
name = thelist.readline()
path = r"filepath"+name+".obj"
FILEPATH = str.join("", path.splitlines())
bpy.ops.import_scene.obj(filepath=FILEPATH)
for obj in bpy.context.scene.objects:
if obj.type == 'MESH':
obj.name = "Specimen"
bpy.data.objects["Specimen"].select = True
bpy.context.scene.objects.active = bpy.data.objects["Specimen"]
bpy.context.active_object.scale = (0.1, 0.1, 0.1)
bpy.ops.mesh.primitive_ico_sphere_add()
bpy.context.active_object.scale = (5, 5, 5)
bpy.ops.object.modifier_add(type='SHRINKWRAP')
bpy.data.objects["Icosphere"].modifiers["Shrinkwrap"].target = bpy.data.objects["Specimen"]
with open("Landmarks.csv", "a") as file:
file.write(name)
vs = bpy.data.meshes["Icosphere"].vertices
for v in vs:
file.write("%.2f, %.2f, %.2f," % (v.co[:]))
for o in bpy.context.scene.objects:
if o.type == 'MESH':
o.select = True
bpy.ops.object.delete()
name = thelist.readline()
path = r"filepath"+name+".obj"
FILEPATH = str.join("", path.splitlines())
bpy.ops.import_scene.obj(filepath=FILEPATH)
for obj in bpy.context.scene.objects:
if obj.type == 'MESH':
obj.name = "Specimen"
bpy.data.objects["Specimen"].select = True
bpy.context.scene.objects.active = bpy.data.objects["Specimen"]
bpy.context.active_object.scale = (0.1, 0.1, 0.1)
bpy.ops.mesh.primitive_ico_sphere_add()
bpy.context.active_object.scale = (5, 5, 5)
bpy.ops.object.modifier_add(type='SHRINKWRAP')
bpy.data.objects["Icosphere"].modifiers["Shrinkwrap"].target = bpy.data.objects["Specimen"]
with open("Landmarks.csv", "a") as file:
file.write(name)
vs = bpy.data.meshes["Icosphere"].vertices
for v in vs:
file.write("%.2f, %.2f, %.2f," % (v.co[:]))
I want to make this process automatic and go through all of my models without having to constantly click around. However, when I duplicated the code, I get a different model in blender but the same coordinates in my excel file from the first Icosphere. I already know that creating a new file clears out this memory, but it means that the programme can't be fully automatic. So how do I clear out those stored vertices without having to create a new file and stop the automatic process?
EDIT: added the duplicated code, as suggested in the comments. Once this issue has been sorted I will be turning the programme into a loop, however, going through the list.
print(context.object.name)after running the add ico operator . Similarly with its mesh. Recommend reference them, eg after ico opico = context.objectand useicoas object ref andico.datarather thanbpy.data.meshes["IcoSphere"]from then on. – batFINGER Sep 26 '21 at 13:29