1 # SPDX-FileCopyrightText: 2012 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
9 def _redraw_yasiamevil():
12 def _redraw_yasiamevil():
13 _redraw_yasiamevil
.opr(**_redraw_yasiamevil
.arg
)
14 _redraw_yasiamevil
.opr
= bpy
.ops
.wm
.redraw_timer
15 _redraw_yasiamevil
.arg
= dict(type='DRAW_WIN_SWAP', iterations
=1)
18 def _points_from_object(depsgraph
, scene
, obj
, source
):
21 'PARTICLE_OWN', 'PARTICLE_CHILD',
23 'VERT_OWN', 'VERT_CHILD',
26 # print(source - _source_all)
28 assert(len(source | _source_all
) == len(_source_all
))
33 def edge_center(mesh
, edge
):
34 v1
, v2
= edge
.vertices
35 return (mesh
.vertices
[v1
].co
+ mesh
.vertices
[v2
].co
) / 2.0
37 def poly_center(mesh
, poly
):
38 from mathutils
import Vector
41 for i
in poly
.loop_indices
:
42 co
+= mesh
.vertices
[mesh
.loops
[i
].vertex_index
].co
46 def points_from_verts(obj
):
47 """Takes points from _any_ object with geometry"""
48 if obj
.type == 'MESH':
50 matrix
= obj
.matrix_world
.copy()
51 points
.extend([matrix
@ v
.co
for v
in mesh
.vertices
])
53 ob_eval
= ob
.evaluated_get(depsgraph
)
55 mesh
= ob_eval
.to_mesh()
60 matrix
= obj
.matrix_world
.copy()
61 points
.extend([matrix
@ v
.co
for v
in mesh
.vertices
])
62 ob_eval
.to_mesh_clear()
64 def points_from_particles(obj
):
65 obj_eval
= obj
.evaluated_get(depsgraph
)
66 points
.extend([p
.location
.copy()
67 for psys
in obj_eval
.particle_systems
68 for p
in psys
.particles
])
71 if 'VERT_OWN' in source
:
72 points_from_verts(obj
)
75 if 'VERT_CHILD' in source
:
76 for obj_child
in obj
.children
:
77 points_from_verts(obj_child
)
80 if 'PARTICLE_OWN' in source
:
81 points_from_particles(obj
)
83 if 'PARTICLE_CHILD' in source
:
84 for obj_child
in obj
.children
:
85 points_from_particles(obj_child
)
88 def get_points(stroke
):
89 return [point
.co
.copy() for point
in stroke
.points
]
93 frame
= gp
.layers
.active
.active_frame
94 return [get_points(stroke
) for stroke
in frame
.strokes
]
98 if 'PENCIL' in source
:
99 # Used to be from object in 2.7x, now from scene.
100 gp
= scene
.grease_pencil
102 points
.extend([p
for spline
in get_splines(gp
) for p
in spline
])
104 print("Found %d points" % len(points
))
109 def cell_fracture_objects(
110 context
, collection
, obj
,
111 source
={'PARTICLE_OWN'},
116 use_smooth_faces
=False,
117 use_data_match
=False,
118 use_debug_points
=False,
121 use_debug_redraw
=False,
122 cell_scale
=(1.0, 1.0, 1.0),
124 from . import fracture_cell_calc
125 depsgraph
= context
.evaluated_depsgraph_get()
126 scene
= context
.scene
127 view_layer
= context
.view_layer
129 # -------------------------------------------------------------------------
132 points
= _points_from_object(depsgraph
, scene
, obj
, source
)
135 # print using fallback
136 points
= _points_from_object(depsgraph
, scene
, obj
, {'VERT_OWN'})
139 print("no points found")
142 # apply optional clamp
143 if source_limit
!= 0 and source_limit
< len(points
):
145 random
.shuffle(points
)
146 points
[source_limit
:] = []
148 # sadly we can't be sure there are no doubles
149 from mathutils
import Vector
150 to_tuple
= Vector
.to_tuple
151 points
= list({to_tuple(p
, 4): p
for p
in points
}.values())
158 if source_noise
> 0.0:
159 from random
import random
160 # boundbox approx of overall scale
161 from mathutils
import Vector
162 matrix
= obj
.matrix_world
.copy()
163 bb_world
= [matrix
@ Vector(v
) for v
in obj
.bound_box
]
164 scalar
= source_noise
* ((bb_world
[0] - bb_world
[6]).length
/ 2.0)
166 from mathutils
.noise
import random_unit_vector
168 points
[:] = [p
+ (random_unit_vector() * (scalar
* random())) for p
in points
]
174 mesh_tmp
= bpy
.data
.meshes
.new(name
="DebugPoints")
177 obj_tmp
= bpy
.data
.objects
.new(name
=mesh_tmp
.name
, object_data
=mesh_tmp
)
178 collection
.objects
.link(obj_tmp
)
179 del obj_tmp
, mesh_tmp
182 matrix
= obj
.matrix_world
.copy()
183 verts
= [matrix
@ v
.co
for v
in mesh
.vertices
]
185 cells
= fracture_cell_calc
.points_as_bmesh_cells(
193 cell_name
= obj
.name
+ "_cell"
197 for center_point
, cell_points
in cells
:
199 # ---------------------------------------------------------------------
202 # create the convex hulls
205 # WORKAROUND FOR CONVEX HULL BUG/LIMIT
210 return (random
.random() - 0.5) * 0.001
213 for i
, co
in enumerate(cell_points
):
221 bm_vert
= bm
.verts
.new(co
)
224 bmesh
.ops
.remove_doubles(bm
, verts
=bm
.verts
, dist
=0.005)
226 bmesh
.ops
.convex_hull(bm
, input=bm
.verts
)
229 traceback
.print_exc()
234 bmesh
.ops
.dissolve_limit(bm
, verts
=bm
.verts
, angle_limit
=0.001)
237 traceback
.print_exc()
238 # Smooth faces will remain only inner faces, after applying boolean modifier.
240 for bm_face
in bm
.faces
:
241 bm_face
.smooth
= True
243 if material_index
!= 0:
244 for bm_face
in bm
.faces
:
245 bm_face
.material_index
= material_index
247 # ---------------------------------------------------------------------
249 mesh_dst
= bpy
.data
.meshes
.new(name
=cell_name
)
256 # match materials and data layers so boolean displays them
257 # currently only materials + data layers, could do others...
259 for mat
in mesh_src
.materials
:
260 mesh_dst
.materials
.append(mat
)
261 for lay_attr
in ("vertex_colors", "uv_layers"):
262 lay_src
= getattr(mesh_src
, lay_attr
)
263 lay_dst
= getattr(mesh_dst
, lay_attr
)
264 for key
in lay_src
.keys():
265 lay_dst
.new(name
=key
)
267 # ---------------------------------------------------------------------
270 obj_cell
= bpy
.data
.objects
.new(name
=cell_name
, object_data
=mesh_dst
)
271 collection
.objects
.link(obj_cell
)
272 # scene.objects.active = obj_cell
273 obj_cell
.location
= center_point
275 objects
.append(obj_cell
)
277 # support for object materials
279 for i
in range(len(mesh_dst
.materials
)):
280 slot_src
= obj
.material_slots
[i
]
281 slot_dst
= obj_cell
.material_slots
[i
]
283 slot_dst
.link
= slot_src
.link
284 slot_dst
.material
= slot_src
.material
295 def cell_fracture_boolean(
296 context
, collection
, obj
, objects
,
297 use_debug_bool
=False,
299 use_island_split
=False,
300 use_interior_hide
=False,
301 use_debug_redraw
=False,
307 scene
= context
.scene
308 view_layer
= context
.view_layer
310 if use_interior_hide
and level
== 0:
311 # only set for level 0
312 obj
.data
.polygons
.foreach_set("hide", [False] * len(obj
.data
.polygons
))
314 for obj_cell
in objects
:
315 mod
= obj_cell
.modifiers
.new(name
="Boolean", type='BOOLEAN')
317 mod
.operation
= 'INTERSECT'
319 if not use_debug_bool
:
321 if use_interior_hide
:
322 obj_cell
.data
.polygons
.foreach_set("hide", [True] * len(obj_cell
.data
.polygons
))
324 # Calculates all booleans at once (faster).
325 depsgraph
= context
.evaluated_depsgraph_get()
327 for obj_cell
in objects
:
329 if not use_debug_bool
:
331 obj_cell_eval
= obj_cell
.evaluated_get(depsgraph
)
332 mesh_new
= bpy
.data
.meshes
.new_from_object(obj_cell_eval
)
333 mesh_old
= obj_cell
.data
334 obj_cell
.data
= mesh_new
335 obj_cell
.modifiers
.remove(obj_cell
.modifiers
[-1])
337 # remove if not valid
338 if not mesh_old
.users
:
339 bpy
.data
.meshes
.remove(mesh_old
)
340 if not mesh_new
.vertices
:
341 collection
.objects
.unlink(obj_cell
)
342 if not obj_cell
.users
:
343 bpy
.data
.objects
.remove(obj_cell
)
345 if not mesh_new
.users
:
346 bpy
.data
.meshes
.remove(mesh_new
)
349 # avoid unneeded bmesh re-conversion
350 if mesh_new
is not None:
354 if bm
is None: # ok this will always be true for now...
356 bm
.from_mesh(mesh_new
)
359 bmesh
.ops
.dissolve_limit(bm
, verts
=bm
.verts
, edges
=bm
.edges
, angle_limit
=0.001)
362 traceback
.print_exc()
367 bm
.from_mesh(mesh_new
)
368 bmesh
.ops
.remove_doubles(bm
, verts
=bm
.verts
, dist
=0.005)
377 if obj_cell
is not None:
378 objects_boolean
.append(obj_cell
)
383 if (not use_debug_bool
) and use_island_split
:
384 # this is ugly and Im not proud of this - campbell
385 for ob
in view_layer
.objects
:
387 for obj_cell
in objects_boolean
:
388 obj_cell
.select_set(True)
390 bpy
.ops
.mesh
.separate(type='LOOSE')
392 objects_boolean
[:] = [obj_cell
for obj_cell
in view_layer
.objects
if obj_cell
.select_get()]
394 context
.view_layer
.update()
396 return objects_boolean
399 def cell_fracture_interior_handle(
401 use_interior_vgroup
=False,
402 use_sharp_edges
=False,
403 use_sharp_edges_apply
=False,
405 """Run after doing _all_ booleans"""
407 assert(use_interior_vgroup
or use_sharp_edges
or use_sharp_edges_apply
)
409 for obj_cell
in objects
:
414 if use_interior_vgroup
:
415 for bm_vert
in bm
.verts
:
417 for bm_face
in bm
.faces
:
419 for bm_vert
in bm_face
.verts
:
422 # now add all vgroups
423 defvert_lay
= bm
.verts
.layers
.deform
.verify()
424 for bm_vert
in bm
.verts
:
426 bm_vert
[defvert_lay
][0] = 1.0
429 obj_cell
.vertex_groups
.new(name
="Interior")
432 for bm_edge
in bm
.edges
:
433 if len({bm_face
.hide
for bm_face
in bm_edge
.link_faces
}) == 2:
434 bm_edge
.smooth
= False
436 if use_sharp_edges_apply
:
437 edges
= [edge
for edge
in bm
.edges
if edge
.smooth
is False]
440 bmesh
.ops
.split_edges(bm
, edges
=edges
)
442 for bm_face
in bm
.faces
: