Skip to content
Snippets Groups Projects
mesh_carver.py 133 KiB
Newer Older
  • Learn to ignore specific revisions
  • 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404
                self.InitBrushQRotation = None
                self.InitBrushERotation = None
                self.InitBrushARotation = None
    
                self.ObjectBrush_DT = "WIRE"
                self.XRay = False
    
                # Grid mesh
                self.nbcol = 1
                self.nbrow = 1
                self.gapx = 0
                self.gapy = 0
                self.scale_x = 1
                self.scale_y = 1
    
                self.GridScaleX = False
                self.GridScaleY = False
    
                if len(context.selected_objects) > 1:
                    self.ObjectBrush = context.active_object
    
                #---Copy the brush object
                    ob = bpy.data.objects.new("CarverBrushCopy", context.object.data.copy())
                    ob.location = self.ObjectBrush.location
                    scene = context.scene
                    scene.objects.link(ob)
                    scene.update()
    
                    # Get default variables
                    self.InitBrushPosition = self.ObjectBrush.location.copy()
                    self.InitBrushScale = self.ObjectBrush.scale.copy()
                    self.InitBrushQRotation = self.ObjectBrush.rotation_quaternion.copy()
                    self.InitBrushERotation = self.ObjectBrush.rotation_euler.copy()
                    self.ObjectBrush_DT = self.ObjectBrush.draw_type
                    self.XRay = self.ObjectBrush.show_x_ray
                    # Test if flat object
                    z = self.ObjectBrush.data.vertices[0].co.z
                    ErrorMarge = 0.01
                    self.Solidify_Active_Start = True
                    for v in self.ObjectBrush.data.vertices:
                        if abs(v.co.z - z) > ErrorMarge:
                            self.Solidify_Active_Start = False
                            break
                    self.SolidifyPossible = False
    
                self.CList = []
                self.OPList = []
                self.RList = []
                self.OB_List = []
    
                for ent in context.selected_objects:
                    if ent != self.ObjectBrush:
                        self.OB_List.append(ent)
    
                # Left button
                self.LMB = False
    
                # Undo Variables
                self.undo_index = 0
                self.undo_limit = context.user_preferences.edit.undo_steps
                self.undo_list = []
    
                # Boolean operations type
                self.BooleanType = 0
    
                self.UList = []
                self.UList_Index = -1
                self.UndoOps = []
    
                context.window_manager.modal_handler_add(self)
                return {'RUNNING_MODAL'}
            else:
                self.report({'WARNING'}, "View3D not found, cannot run operator")
                return {'CANCELLED'}
        #---------------------------------------------------------------------------------------------------
    
        #---------------------------------------------------------------------------------------------------
        def CreateGeometry(self):
            context = bpy.context
    
            region_id = context.region.id
    
            bLocalView = False
            for area in context.screen.areas:
                if area.type == 'VIEW_3D':
                    if area.spaces[0].local_view is not None:
                        bLocalView = True
    
            if bLocalView:
                bpy.ops.view3d.localview()
    
            if self.ExclusiveCreateMode:
                # Default width
                objBBDiagonal = 0.5
            else:
                ActiveObj = self.CurrentSelection[0]
                if ActiveObj is not None:
                    objBBDiagonal = objDiagonal(ActiveObj) / 4
            subdivisions = 2
    
            if len(context.selected_objects) > 0:
                bpy.ops.object.select_all(action='TOGGLE')
    
            context.scene.objects.active = self.CurrentObj
    
            bpy.data.objects[self.CurrentObj.name].select = True
            bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')
    
            bpy.ops.object.mode_set(mode='EDIT')
            bpy.ops.mesh.select_all(action='SELECT')
            bpy.ops.mesh.select_mode(type="EDGE")
            if self.snapCursor == False:
                bpy.ops.transform.translate(value=self.ViewVector * objBBDiagonal * subdivisions)
            bpy.ops.mesh.extrude_region_move(
                TRANSFORM_OT_translate={"value": -self.ViewVector * objBBDiagonal * subdivisions * 2})
    
            bpy.ops.mesh.select_all(action='SELECT')
            bpy.ops.mesh.normals_make_consistent()
            bpy.ops.object.mode_set(mode='OBJECT')
    
            saved_location_0 = context.scene.cursor_location.copy()
            bpy.ops.view3d.snap_cursor_to_active()
            saved_location = context.scene.cursor_location.copy()
            bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
            context.scene.cursor_location = saved_location
            bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
            context.scene.cursor_location = saved_location_0
    
            bpy.data.objects[self.CurrentObj.name].select = True
            bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')
    
            for o in self.SavSel:
                bpy.data.objects[o.name].select = True
    
            if bLocalView:
                bpy.ops.view3d.localview()
    
            self.bDone = False
            self.mouse_path.clear()
            self.mouse_path = [(0, 0), (0, 0)]
        #---------------------------------------------------------------------------------------------------
    
        #---------------------------------------------------------------------------------------------------
        def Cut(self):
            context = bpy.context
    
            UNDO = []
    
            # Local view ?
            bLocalView = False
            for area in context.screen.areas:
                if area.type == 'VIEW_3D':
                    if area.spaces[0].local_view is not None:
                        bLocalView = True
    
            if bLocalView:
                bpy.ops.view3d.localview()
    
            # Save cursor position
            CursorLocation = context.scene.cursor_location.copy()
    
            ActiveObjList = []
            if (self.ObjectMode == False) and (self.ProfileMode == False):
                objBBDiagonal = objDiagonal(self.CurrentSelection[0])
                subdivisions = 32
                if self.DontApply:
                    subdivisions = 1
    
                # Get selected objects
                ActiveObjList = context.selected_objects.copy()
    
                bpy.ops.object.select_all(action='TOGGLE')
    
                context.scene.objects.active = self.CurrentObj
    
                bpy.data.objects[self.CurrentObj.name].select = True
                bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')
    
                bpy.ops.object.mode_set(mode='EDIT')
                bpy.ops.mesh.select_all(action='SELECT')
                bpy.ops.mesh.select_mode(type="EDGE")
                if (self.snapCursor == False) or (self.ForceRebool):
                    bpy.ops.transform.translate(value=self.ViewVector * objBBDiagonal * subdivisions)
                bpy.ops.mesh.extrude_region_move(
                    TRANSFORM_OT_translate={"value": -self.ViewVector * objBBDiagonal * subdivisions * 2})
                bpy.ops.mesh.select_all(action='SELECT')
                bpy.ops.mesh.normals_make_consistent()
                bpy.ops.object.mode_set(mode='OBJECT')
            else:
                # Create liste
                if self.ObjectMode:
                    for o in self.CurrentSelection:
                        if o != self.ObjectBrush:
                            ActiveObjList.append(o)
                    self.CurrentObj = self.ObjectBrush
                else:
                    ActiveObjList = self.CurrentSelection
                    self.CurrentObj = self.ProfileBrush
    
            for o in self.CurrentSelection:
                UndoAdd(self, "MESH", o)
    
            # List objects create with rebool
            lastSelected = []
    
            for ActiveObj in ActiveObjList:
                context.scene.cursor_location = CursorLocation
    
                if len(context.selected_objects) > 0:
                    bpy.ops.object.select_all(action='TOGGLE')
    
                # Testif intitiale object has bevel
                BevelAO = False
                for obj in ActiveObjList:
                    for mb in obj.modifiers:
                        if mb.type == 'BEVEL':
                            BevelAO = True
    
                # Select cut object
                bpy.data.objects[self.CurrentObj.name].select = True
                context.scene.objects.active = self.CurrentObj
    
                bpy.ops.object.mode_set(mode='EDIT')
                bpy.ops.mesh.select_all(action='SELECT')
                bpy.ops.object.mode_set(mode='OBJECT')
    
                # Select object to cut
                bpy.data.objects[ActiveObj.name].select = True
                context.scene.objects.active = ActiveObj
    
                bpy.ops.object.mode_set(mode='EDIT')
                bpy.ops.mesh.select_all(action='DESELECT')
                bpy.ops.object.mode_set(mode='OBJECT')
    
                # Boolean operation
                if (self.shift == False) and (self.ForceRebool == False):
                    if self.ObjectMode or self.ProfileMode:
                        if self.BoolOps == UNION:
                            # Union
                            boolean_union()
                        else:
                            # Cut object
                            boolean_difference()
                    else:
                        # Cut
                        boolean_difference()
    
                    # Apply booleans
                    if self.DontApply == False:
                        BMname = "CT_" + self.CurrentObj.name
                        for mb in ActiveObj.modifiers:
                            if (mb.type == 'BOOLEAN') and (mb.name == BMname):
                                try:
                                    bpy.ops.object.modifier_apply(apply_as='DATA', modifier=BMname)
                                except:
                                    bpy.ops.object.modifier_remove(modifier=BMname)
                                    exc_type, exc_value, exc_traceback = sys.exc_info()
                                    self.report({'ERROR'}, str(exc_value))
    
                    bpy.ops.object.select_all(action='TOGGLE')
                else:
                    if self.ObjectMode or self.ProfileMode:
                        for mb in self.CurrentObj.modifiers:
                            if (mb.type == 'SOLIDIFY') and (mb.name == "CT_SOLIDIFY"):
                                try:
                                    bpy.ops.object.modifier_apply(apply_as='DATA', modifier="CT_SOLIDIFY")
                                except:
                                    exc_type, exc_value, exc_traceback = sys.exc_info()
                                    self.report({'ERROR'}, str(exc_value))
    
                    # Rebool
                    Rebool(context, self)
                    # Test if not empty object
                    if context.selected_objects[0]:
                        rebool_RT = context.selected_objects[0]
                        if len(rebool_RT.data.vertices) > 0:
                            # Create Bevel for new objects
                            if BevelAO:
                                CreateBevel(context, context.selected_objects[0])
                            UndoAdd(self, "REBOOL", context.selected_objects[0])
    
                            context.scene.cursor_location = ActiveObj.location
                            bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
                        else:
                            bpy.ops.object.delete(use_global=False)
    
                    context.scene.cursor_location = CursorLocation
    
                    if self.ObjectMode:
                        context.scene.objects.active = self.ObjectBrush
                    if self.ProfileMode:
                        context.scene.objects.active = self.ProfileBrush
    
                if self.DontApply == False:
                    # Apply booleans
                    BMname = "CT_" + self.CurrentObj.name
                    for mb in ActiveObj.modifiers:
                        if (mb.type == 'BOOLEAN') and (mb.name == BMname):
                            try:
                                bpy.ops.object.modifier_apply(apply_as='DATA', modifier=BMname)
                            except:
                                bpy.ops.object.modifier_remove(modifier=BMname)
                                exc_type, exc_value, exc_traceback = sys.exc_info()
                                self.report({'ERROR'}, str(exc_value))
                    # Get new objects created with rebool operations
                    if len(context.selected_objects) > 0:
                        if self.shift == True:
                            # Get the last object selected
                            lastSelected.append(context.selected_objects[0])
    
            context.scene.cursor_location = CursorLocation
    
            if self.DontApply == False:
                # Remove cut object
                if (self.ObjectMode == False) and (self.ProfileMode == False):
                    if len(context.selected_objects) > 0:
                        bpy.ops.object.select_all(action='TOGGLE')
                    bpy.data.objects[self.CurrentObj.name].select = True
                    cname = self.CurrentObj.name
                    bpy.ops.object.delete(use_global=False)
                else:
                    if self.ObjectMode:
                        self.ObjectBrush.draw_type = self.ObjectBrush_DT
    
            if len(context.selected_objects) > 0:
                bpy.ops.object.select_all(action='TOGGLE')
    
            # Select cutted objects
            for obj in lastSelected:
                bpy.data.objects[obj.name].select = True
    
            for ActiveObj in ActiveObjList:
                bpy.data.objects[ActiveObj.name].select = True
                context.scene.objects.active = ActiveObj
            # Update bevel
            list_act_obj = context.selected_objects.copy()
            if self.Auto_BevelUpdate:
                update_bevel(context)
    
            # Reselect intiale objects
            bpy.ops.object.select_all(action='TOGGLE')
            if self.ObjectMode:
                # Reselect brush
                self.ObjectBrush.select = True
            for ActiveObj in ActiveObjList:
                bpy.data.objects[ActiveObj.name].select = True
                context.scene.objects.active = ActiveObj
    
            # If object has children, set "Wire" draw type
            if self.ObjectBrush is not None:
                if len(self.ObjectBrush.children) > 0:
                    self.ObjectBrush.draw_type = "WIRE"
            if self.ProfileMode:
                self.ProfileBrush.draw_type = "WIRE"
    
            if bLocalView:
                bpy.ops.view3d.localview()
    
            # Reset variables
            self.bDone = False
            self.mouse_path.clear()
            self.mouse_path = [(0, 0), (0, 0)]
    
            self.ForceRebool = False
    
    
    classes = (
        Carver,
        )
    
    addon_keymaps = []
    
    
    def register():
        bpy.types.Scene.DepthCursor = bpy.props.BoolProperty(name="DepthCursor", default=False)
        bpy.types.Scene.OInstanciate = bpy.props.BoolProperty(name="Obj_Instantiate", default=False)
        bpy.types.Scene.ORandom = bpy.props.BoolProperty(name="Random_Rotation", default=False)
        bpy.types.Scene.DontApply = bpy.props.BoolProperty(name="Dont_Apply", default=False)
        bpy.types.Scene.nProfile = bpy.props.IntProperty(name="Num_Profile", default=0)
    
        bpy.utils.register_class(CarverPrefs)
    
        bpy.utils.register_class(Carver)
        # add keymap entry
        kcfg = bpy.context.window_manager.keyconfigs.addon
        if kcfg:
            km = kcfg.keymaps.new(name='3D View', space_type='VIEW_3D')
            kmi = km.keymap_items.new("object.carver", 'X', 'PRESS', shift=True, ctrl=True)
            addon_keymaps.append((km, kmi))
    
    
    def unregister():
        bpy.utils.unregister_class(CarverPrefs)
    
       # remove keymap entry
        for km, kmi in addon_keymaps:
            km.keymap_items.remove(kmi)
        addon_keymaps.clear()
    
        bpy.utils.unregister_class(Carver)
    
    
    if __name__ == "__main__":
        register()