-
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathobject_list.py
More file actions
159 lines (128 loc) · 4.89 KB
/
Copy pathobject_list.py
File metadata and controls
159 lines (128 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# ##### BEGIN GPL LICENSE BLOCK #####
#
# ObjectList, a Blender add-on
# (c) 2021,2025 Michel Anders (varkenvarken)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# ##### END GPL LICENSE BLOCK #####
bl_info = {
"name": "ObjectList",
"author": "Michel Anders (varkenvarken)",
"version": (0, 0, 20250924144433),
"blender": (4, 4, 0),
"location": "View3D > Object > Object List",
"description": "create a comma separated list with object mesh info",
"warning": "",
"wiki_url": "",
"tracker_url": "",
"category": "Object",
}
from csv import DictWriter
import bpy
import bmesh
def object_list(context):
bm = bmesh.new()
# get all objects in a scene and remember selected/active status
objects = context.scene.objects[:]
selection_status = {ob: ob.select_get() for ob in objects}
active_object = context.active_object
bpy.ops.object.select_all(action="DESELECT")
# try to convert each object to a mesh while keeping the orginal
for ob in objects:
ob.select_set(True)
bpy.ops.object.convert(target="MESH", keep_original=True)
# if we have an extra object, the conversion was successful
# (Note even a mesh will be converted to a mesh, which is a bit wasteful)
for ob2 in context.scene.objects:
if ob2 not in objects:
# yield a dictionary of mesh data
tris, faces, edges, verts = 0, 0, 0, 0
bm.from_mesh(ob2.data)
tris = len(bm.calc_loop_triangles())
verts = len(bm.verts)
edges = len(bm.edges)
faces = len(bm.faces)
# note that names etc come from the original object
# and we make sure to copy string to prevent dangling references that might crash Blender
info = {
"Name": ob.name[:],
"Type": ob.type[:],
"Tris": tris,
"Faces": faces,
"Edges": edges,
"Verts": verts,
"Datablock name": ob.data.name[:] if ob.data else None,
"Users": ob.data.users,
}
for i,c in enumerate(ob.users_collection, start=1):
info[f"Collection {i}"] = c.name[:]
yield info
# clear the bmesh, it will be reused for the next object
bm.clear()
# remove the duplicated mesh object and its data block
data = ob2.data
bpy.data.objects.remove(ob2)
bpy.data.meshes.remove(data)
# restore the selection and active status
for ob in objects:
ob.select_set(selection_status[ob])
bpy.context.view_layer.objects.active = active_object
# and remove the bmesh object
bm.free()
class ObjectList(bpy.types.Operator):
bl_idname = "object.object_list"
bl_label = "ObjectList"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
# get a list of dicts that describe each object that is convertible to a mesh
oblist = list(object_list(bpy.context))
# add a new text object and a dictwriter that references it
# (a text object has a write() method so can be uses by the dictwriter directly
t = bpy.data.texts.new(name="Object list.csv")
dictwriter = DictWriter(
t,
fieldnames=[
"Name",
"Type",
"Tris",
"Faces",
"Edges",
"Verts",
"Datablock name",
"Users",
"Collection 1",
"Collection 2",
"Collection 3",
],
)
# write out the data
dictwriter.writeheader()
dictwriter.writerows(oblist)
return {"FINISHED"}
def menu_func(self, context):
self.layout.operator(
ObjectList.bl_idname,
text="Create a comma separated list with object mesh info",
icon="INFO",
)
classes = (ObjectList,)
def register():
for c in classes:
bpy.utils.register_class(c)
bpy.types.VIEW3D_MT_object.append(menu_func)
def unregister():
bpy.types.VIEW3D_MT_object.remove(menu_func)
for c in classes:
bpy.utils.unregister_class(c)