2
$\begingroup$

I want to implement an extrude functionality like the one in Blender, but with vertices I draw in a draw handler.

I have the 2d position of the mouse at which the extrusion of my vertices starts and the current mouse position. I also know the direction as 3d normalized vector in which to extrude.

I tried to calculate the 3d locations for mouse positions to get the extrusion length like this

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)

self._extrusion = diff_vec.length

self.extrude_vertices(context)

# Extrusion of vertices
def extrude_vertices(self, context):

    dir = self.get_dir() * self._extrusion
    for index, vertex3d in enumerate(self._vertices):    
         self._vertices_extruded[index] = vertex3d + dir

Problems:

  • I cant extrude negative direction, it's always positive
  • I want to include the direction of the extrusion into the extrusion amount (like the extrude in Blender: When the mouse is moved along the direction it extrudes more, if not (e.g. perpendicular to extrude direction) we get less extrusion.

enter image description here

So what is needed is to find the amount of extrusion for the direction vector d.

Any help is appreciated.

Update:

I added this, I guess it must have something to do with the dot product:

diff_vec = (ext_3d - mouse_3d)
dot_prod = diff_vec.dot(dir.orthogonal())        
self._extrusion = dot_prod 

But the amount is not correct.

$\endgroup$
0
$\begingroup$

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:

  1. 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

  2. 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
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.