Subscribe to our free newsletter

To make sure you won't miss any valuable content we share with our community.

How to Easily Trim Objects in Blender with A Clean Cut

To Trim or cut objects in Blender can sometimes seem impossible as the tools in Blender will only allow you to cut only through the meshes. As a result, you will sometimes end up in a situation where you cannot find a proper way to cut your object. In this tutorial, we will show you a way to create a tool using which you can make your own custom cut on the shape from above. However, you can develop this tool in a way that it can cut from all angles.

Making A Tool to Trim Objects in Blender

trim objects in Blender

We want to create a tool that you can draw your custom cut shape and after you have made sure the cut shape is alright, confirm it.

trim objects in Blender

So let’s get started. Using the below scripts, we will be able to easily trim with a clear cut through the meshes with the pencil tool. We first begin with the utility functions and then execute them within a sequence. In the panel that is created after the running of the codes, we will see 2 buttons:
  1. For drawing the pattern.
  2. For applying the cut with the pattern drawn by the user.

import  bpy
import bmesh
import math

####################################################################
#####                Utility Functions
####################################################################
class BOOLEAN_TYPE:
    UNION = 'UNION'
    DIFFERENCE = 'DIFFERENCE'
    INTERSECT = 'INTERSECT'
       
def make_boolean(obj1, obj2, boolean_type):
    if not obj1 or not obj2:
        return

    modifier = obj1.modifiers.new(name='booly', type='BOOLEAN')
    modifier.object = obj2
    modifier.operation = boolean_type
    res = bpy.ops.object.modifier_apply({"object": obj1}, apply_as='DATA', modifier=modifier.name)

    assert "FINISHED" in res, "Error"


The above function will apply the boolean operation on the object specified according to the type of boolean categorized in the class BOOLEAN_TYPE.

def delete_object(objName):
    
    bpy.ops.object.select_all(action='DESELECT')
    bpy.data.objects[objName].select_set(True) # Blender 2.8x
    bpy.ops.object.delete()


The above function will delete any given object. To delete an object, we need to first deselect all the other objects and then select the object that we specified its name in the function and finally delete the selected object.

def get_object_by_name(obj_name):
    assert obj_name in bpy.data.objects, "Error getting object by name:	{}".format(obj_name)
    obj = bpy.data.objects[obj_name]
    return obj


The above function will get the object by its name. Meaning that it will select it according to the name given.

####################################################################
########             Main Panel
####################################################################

class MainPanel(bpy.types.Panel):
    bl_label = "Object Adder"
    bl_idname = "VIEW_PT_MainPanel"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = 'Design Automation'
    
    def draw(self, context):
        layout = self.layout
        layout.scale_y = 1.2
        
        row = layout.row()
        row.label(text= "Design Automation", icon= 'OBJECT_ORIGIN')
        row = layout.row()
        row.operator("wm_trim.myop", text= "Trim from above")           
        row = layout.row()
        row.operator("wm_confirm.myop", text= "Confirm the cut")

####################################################################
####                  Main UI ّFunctions                  
####################################################################

class WM_Trim_myOp(bpy.types.Operator):
    """Draw The trim pattern"""
    bl_label = "Draw The trim pattern"
    bl_idname = "wm_trim.myop"
      
    def execute(self, context):
             
        bpy.ops.curve.primitive_nurbs_curve_add(enter_editmode=False, align='WORLD', location=(0,0,0))
        bpy.ops.object.editmode_toggle()
        bpy.context.scene.tool_settings.curve_paint_settings.curve_type = 'BEZIER'
        bpy.ops.curve.delete(type='VERT')
        
        return {'FINISHED'}
    def invoke(self, context, event):
        return context.window_manager.invoke_props_dialog(self)


In the above class, we have provided everything for the user to easily draw a pattern. This preparation contains creating a Nurbs Curve, switching to edit mode, specifying the type of the curve as Bezier, and deleting a curve. All of these tasks provide means for the user to draw another curve for trimming.

