Welcome to mirror list, hosted at ThFree Co, Russian Federation.

operator.py « object_fracture_cell - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c01e866a66e6d49c7295d9cd357bd910818e7d07 (plain)
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

if "bpy" in locals():
    import importlib
    importlib.reload(cell_main)
    importlib.reload(crack_functions)
    importlib.reload(material_functions)
    importlib.reload(utilities)

else:
    from .process import cell_main
    from .process import crack_functions
    from .process import material_functions
    from . import utilities

import bpy

from bpy.types import (
        Operator,
        )

class FRACTURE_OT_Cell(Operator):
    bl_idname = "object.add_fracture_cell"
    bl_label = "Cell fracture"
    bl_description = "Make fractured cells from selected object."
    bl_options = {'REGISTER', 'UNDO'}

    @classmethod
    def poll(cls, context):
        obj = context.active_object
        return obj and obj.type == "MESH"

    def execute(self, context):
        #keywords = self.as_keywords()  # ignore=("blah",)

        fracture_cell_props = context.window_manager.fracture_cell_props
        cell_keywords = utilities._cell_props_to_dict(fracture_cell_props)

        originals = context.selected_editable_objects
        for original in originals:
            cell_main.main(context, original, **cell_keywords)

        return {'FINISHED'}

    def invoke(self, context, event):
        wm = context.window_manager
        return wm.invoke_props_dialog(self, width=350)

    def draw(self, context):
        cell_props = context.window_manager.fracture_cell_props

        layout = self.layout

        box = layout.box()
        col = box.column()
        col.label(text="Fracture From")
        row = col.row()
        #row.prop(cell_props, "source")
        row.prop(cell_props, "source_vert_own")
        row.prop(cell_props, "source_vert_child")
        row = col.row()
        row.prop(cell_props, "source_particle_own")
        row.prop(cell_props, "source_particle_child")
        row = col.row()
        row.prop(cell_props, "source_random")
        row.prop(cell_props, "source_pencil")

        box = layout.box()
        col = box.column()
        col.label(text="Transform")
        row = col.row()
        row.prop(cell_props, "pre_simplify")
        row.prop(cell_props, "source_noise")
        row = col.row(align=True)
        row.prop(cell_props, "margin")
        row.prop(cell_props, "use_recenter")
        row = col.row(align=True)
        row.prop(cell_props, "cell_scale")
        # could be own section, control how we subdiv
        #row.prop(cell_props, "use_island_split")

        box = layout.box()
        col = box.column()
        col.label(text="Recursive Shatter")
        row = col.row(align=True)
        row.prop(cell_props, "recursion")
        row.prop(cell_props, "recursion_chance")
        row = col.row(align=True)
        if cell_props.recursion > 0:
            row.enabled = True
        else:
            row.enabled = False
        row.prop(cell_props, "recursion_source_limit")
        row.prop(cell_props, "recursion_clamp")
        row = col.row()
        row.prop(cell_props, "recursion_chance_select")#, expand=True)

        box = layout.box()
        col = box.column()
        col.label(text="Interior Meshes")
        row = col.row(align=True)
        row.prop(cell_props, "use_data_match")
        row.prop(cell_props, "use_interior_vgroup")
        row = col.row(align=True)
        row.prop(cell_props, "use_smooth_faces")
        row.prop(cell_props, "use_sharp_edges")
        if cell_props.use_sharp_edges == True:
            row.prop(cell_props, "use_sharp_edges_apply")

        row = col.row()
        if cell_props.use_data_match == True:
            row.enabled = True
        else:
            row.enabled = False
        row.alignment = 'LEFT'
        # on same row for even layout but infact are not all that related
        row.prop(cell_props, "material_index")

        box = layout.box()
        col = box.column()
        col.label(text="Custom Properties")
        row = col.row(align=True)
        row.prop(cell_props, "use_mass")
        if cell_props.use_mass:
            row = col.row(align=True)
            row.prop(cell_props, "mass_name")
            row = col.row(align=True)
            row.prop(cell_props, "mass_mode")
            row.prop(cell_props, "mass")

        box = layout.box()
        col = box.column()
        col.label(text="Object Management")
        row = col.row(align=True)
        row.prop(cell_props, "original_hide")
        row.prop(cell_props, "cell_relocate")

        box = layout.box()
        col = box.column()
        col.label(text="Collections:")
        row = col.row(align=True)
        row.prop(cell_props, "use_collection")
        if cell_props.use_collection:
            row.prop(cell_props, "new_collection")
            row.prop(cell_props, "collection_name")

        box = layout.box()
        col = box.column()
        col.label(text="Debug")
        row = col.row(align=True)
        row.prop(cell_props, "use_debug_points")
        row.prop(cell_props, "use_debug_bool")
        row = col.row(align=True)
        row.prop(cell_props, "use_debug_redraw")

