Subscribe to our free newsletter

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

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

How to Instantly Translate An Object in Blender

To Translate an object to the place we visually determine by eyeballing in Blender is a time-taking process especially when we are working with a great number of objects. In this tutorial, we are going to see how we can instantly move an object to the desired location we specify with the cursor. To do so, we first need to retrieve the point, and then using the acquired coordinates of the point, we can change the location of the object.

Translate An Object to A Special Point in Blender

Translating an object to the place we visually determine by eyeballing is a time-taking process especially when we are working with a great number of objects. In this tutorial, we are going to see how we can instantly move an object to the desired location we specify with the cursor. To do so we first need to retrieve the point and then using the acquired coordinates of the point, we can change the location of the object.

translate an object in Blender

With that said, let’s get started:

import  bpy
import bmesh
import math
####################################################################
#####                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()
    return location + normal  # return tuple of 6 floats

def obj_transform(filename, obj_name, size, location, angle):    
    ob = bpy.context.scene.objects[obj_name]       # Get the object
    bpy.ops.object.select_all(action='DESELECT') # Deselect all objects
    bpy.context.view_layer.objects.active = ob   # Make the cube the active object
    ob.select_set(True)             
    obj = bpy.data.objects[obj_name]
    obj.location = location
    bpy.ops.transform.rotate(value=angle, orient_axis='Z',
        orient_type='GLOBAL',
        orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)),
        constraint_axis=(False, False, True))
                                                               
def object_put_part(part_name, point, obj, scale, obj_name):
    vx,vy,vz,a,b,c = object_closest_point_mesh(point, obj)
    a1 = math.atan2(b, a)
    obj_transform(part_name, obj_name, scale, (point[0], point[1], point[2]), a1)


The above functions will get the specified point and translate our object part_name on the main object obj.

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 we specified and return its data. The data contains the coordinates of the point that has been selected. Here, we do not need the orientation of the normal object. Otherwise, we would have had to use another script to be able to retrieve the data of the normal vector as well.

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()

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

####################################################################
########             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= "Translate the Object")           

####################################################################
####                  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"
    
    scale = bpy.props.FloatProperty(name= "Enter the scale of lattice", default= 1)
    
    def execute(self, context):
        
        Scale = self.scale        
        point = get_vertex()            
        bpy.ops.object.editmode_toggle()  
        obj = get_object_by_name('Cylinder')   
        object_put_part('Sphere', point, obj, Scale, 'Sphere')
        delete_object("VertexPoint")
        return {'FINISHED'}
    
    def invoke(self, context, event):       
        return context.window_manager.invoke_props_dialog(self)  


In the above def execute function, we use the get vertex function to get the data of the point that the user specifies. Then, use object_put_part to translate the object that we want to translate.

####################################################################
#####                     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()


Now, if we test our UI by determining a point on the main object:

translate an object in Blender

And clicking on the Translate the object button and then clicking OK, we will see that our object has been translated to our specified point.

Translate An Object in Blender: Final Word

In this tutorial, we have managed to create a tool in Blender that will help us translate (change the location of the object) using the point specified by the user on the object. This tool is very useful for designing complex 3D models especially if we want to save some time for the creativity of the design rather than operating time-taking tasks.

Download this Article in PDF format

web developement

Check Out Our Services

In Arashtad, we’re working on 3D games, metaverses, and other types of WebGL and 3D applications with our 3D web development team. However, our services are not limited to these. Back-end developments, front-end developments, 3d modeling, and animations are in our arsenal too.

Arashtad Serivces
Drop us a message and tell us about your ideas.
Tell Us What You Need
3D Development