class WM_Confirm_myOp(bpy.types.Operator):
    """Click OK to confirm"""
    bl_label = "Click OK to confirm"
    bl_idname = "wm_confirm.myop"
    name = bpy.props.StringProperty(name= "Enter the name of the object to be trimmed", default= "")
          
    def execute(self, context):

        name = self.name                     
        bpy.context.object.data.dimensions = '2D'
        bpy.context.object.data.fill_mode = 'BOTH'
        bpy.context.object.data.extrude = 1000
        bpy.ops.object.editmode_toggle()
        context = bpy.context
        scene = context.scene
        cut = scene.objects.get("NurbsCurve")
        bpy.ops.object.convert(target='MESH')
        
        obj1 = get_object_by_name('%s'%name)
        obj2 = get_object_by_name('NurbsCurve')
        
        make_boolean(obj1, obj2, 'DIFFERENCE')
        delete_object("NurbsCurve")
        #fixMesh('%s'%name)

        return {'FINISHED'}
    def invoke(self, context, event):
        return context.window_manager.invoke_props_dialog(self)




The above class will fill the curve and extrude it for the height of 1000 mm (You can increase the number if you have a larger object than that height) and switches back to object mode then it will subtract the created object from the main object.

Don’t forget to add the ending of your script:

####################################################################
#####                     Register and Unregister
####################################################################

         
def register():
    bpy.utils.register_class(MainPanel)
    bpy.utils.register_class(WM_Trim_myOp)
    bpy.utils.register_class(WM_Confirm_myOp)                                            
    

def unregister():
    bpy.utils.unregister_class(MainPanel)
    bpy.utils.unregister_class(WM_Trim_myOp)
    bpy.utils.unregister_class(WM_Confirm_myOp)   
                                                       
    
if __name__ == "__main__":
    register()



Now, let’s run the code and test our tool. After you have successfully run the code and the panel has appeared, you can click the Trim from the above button and choose the pencil tool from the left-hand side of the page where we have a toolbar and then draw the pattern from the top view. You can set the view from the top by clicking on the z-axis (blue circle). After you have drawn the pattern, it is time to apply the pattern for trimming. To do so, click on the Confirm the Cut button. Let’s follow the instruction with the photos:

trim objects in Blender

Using the pencil tool from the left-hand side menu draw the shape that you want to trim from above (Notice that your view must be perpendicular to the XY plane).

blender

blender

Now, click Confirm the Cut, enter the name of the shape that is going to be trimmed, and then click OK.

blender

And as you can see, we have successfully trimmed our object exactly the way we had drawn.

Final Thought

In this tutorial, we have managed to show you a way to create a tool using which you can make your own custom cut on the shape from above. However, you can develop this tool in a way that it can cut from all angles.

Download this Article in PDF format

3d websites

Care to Know Us More?

In Arashtad, we have gathered a professional team of developers who are working in fields such as 3D websites, 3D games, metaverses, and other types of WebGL and 3D applications.

Arashtad Serivces
Drop us a message and tell us about your ideas.
Fill in the Form
3D Development

Retrieve the Coordinates of a Selected Point in Blender

The purpose of this article is to find an easy way to obtain the coordinates and the normal of a point in Blender specified by the user. Using the obtained point data, we can automate the translation of the objects that are going to be transferred to that certain point and rotated according to the normal directions of that point. The use cases of this kind of object translation are for 3D modeling of complex objects as well as placing the internal lattice structures.

Obtaining the Coordinates of Any Point in Blender

Finding the coordinates of a selected point is a key function for a lot of other important tasks, such as translating another part to that point and so on. Here, we write some functions in addition to an interface for you to get the coordinates of a selected point on an object and print the data of the location and the direction of the mesh normal.

blender

Notice that the user should go to edit mode while selecting the object and click on the edge that they want to get the coordinates from. As you know edges have no normals so we have to find the closest mesh to the edge that has been selected by the user.

Using the below python scripts in the scripts section of Blender, we will define some utility functions as well as the main execute class that is going to apply the utility functions with a certain sequence.

import  bpy
import bmesh

####################################################################
#####                Utility Functions
####################################################################

def object_closest_point_mesh(p, obj):

    result, location, normal, face_index = obj.closest_point_on_mesh(p)
    assert result, "Can't find closest point on mesh"

    location = location.to_tuple()
    normal = normal.to_tuple()