class FRACTURE_OT_Crack(Operator):
    bl_idname = "object.add_fracture_crack"
    bl_label = "Cell To Crack"
    bl_description = "Make a cracked object from cell objects"
    bl_options = {'REGISTER', 'UNDO'}

    @classmethod
    def poll(cls, context):
        obj = context.active_object
        return obj and obj.type == "MESH"

    def invoke(self, context, event):
        wm = context.window_manager
        return wm.invoke_props_dialog(self, width=350)

    def execute(self, context):
        crack_props = context.window_manager.fracture_crack_props

        cells = context.selected_editable_objects
        object = None

        if cells:
            # clear sharp edges for correct crack surface.
            bpy.context.view_layer.objects.active = cells[0]
            bpy.ops.object.mode_set(mode='EDIT')
            bpy.ops.mesh.reveal()

            bpy.ops.mesh.mark_sharp(clear=True, use_verts=True)
            bpy.ops.object.mode_set(mode='OBJECT')

            for cell in cells:
                bpy.context.view_layer.objects.active = cell
                bpy.ops.object.modifier_remove(modifier="EDGE_SPLIT_cell")
                bpy.context.object.vertex_groups.clear()

            bpy.context.view_layer.objects.active = cells[0]
            object = crack_functions.make_join(cells)

        if object:
            bpy.context.view_layer.objects.active = object

            crack_functions.add_modifiers()
            bpy.context.object.modifiers['DECIMATE_crackit'].ratio = crack_props.modifier_decimate
            bpy.context.object.modifiers['SMOOTH_crackit'].factor = crack_props.modifier_smooth

            crack_functions.multiExtrude(
                off=0.1,
                rotx=0, roty=0, rotz=0,
                sca=crack_props.extrude_scale,
                var1=crack_props.extrude_var, var2=crack_props.extrude_var, var3=crack_props.extrude_var,
                num=crack_props.extrude_num, ran=0
                )
            bpy.ops.object.modifier_apply(apply_as='DATA', modifier='DECIMATE_crackit')
            bpy.ops.object.shade_smooth()

            if crack_props.modifier_wireframe == True:
                bpy.ops.object.modifier_add(type='WIREFRAME')
                wireframe = bpy.context.object.modifiers[-1]
                wireframe.name = 'WIREFRAME_crackit'
                wireframe.use_even_offset = False
                wireframe.thickness = 0.01
        else:
            assert("Joining into One object had been failed. Mesh object can only be joined.")

        return {'FINISHED'}

    def draw(self, context):
        cell_props = context.window_manager.fracture_cell_props
        crack_props = context.window_manager.fracture_crack_props
        layout = self.layout

        box = layout.box()
        col = box.column()
        col.label(text='* Execute After "1. Cell Fracture"')

        box = layout.box()
        col = box.column()
        col.label(text="Surface:")
        row = col.row(align=True)
        row.alignment = 'LEFT'
        row.prop(crack_props, "modifier_decimate")
        row.prop(crack_props, "modifier_smooth")

        col = box.column()
        col.label(text="Extrude:")
        row = col.row(align=True)
        row.prop(crack_props, "extrude_scale")
        row.prop(crack_props, "extrude_var")
        row.prop(crack_props, "extrude_num")

        col = box.column()
        col.label(text="Post Processing")
        row = col.row(align=True)
        row.prop(crack_props, "modifier_wireframe")


class FRACTURE_OT_Material(Operator):
    bl_idname = "object.add_fracture_material"
    bl_label = "Material Preset"
    bl_description = ("Material preset for cracked object")
    bl_options = {'REGISTER', 'UNDO'}

    @classmethod
    def poll(cls, context):
        obj = context.active_object
        # included - type that can have materials
        included = ['MESH', 'CURVE', 'SURFACE', 'FONT', 'META']
        return (obj is not None and obj.type in included)

    def execute(self, context):
        material_props = context.window_manager.fracture_material_props

        mat_name = material_props.material_preset
        mat_lib_name = material_props.material_lib_name
        mat_ui_name = material_props.get_ui_mat_name(mat_name) if not mat_lib_name else mat_name

        try:
            material_functions.appendMaterial(
                    mat_lib_name = mat_lib_name,
                    mat_name = mat_name,
                    mat_ui_names = mat_ui_name
                    )
        except Exception as e:
            material_functions.error_handlers(
                    self, "mesh.crackit_material", e,
                    "The active Object could not have the Material {} applied".format(mat_ui_name)
                    )
            return {"CANCELLED"}

        return {'FINISHED'}