Changeset View
Changeset View
Standalone View
Standalone View
object_fracture_crack/crack_it.py
| Context not available. | |||||
| gauss, | gauss, | ||||
| seed, | seed, | ||||
| ) | ) | ||||
| from math import radians | from math import radians, pi | ||||
| from mathutils import Euler | from mathutils import Euler | ||||
| Context not available. | |||||
| return mat_ui_name | return mat_ui_name | ||||
| # error_type='ERROR' for popup massage | |||||
| def error_handlers(self, op_name, error, reports="ERROR", func=False): | def error_handlers(self, op_name, error, reports="ERROR", func=False, error_type='WARNING'): | ||||
| if self and reports: | if self and reports: | ||||
| self.report({'WARNING'}, reports + " (See Console for more info)") | self.report({error_type}, reports + " (See Console for more info)") | ||||
| is_func = "Function" if func else "Operator" | is_func = "Function" if func else "Operator" | ||||
| print("\n[Cell Fracture Crack It]\n{}: {}\nError: " | print("\n[Cell Fracture Crack It]\n{}: {}\nError: " | ||||
| "{}\nReport: {}\n".format(is_func, op_name, error, reports)) | "{}\nReport: {}\n".format(is_func, op_name, error, reports)) | ||||
| # -------------------- Crack ------------------- | # -------------------- Crack ------------------- | ||||
| # Cell fracture and post-process: | # Cell fracture and post-process: | ||||
| def makeFracture(child_verts=False, division=100, noise=0.00, | # Setting before crack from grease pencil | ||||
| scaleX=1.00, scaleY=1.00, scaleZ=1.00, recursion=0, margin=0.001): | def grease_pencil_setting(): | ||||
| obj = bpy.context.object | |||||
| # Get active object name and active layer | sce = bpy.context.scene | ||||
| active_name = bpy.context.scene.objects.active.name | sce.tool_settings.grease_pencil_source = 'OBJECT' | ||||
| active_layer = bpy.context.scene.active_layer | if sce.grease_pencil and not obj.grease_pencil: | ||||
| obj.grease_pencil = sce.grease_pencil | |||||
| # source method of whether use child verts | if obj.grease_pencil: | ||||
| if child_verts is True: | obj.grease_pencil.layers[0].parent = obj | ||||
| crack_source = 'VERT_CHILD' | |||||
| else: | else: | ||||
| crack_source = 'PARTICLE_OWN' | print("Detecting Grease Pencil was failed. " | ||||
| "Set pencil source to 'OBJECT' from 'SCENE' and set parent.") | |||||
| bpy.ops.object.add_fracture_cell_objects( | |||||
| source={crack_source}, source_limit=division, source_noise=noise, | |||||
| cell_scale=(scaleX, scaleY, scaleZ), recursion=recursion, | |||||
| recursion_source_limit=8, recursion_clamp=250, recursion_chance=0.25, | |||||
| recursion_chance_select='SIZE_MIN', use_smooth_faces=False, | |||||
| use_sharp_edges=False, use_sharp_edges_apply=True, use_data_match=True, | |||||
| use_island_split=True, margin=margin, material_index=0, | |||||
| use_interior_vgroup=False, mass_mode='VOLUME', mass=1, use_recenter=True, | |||||
| use_remove_original=True, use_layer_index=0, use_layer_next=False, | |||||
| group_name="", use_debug_points=False, use_debug_redraw=True, use_debug_bool=False | |||||
| ) | |||||
| _makeJoin(active_name, active_layer) | def simplifyOriginal(simplify): | ||||
| bpy.ops.object.modifier_add(type='DECIMATE') | |||||
| decimate = bpy.context.object.modifiers[-1] | |||||
| decimate.name = 'DECIMATE_crackit_original' | |||||
| decimate.ratio = 1-simplify | |||||
| def desimplifyOriginal(original_obj, cracked_obj): | |||||
| bpy.context.scene.objects.active = original_obj | |||||
| if 'DECIMATE_crackit_original' in bpy.context.object.modifiers.keys(): | |||||
| bpy.ops.object.modifier_remove(modifier='DECIMATE_crackit_original') | |||||
| bpy.context.scene.objects.active = cracked_obj | |||||
| # Join fractures into an object | # Join fractures into an object | ||||
| def _makeJoin(active_name, active_layer): | def makeJoin(self, original_loc): | ||||
| # Get active object name and active layer | |||||
| active_name = bpy.context.scene.objects.active.name | |||||
| active_layer = bpy.context.scene.active_layer | |||||
| # Get object by name | # Get object by name | ||||
| bpy.ops.object.select_all(action='DESELECT') | bpy.ops.object.select_all(action='DESELECT') | ||||
| bpy.ops.object.select_pattern(pattern=active_name + '_cell*') | bpy.ops.object.select_pattern(pattern=active_name + '_cell*') | ||||
| Context not available. | |||||
| bpy.ops.object.join() | bpy.ops.object.join() | ||||
| else: | else: | ||||
| error_handlers( | error_handlers( | ||||
| False, "_makeJoin", "if fractures condition has not passed", | self, "_makeJoin", "if fractures condition has not passed", | ||||
| "Warning: No objects could be joined", True | "Warning: No objects could be joined.\nIt can happen with too big margin: 0.20-1.00", | ||||
| True, error_type='ERROR' | |||||
| ) | ) | ||||
| # Duplicate ojbect | |||||
| bpy.ops.object.select_pattern(pattern=active_name) | |||||
| bpy.ops.object.duplicate_move(TRANSFORM_OT_translate={"value":(2, 0, 0)}) | |||||
| # Change name | # Change name | ||||
| bpy.context.scene.objects.active.name = active_name + '_crack' | bpy.context.scene.objects.active.name = active_name + '_crack' | ||||
| # Change origin | # Change origin | ||||
| bpy.ops.object.origin_set(type='GEOMETRY_ORIGIN') | bpy.ops.object.origin_set(type='GEOMETRY_ORIGIN') | ||||
| # Change location to avoid overlap of two objects | |||||
| new_location = bpy.context.object.location.copy() | |||||
| new_location[0] += original_loc | |||||
| bpy.context.scene.objects.active.location = new_location | |||||
| return bpy.context.scene.objects.active | |||||
| # Add modifier and setting | # Add modifier and setting | ||||
| def addModifiers(): | def addModifiers(decimate_val=0.4, smooth_val=0.5): | ||||
| bpy.ops.object.modifier_add(type='DECIMATE') | bpy.ops.object.modifier_add(type='DECIMATE') | ||||
| decimate = bpy.context.object.modifiers[-1] | decimate = bpy.context.object.modifiers[-1] | ||||
| decimate.name = 'DECIMATE_crackit' | decimate.name = 'DECIMATE_crackit' | ||||
| decimate.ratio = 0.4 | #decimate.decimate_type = 'DISSOLVE' | ||||
| decimate.ratio = decimate_val | |||||
| #decimate.angle_limit = decimate_val*pi | |||||
| bpy.ops.object.modifier_add(type='SUBSURF') | bpy.ops.object.modifier_add(type='SUBSURF') | ||||
| subsurf = bpy.context.object.modifiers[-1] | subsurf = bpy.context.object.modifiers[-1] | ||||
| Context not available. | |||||
| bpy.ops.object.modifier_add(type='SMOOTH') | bpy.ops.object.modifier_add(type='SMOOTH') | ||||
| smooth = bpy.context.object.modifiers[-1] | smooth = bpy.context.object.modifiers[-1] | ||||
| smooth.name = 'SMOOTH_crackit' | smooth.name = 'SMOOTH_crackit' | ||||
| smooth.factor = smooth_val | |||||
| # -------------- multi extrude -------------------- | # -------------- multi extrude -------------------- | ||||
| # var1=random offset, var2=random rotation, var3=random scale | # var1=random offset, var2=random rotation, var3=random scale | ||||
| def multiExtrude(off=0.1, rotx=0, roty=0, rotz=0, sca=1.0, | def multiExtrude(off=0.1, rotx=0, roty=0, rotz=0, sca=0.0, | ||||
| var1=0.01, var2=0.3, var3=0.3, num=1, ran=0): | var1=0.01, var2=0.01, var3=0.01, num=1, ran=0): | ||||
| obj = bpy.context.object | obj = bpy.context.object | ||||
| bpy.context.tool_settings.mesh_select_mode = [False, False, True] | bpy.context.tool_settings.mesh_select_mode = [False, False, True] | ||||
| Context not available. | |||||
| # -------------- Material preset -------------------------- | # -------------- Material preset -------------------------- | ||||
| def appendMaterial(addon_path, material_name, mat_ui_names="Nameless Material"): | def appendMaterial(crackit, addon_path, material_name, mat_ui_names="Nameless Material"): | ||||
| # Load material from the addon directory | # Load material from the addon directory | ||||
| file_path = _makeFilePath(addon_path=addon_path) | file_path = _makeFilePath(addon_path=addon_path) | ||||
| bpy.ops.wm.append(filename=material_name, directory=file_path) | bpy.ops.wm.append(filename=material_name, directory=file_path) | ||||
| Context not available. | |||||
| if last_material: | if last_material: | ||||
| mat = bpy.data.materials[last_material] | mat = bpy.data.materials[last_material] | ||||
| # skip renaming if the prop is True | # skip renaming if the prop is True | ||||
| if not bpy.context.scene.crackit.material_lib_name: | if not crackit.material_lib_name: | ||||
| mat.name = mat_ui_names | mat.name = mat_ui_names | ||||
| # Apply Only one material in the material slot | # Apply Only one material in the material slot | ||||
| Context not available. | |||||