The above function will find the closest point on the mesh for us. The function itself uses another function that is built in Blender and finds the closest vertex on a mesh based on the given point and returns the location and the normal data of the said vertex. Finally, it returns the 2 sets of data.

def get_vertex():

        bm = bmesh.new()
        ob = bpy.context.active_object
        bm = bmesh.from_edit_mesh(ob.data)

        points = []
        for v in bm.verts:
            if (v.select == True):
                obMat = ob.matrix_world
                points.append(obMat @ v.co)       
        for p in points:
            pOb = bpy.data.objects.new("VertexPoint", None)
            bpy.context.collection.objects.link(pOb)
            pOb.location = p
        return p


The above function will get the vertex that has been selected by the user in the edit mode and returns the coordinates of the said vertex.


def delete_object(objName):    
    bpy.ops.object.select_all(action='DESELECT')
    bpy.data.objects[objName].select_set(True) # Blender 2.8x
    bpy.ops.object.delete()


The above function will delete any given object. To delete an object, we need to first deselect all the other objects and then select the object that we specified its name in the function and finally delete the selected object.

def get_object_by_name(obj_name):
    assert obj_name in bpy.data.objects, "Error getting object by name: {}".format(obj_name)
    obj = bpy.data.objects[obj_name]
    return obj


The above function will get the object by its name. Meaning that it will select according to the name given.

####################################################################
########             Main Panel
####################################################################

class MainPanel(bpy.types.Panel):
    bl_label = "Object Adder"
    bl_idname = "VIEW_PT_MainPanel"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = 'Design Automation'
    
    def draw(self, context):
        layout = self.layout
        layout.scale_y = 1.2
        
        row = layout.row()
        row.label(text= "Design Automation", icon= 'OBJECT_ORIGIN')
        row = layout.row()
        row.operator("wm_function.myop", text= "retrieve the point data")           


####################################################################
####                  Main UI ّFunctions                  
####################################################################

class WM_Function_myOp(bpy.types.Operator):
    """Go to edit mode and determine the point then Click the button"""
    bl_label = "Our customized function"
    bl_idname = "wm_function.myop"
        
    def execute(self, context):
        
        p = get_vertex()            
        bpy.ops.object.editmode_toggle()  
        obj = get_object_by_name('Sphere')   
        print (object_closest_point_mesh(p,obj))
        delete_object("VertexPoint")
        return {'FINISHED'}
    
    def invoke(self, context, event):
        
        return context.window_manager.invoke_props_dialog(self)  



The above execute function is so simple. It first gets the selected vertex in the edit mode from the user, then it toggles the edit mode to object mode. After that, it prints the coordinates and the normals of the selected point. And at last, it deletes the VertexPoint object from the list of objects.

####################################################################
#####                     Register and Unregister
####################################################################
        
def register():
    bpy.utils.register_class(MainPanel)
    bpy.utils.register_class(WM_Function_myOp)
                                               
def unregister():
    bpy.utils.unregister_class(MainPanel)
    bpy.utils.unregister_class(WM_Function_myOp)                                                      
    
if __name__ == "__main__":
    register()


Don’t forget to close the project by registering and unregistering the classes defined. If you run the scripts, you will be able to see the panel below. Click the button in the panel (retrieve the point data button) and Click OK and notice that before you click OK, you need to have determined the point you wish to select, in the edit mode of Blender.

coordinates of a point in blender

The following is the result of retrieved data of the point: (-0.46193963289260864, 0.3086579740047455, 0.8314695954322815, -0.41729676723480225, 0.22304992377758026, 0.8809722661972046) Info: Deleted 1 object(s) The first 3 numbers are the XYZ coordinates of the point and the second 3 numbers are related to the direction of the mesh normal.

The Job Is Done

In this tutorial, we have managed to propose a way to quickly obtain the coordinates and normal directions of any point on an object. This data is very useful especially when you want to operate some complex 3D modeling or translate objects to a certain point on another object.

Download this Article in PDF format

3d websites

Care to Know Us More?

In Arashtad, we have gathered a professional team of developers who are working in fields such as 3D websites, 3D games, metaverses, and other types of WebGL and 3D applications.

Arashtad Serivces
Drop us a message and tell us about your ideas.
Fill in the Form
3D Development