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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
# ##### BEGIN GPL LICENSE BLOCK #####
#
# 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 2
# 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, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
if "bpy" in locals():
import imp
imp.reload(utils)
else:
from blenderkit import utils
import bpy, bmesh
import mathutils
import object_print3d_utils
from object_print3d_utils import operators as ops
from mathutils import Vector
RENDER_OBTYPES = ['MESH', 'CURVE', 'SURFACE', 'METABALL', 'TEXT']
def check_material(props, mat):
e = bpy.context.scene.render.engine
shaders = []
if e == 'CYCLES':
if mat.node_tree is not None:
checknodes = mat.node_tree.nodes[:]
while len(checknodes) > 0:
n = checknodes.pop()
if n.type == 'GROUP': # dive deeper here.
checknodes.extend(n.node_tree.nodes)
if len(n.outputs) == 1 and n.outputs[0].type == 'SHADER' and n.type != 'GROUP':
if n.type not in shaders:
shaders.append(n.type)
if n.type == 'TEX_IMAGE':
mattype = 'image based'
if n.image is not None:
maxres = max(n.image.size[0], n.image.size[1])
props.texture_resolution_max = max(props.texture_resolution_max, maxres)
minres = min(n.image.size[0], n.image.size[1])
if props.texture_resolution_min == 0:
props.texture_resolution_min = minres
else:
props.texture_resolution_min = min(props.texture_resolution_min, minres)
props.shaders = ''
for s in shaders:
if s.startswith('BSDF_'):
s = s[5:]
s = s.lower().replace('_', ' ')
props.shaders += (s + ', ')
def check_render_engine(props, obs):
ob = obs[0]
m = None
e = bpy.context.scene.render.engine
mattype = None
materials = []
shaders = []
props.uv = False
for ob in obs: # TODO , this is duplicated here for other engines, otherwise this should be more clever.
for ms in ob.material_slots:
if ms.material is not None:
m = ms.material
if m.name not in materials:
materials.append(m.name)
if ob.type == 'MESH' and len(ob.data.uv_layers) > 0:
props.uv = True
if e == 'BLENDER_RENDER':
props.engine = 'BLENDER_INTERNAL'
elif e == 'CYCLES':
props.engine = 'CYCLES'
for mname in materials:
m = bpy.data.materials[mname]
if m is not None and m.node_tree is not None:
checknodes = m.node_tree.nodes[:]
while len(checknodes) > 0:
n = checknodes.pop()
if n.type == 'GROUP': # dive deeper here.
checknodes.extend(n.node_tree.nodes)
if len(n.outputs) == 1 and n.outputs[0].type == 'SHADER' and n.type != 'GROUP':
if n.type not in shaders:
shaders.append(n.type)
if n.type == 'TEX_IMAGE':
mattype = 'image based'
if n.image is not None:
maxres = max(n.image.size[0], n.image.size[1])
props.texture_resolution_max = max(props.texture_resolution_max, maxres)
minres = min(n.image.size[0], n.image.size[1])
if props.texture_resolution_min == 0:
props.texture_resolution_min = minres
else:
props.texture_resolution_min = min(props.texture_resolution_min, minres)
# if mattype == None:
# mattype = 'procedural'
# tags['material type'] = mattype
elif e == 'BLENDER_GAME':
props.engine = 'BLENDER_GAME'
# write to object properties.
props.materials = ''
props.shaders = ''
for m in materials:
props.materials += (m + ', ')
for s in shaders:
if s.startswith('BSDF_'):
s = s[5:]
s = s.lower()
s = s.replace('_', ' ')
props.shaders += (s + ', ')
def check_printable(props, obs):
if len(obs) == 1:
check_cls = (
ops.Print3DCheckSolid,
ops.Print3DCheckIntersections,
ops.Print3DCheckDegenerate,
ops.Print3DCheckDistorted,
ops.Print3DCheckThick,
ops.Print3DCheckSharp,
# ops.Print3DCheckOverhang,
)
ob = obs[0]
info = []
for cls in check_cls:
cls.main_check(ob, info)
printable = True
for item in info:
passed = item[0].endswith(' 0')
if not passed:
print(item[0])
printable = False
props.printable_3d = printable
def check_rig(props, obs):
for ob in obs:
if ob.type == 'ARMATURE':
props.rig = True
def check_anim(props, obs):
animated = False
for ob in obs:
if ob.animation_data is not None:
a = ob.animation_data.action
if a is not None:
for c in a.fcurves:
if len(c.keyframe_points) > 1:
animated = True
# c.keyframe_points.remove(c.keyframe_points[0])
if animated:
props.animated = True
def check_meshprops(props, obs):
''' checks polycount, manifold, mesh parts (not implemented)'''
fc = 0
fcr = 0
tris = 0
quads = 0
ngons = 0
vc = 0
edges_counts = {}
manifold = True
for ob in obs:
if ob.type == 'MESH' or ob.type == 'CURVE':
ob_eval = None
if ob.type == 'CURVE':
# depsgraph = bpy.context.evaluated_depsgraph_get()
# object_eval = ob.evaluated_get(depsgraph)
mesh = ob.to_mesh()
else:
mesh = ob.data
fco = len(mesh.polygons)
fc += fco
vc += len(mesh.vertices)
fcor = fco
for f in mesh.polygons:
# face sides counter
if len(f.vertices) == 3:
tris += 1
elif len(f.vertices) == 4:
quads += 1
elif len(f.vertices) > 4:
ngons += 1
# manifold counter
for i, v in enumerate(f.vertices):
v1 = f.vertices[i - 1]
e = (min(v, v1), max(v, v1))
edges_counts[e] = edges_counts.get(e, 0) + 1
# all meshes have to be manifold for this to work.
manifold = manifold and not any(i in edges_counts.values() for i in [0, 1, 3, 4])
for m in ob.modifiers:
if m.type == 'SUBSURF' or m.type == 'MULTIRES':
fcor *= 4 ** m.render_levels
if m.type == 'SOLIDIFY': # this is rough estimate, not to waste time with evaluating all nonmanifold edges
fcor *= 2
if m.type == 'ARRAY':
fcor *= m.count
if m.type == 'MIRROR':
fcor *= 2
if m.type == 'DECIMATE':
fcor *= m.ratio
fcr += fcor
if ob_eval:
ob_eval.to_mesh_clear()
# write out props
props.face_count = fc
props.face_count_render = fcr
# print(tris, quads, ngons)
if quads > 0 and tris == 0 and ngons == 0:
props.mesh_poly_type = 'QUAD'
elif quads > tris and quads > ngons:
props.mesh_poly_type = 'QUAD_DOMINANT'
elif tris > quads and tris > quads:
props.mesh_poly_type = 'TRI_DOMINANT'
elif quads == 0 and tris > 0 and ngons == 0:
props.mesh_poly_type = 'TRI'
elif ngons > quads and ngons > tris:
props.mesh_poly_type = 'NGON'
else:
props.mesh_poly_type = 'OTHER'
props.manifold = manifold
def countObs(props, obs):
ob_types = {}
count = len(obs)
for ob in obs:
otype = ob.type.lower()
ob_types[otype] = ob_types.get(otype, 0) + 1
props.object_count = count
def check_modifiers(props, obs):
# modif_mapping = {
# }
modifiers = []
for ob in obs:
for m in ob.modifiers:
mtype = m.type
mtype = mtype.replace('_', ' ')
mtype = mtype.lower()
# mtype = mtype.capitalize()
if mtype not in modifiers:
modifiers.append(mtype)
if m.type == 'SMOKE':
if m.smoke_type == 'FLOW':
smt = m.flow_settings.smoke_flow_type
if smt == 'BOTH' or smt == 'FIRE':
modifiers.append('fire')
# for mt in modifiers:
effectmodifiers = ['soft body', 'fluid simulation', 'particle system', 'collision', 'smoke', 'cloth',
'dynamic paint']
for m in modifiers:
if m in effectmodifiers:
props.simulation = True
if ob.rigid_body is not None:
props.simulation = True
modifiers.append('rigid body')
finalstr = ''
for m in modifiers:
finalstr += m
finalstr += ','
props.modifiers = finalstr
def get_autotags():
""" call all analysis functions """
ui = bpy.context.scene.blenderkitUI
if ui.asset_type =='MODEL':
ob = utils.get_active_model()
obs = utils.get_hierarchy(ob)
props = ob.blenderkit
if props.name == "":
props.name = ob.name
# reset some properties here, because they might not get re-filled at all when they aren't needed anymore.
props.texture_resolution_max = 0
props.texture_resolution_min = 0
# disabled printing checking, some 3d print addon bug.
# check_printable( props, obs)
check_render_engine(props, obs)
dim, bbox_min, bbox_max = utils.get_dimensions(obs)
props.dimensions = dim
props.bbox_min = bbox_min
props.bbox_max = bbox_max
check_rig(props, obs)
check_anim(props, obs)
check_meshprops(props, obs)
check_modifiers(props, obs)
countObs(props, obs)
elif ui.asset_type =='MATERIAL':
# reset some properties here, because they might not get re-filled at all when they aren't needed anymore.
mat = utils.get_active_asset()
props = mat.blenderkit
props.texture_resolution_max = 0
props.texture_resolution_min = 0
check_material(props, mat)
class AutoFillTags(bpy.types.Operator):
"""Fill tags for asset. Now run before upload, no need to interact from user side."""
bl_idname = "object.blenderkit_auto_tags"
bl_label = "Generate Auto Tags for BlenderKit"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
get_autotags()
return {'FINISHED'}
def register_asset_inspector():
bpy.utils.register_class(AutoFillTags)
def unregister_asset_inspector():
bpy.utils.unregister_class(AutoFillTags)
if __name__ == "__main__":
register()
|