Extrude Python for mouse pos 2d
The code for the addon can be found here:
https://github.com/jayanam/jmesh-tools

The last commit is version 1.0.1.4 to which I added the extrude with the mouse cursor for primitives.

The logic is like this:


The primitive mode is a modal operator (fc_primitive_mode_op.py) in which a shape that is painted to the surface can be extruded when the "E" key is pressed. Then the 2d start pos (current mouse pos) is stored in the types/shape.py class as member self._extrude_pos
Now when the mouse is moved the method handle_mouse_move of the shape.py is called, the mouse 2d position is passed as parameter.


I want to use the extrude start 2d pos and current mouse 2d pos difference to set the extrusion length for the vertices. All the logic for this is in the types/shape.py class. The goal is to implement an extrude functionality just like the one in Blender for meshes. Thx for your help.

def extrude_vertices(self, context):

    dir = self._extrusion * self.get_dir()

    for index, vertex3d in enumerate(self._vertices):    
        if not self._is_extruded:
            self._vertices_extruded.append(vertex3d + dir)
        else:
            self._vertices_extruded[index] = vertex3d + dir

    self._is_extruded = True

def handle_mouse_move(self, mouse_pos_2d, mouse_pos_3d, event, context):

    if mouse_pos_2d and self.is_extruding():
        dir = self.get_dir()
        mouse_3d = region_2d_to_location_3d(self._view_context.region, self._view_context, mouse_pos_2d, dir)
        ext_3d = region_2d_to_location_3d(self._view_context.region, self._view_context, self._extrude_pos, dir)

        diff_vec = (ext_3d - mouse_3d)

        dot_prod = diff_vec.dot(dir.orthogonal())

        self._extrusion = dot_prod

        self.extrude_vertices(context)
        return True