Update for changes in Blender's API
[blender-addons.git] / object_fracture_crack / operator.py
blobd6b2da7b4edb8b4b68ea4fb84f780f21578792e5
1 # gpl: author Nobuyuki Hirakata
3 import bpy
4 from bpy.types import (
5 Operator,
6 Panel,
8 from . import crack_it
11 def check_object_cell_fracture():
12 if "object_fracture_cell" in bpy.context.user_preferences.addons.keys():
13 return True
14 return False
17 # Access by bpy.ops.mesh.crackit_fracture
18 class FractureOperation(Operator):
19 bl_idname = "mesh.crackit_fracture"
20 bl_label = "Crack it!"
21 bl_description = ("Make cracks using the cell fracture add-on\n"
22 "Needs only one Selected Mesh Object")
23 bl_options = {'REGISTER', 'UNDO'}
25 @classmethod
26 def poll(cls, context):
27 obj = context.active_object
28 sel_obj = len(context.selected_objects) == 1
30 return (obj is not None and obj.type == "MESH" and sel_obj)
32 def execute(self, context):
33 if check_object_cell_fracture():
34 crackit = context.scene.crackit
35 try:
36 crack_it.makeFracture(
37 child_verts=crackit.fracture_childverts,
38 division=crackit.fracture_div, scaleX=crackit.fracture_scalex,
39 scaleY=crackit.fracture_scaley, scaleZ=crackit.fracture_scalez,
40 margin=crackit.fracture_margin
42 crack_it.addModifiers()
43 crack_it.multiExtrude(
44 off=crackit.extrude_offset,
45 var2=crackit.extrude_random, var3=crackit.extrude_random
47 bpy.ops.object.shade_smooth()
49 except Exception as e:
50 crack_it.error_handlers(
51 self, "mesh.crackit_fracture", e, "Crack It! could not be completed."
53 return {"CANCELLED"}
54 else:
55 self.report({'WARNING'},
56 "Depends on Object: Cell Fracture addon. Please enable it first. "
57 "Operation Cancelled"
59 return {"CANCELLED"}
61 return {'FINISHED'}
64 # Apply material preset
65 # Access by bpy.ops.mesh.crackit_material
66 class MaterialOperation(Operator):
67 bl_idname = "mesh.crackit_material"
68 bl_label = "Apply Material"
69 bl_description = ("Apply a preset material\n"
70 "The Material will be applied to the Active Object\n"
71 "from the type of Mesh, Curve, Surface, Font, Meta")
72 bl_options = {'REGISTER', 'UNDO'}
74 @classmethod
75 def poll(cls, context):
76 obj = context.active_object
77 # included - type that can have materials
78 included = ['MESH', 'CURVE', 'SURFACE', 'FONT', 'META']
79 return (obj is not None and obj.type in included)
81 def execute(self, context):
82 crackit = context.scene.crackit
83 mat_name = crackit.material_preset
84 mat_lib_name = crackit.material_lib_name
85 mat_ui_name = crack_it.get_ui_mat_name(mat_name) if not mat_lib_name else mat_name
87 try:
88 crack_it.appendMaterial(
89 addon_path=crackit.material_addonpath,
90 material_name=mat_name,
91 mat_ui_names=mat_ui_name
93 except Exception as e:
94 crack_it.error_handlers(
95 self, "mesh.crackit_material", e,
96 "The active Object could not have the Material {} applied".format(mat_ui_name)
98 return {"CANCELLED"}
100 return {'FINISHED'}
103 # Menu settings
104 class crackitPanel(Panel):
105 bl_label = "Crack it!"
106 bl_idname = 'crack_it'
107 bl_space_type = "VIEW_3D"
108 bl_region_type = "TOOLS"
109 bl_category = "Create"
110 bl_context = 'objectmode'
111 bl_options = {"DEFAULT_CLOSED"}
113 def draw(self, context):
114 crackit = context.scene.crackit
115 layout = self.layout
117 # Crack input
118 box = layout.box()
119 row = box.row()
121 # Warning if the fracture cell addon is not enabled
122 if not check_object_cell_fracture():
123 col = box.column()
124 col.label(text="Please enable Object: Cell Fracture addon", icon="INFO")
125 col.separator()
126 col.operator("wm.addon_userpref_show",
127 text="Go to Cell Fracture addon",
128 icon="PREFERENCES").module = "object_fracture_cell"
130 layout.separator()
131 return
132 else:
133 row.operator(FractureOperation.bl_idname, icon="SPLITSCREEN")
135 row = box.row()
136 row.prop(crackit, "fracture_childverts")
138 col = box.column(align=True)
139 col.prop(crackit, "fracture_scalex")
140 col.prop(crackit, "fracture_scaley")
141 col.prop(crackit, "fracture_scalez")
143 col = box.column(align=True)
144 col.label("Settings:")
145 col.prop(crackit, "fracture_div")
146 col.prop(crackit, "fracture_margin")
148 col = box.column(align=True)
149 col.label("Extrude:")
150 col.prop(crackit, "extrude_offset")
151 col.prop(crackit, "extrude_random")
153 # material Preset:
154 box = layout.box()
155 row = box.row()
156 row.label("Material Preset:")
157 row_sub = row.row()
158 row_sub.prop(crackit, "material_lib_name", text="",
159 toggle=True, icon="LONGDISPLAY")
160 row = box.row()
161 row.prop(crackit, "material_preset")
163 row = box.row()
164 row.operator(MaterialOperation.bl_idname)