Python bgl.glPointSize() Examples

The following are 26 code examples of bgl.glPointSize(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module bgl , or try the search function .
Example #1
Source File: utilities.py    From object_alignment with GNU General Public License v3.0 6 votes vote down vote up
def draw_3d_points_revised(context, points, color, size):
    region = context.region
    region3d = context.space_data.region_3d


    region_mid_width = region.width / 2.0
    region_mid_height = region.height / 2.0

    perspective_matrix = region3d.perspective_matrix.copy()

    bgl.glColor4f(*color)
    bgl.glPointSize(size)
    bgl.glBegin(bgl.GL_POINTS)

    for vec in points:

        vec_4d = perspective_matrix * vec.to_4d()
        if vec_4d.w > 0.0:
            x = region_mid_width + region_mid_width * (vec_4d.x / vec_4d.w)
            y = region_mid_height + region_mid_height * (vec_4d.y / vec_4d.w)
            bgl.glVertex3f(x, y, 0)
    bgl.glEnd() 
Example #2
Source File: utilities.py    From object_alignment with GNU General Public License v3.0 6 votes vote down vote up
def draw_3d_points(context, points, color, size):
    '''
    draw a bunch of dots
    args:
        points: a list of tuples representing x,y SCREEN coordinate eg [(10,30),(11,31),...]
        color: tuple (r,g,b,a)
        size: integer? maybe a float
    '''
    points_2d = [location_3d_to_region_2d(context.region, context.space_data.region_3d, loc) for loc in points]
    if None in points_2d:
        points_2d = filter(None, points_2d)
    bgl.glColor4f(*color)
    bgl.glPointSize(size)
    bgl.glBegin(bgl.GL_POINTS)
    for coord in points_2d:
        #TODO:  Debug this problem....perhaps loc_3d is returning points off of the screen.
        if coord:
            bgl.glVertex2f(*coord)

    bgl.glEnd()
    return 
Example #3
Source File: bmesh_render.py    From addon_common with GNU General Public License v3.0 6 votes vote down vote up
def glSetOptions(prefix, opts):
    if not opts:
        return

    prefix = '%s ' % prefix if prefix else ''

    def set_if_set(opt, cb):
        opt = '%s%s' % (prefix, opt)
        if opt in opts:
            cb(opts[opt])
    dpi_mult = opts.get('dpi mult', 1.0)
    set_if_set('offset', lambda v: bmeshShader.assign('offset', v))
    set_if_set('dotoffset', lambda v: bmeshShader.assign('dotoffset', v))
    set_if_set('color', lambda v: bmeshShader.assign('color', v))
    set_if_set('color selected',
               lambda v: bmeshShader.assign('color_selected', v))
    set_if_set('hidden', lambda v: bmeshShader.assign('hidden', v))
    set_if_set('width', lambda v: bgl.glLineWidth(v*dpi_mult))
    set_if_set('size', lambda v: bgl.glPointSize(v*dpi_mult))
    set_if_set('stipple', lambda v: glEnableStipple(v)) 
Example #4
Source File: mi_curve_test.py    From mifthtools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def mi_draw_2d_point(point_x, point_y, p_size=4, p_col=(1.0,1.0,1.0,1.0)):
    bgl.glEnable(bgl.GL_BLEND)
    #bgl.glColor4f(1.0, 1.0, 1.0, 0.5)
    #bgl.glLineWidth(2)

    bgl.glPointSize(p_size)
#    bgl.glBegin(bgl.GL_LINE_LOOP)
    bgl.glBegin(bgl.GL_POINTS)
 #   bgl.glBegin(bgl.GL_POLYGON)
    bgl.glColor4f(p_col[0], p_col[1], p_col[2], p_col[3])
    bgl.glVertex2f(point_x, point_y)
    bgl.glEnd()

    # restore opengl defaults
    bgl.glLineWidth(1)
    bgl.glDisable(bgl.GL_BLEND)
    bgl.glColor4f(0.0, 0.0, 0.0, 1.0)


# TODO MOVE TO UTILITIES 
Example #5
Source File: mi_curve_test.py    From mifthtools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def mi_curve_draw_3d_polyline(points, p_size, p_col, x_ray):
    bgl.glEnable(bgl.GL_BLEND)
    bgl.glLineWidth(1)

    if x_ray is True:
        bgl.glDisable(bgl.GL_DEPTH_TEST)

    bgl.glPointSize(p_size)
#    bgl.glBegin(bgl.GL_LINE_LOOP)
    bgl.glBegin(bgl.GL_LINE_STRIP)
    bgl.glColor4f(p_col[0], p_col[1], p_col[2], p_col[3])
 #   bgl.glBegin(bgl.GL_POLYGON)

    for point in points:
        bgl.glVertex3f(point[0], point[1], point[2])

    if x_ray is True:
        bgl.glEnable(bgl.GL_DEPTH_TEST)

    bgl.glEnd()

    # restore opengl defaults
    bgl.glLineWidth(1)
    bgl.glDisable(bgl.GL_BLEND)
    bgl.glColor4f(0.0, 0.0, 0.0, 1.0) 
Example #6
Source File: mi_widget_curve.py    From mifthtools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def draw_2d_point(point_x, point_y, p_size=4, p_col=(1.0,1.0,1.0,1.0)):
    bgl.glEnable(bgl.GL_BLEND)
    #bgl.glColor4f(1.0, 1.0, 1.0, 0.5)
    #bgl.glLineWidth(2)

    bgl.glPointSize(p_size)
    coords = ((point_x, point_y), (point_x, point_y))
    batch = batch_for_shader(shader2d, 'POINTS', {"pos": coords})
    shader2d.bind()
    shader2d.uniform_float("color", (p_col[0], p_col[1], p_col[2], p_col[3]))
    batch.draw(shader2d)
    # bgl.glBegin(bgl.GL_POINTS)
    # bgl.glColor4f(p_col[0], p_col[1], p_col[2], p_col[3])
    # bgl.glVertex2f(point_x, point_y)
    # bgl.glEnd()

    # restore opengl defaults
    bgl.glLineWidth(1)
    bgl.glDisable(bgl.GL_BLEND) 
Example #7
Source File: mi_widget_curve.py    From mifthtools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def draw_2d_point(point_x, point_y, p_size=4, p_col=(1.0,1.0,1.0,1.0)):
    bgl.glEnable(bgl.GL_BLEND)
    #bgl.glColor4f(1.0, 1.0, 1.0, 0.5)
    #bgl.glLineWidth(2)

    bgl.glPointSize(p_size)
#    bgl.glBegin(bgl.GL_LINE_LOOP)
    bgl.glBegin(bgl.GL_POINTS)
 #   bgl.glBegin(bgl.GL_POLYGON)
    bgl.glColor4f(p_col[0], p_col[1], p_col[2], p_col[3])
    bgl.glVertex2f(point_x, point_y)
    bgl.glEnd()

    # restore opengl defaults
    bgl.glLineWidth(1)
    bgl.glDisable(bgl.GL_BLEND)
    bgl.glColor4f(0.0, 0.0, 0.0, 1.0) 
Example #8
Source File: mi_curve_test.py    From mifthtools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def mi_curve_draw_3d_polyline(points, p_size, p_col, x_ray):
    bgl.glEnable(bgl.GL_BLEND)
    bgl.glLineWidth(1)

    if x_ray is True:
        bgl.glDisable(bgl.GL_DEPTH_TEST)

    bgl.glPointSize(p_size)
#    bgl.glBegin(bgl.GL_LINE_LOOP)
    bgl.glBegin(bgl.GL_LINE_STRIP)
    bgl.glColor4f(p_col[0], p_col[1], p_col[2], p_col[3])
 #   bgl.glBegin(bgl.GL_POLYGON)

    for point in points:
        bgl.glVertex3f(point[0], point[1], point[2])

    if x_ray is True:
        bgl.glEnable(bgl.GL_DEPTH_TEST)

    bgl.glEnd()

    # restore opengl defaults
    bgl.glLineWidth(1)
    bgl.glDisable(bgl.GL_BLEND)
    bgl.glColor4f(0.0, 0.0, 0.0, 1.0) 
Example #9
Source File: mi_curve_test.py    From mifthtools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def mi_draw_2d_point(point_x, point_y, p_size=4, p_col=(1.0,1.0,1.0,1.0)):
    bgl.glEnable(bgl.GL_BLEND)
    #bgl.glColor4f(1.0, 1.0, 1.0, 0.5)
    #bgl.glLineWidth(2)

    bgl.glPointSize(p_size)
#    bgl.glBegin(bgl.GL_LINE_LOOP)
    bgl.glBegin(bgl.GL_POINTS)
 #   bgl.glBegin(bgl.GL_POLYGON)
    bgl.glColor4f(p_col[0], p_col[1], p_col[2], p_col[3])
    bgl.glVertex2f(point_x, point_y)
    bgl.glEnd()

    # restore opengl defaults
    bgl.glLineWidth(1)
    bgl.glDisable(bgl.GL_BLEND)
    bgl.glColor4f(0.0, 0.0, 0.0, 1.0)


# TODO MOVE TO UTILITIES 
Example #10
Source File: view3d_ops.py    From BlenderPro with GNU General Public License v3.0 5 votes vote down vote up
def draw_opengl(self,context):     
        region = self._window_region(context)
        
        if self.placed_first_point:
            help_text = "Command Help:\nLEFT CLICK: Place Second Point\nRIGHT CLICK: Cancel Command"
        else:
            help_text = "Command Help:\nLEFT CLICK: Place First Point\nRIGHT CLICK: Cancel Command"
        
        if self.found_snap_point:
            help_text += "\n SNAP TO VERTEX"
        
        help_box = TextBox(
            x=0,y=0,
            width=500,height=0,
            border=10,margin=100,
            message=help_text)
        help_box.x = (self.mouse_x + (help_box.width) / 2 + 10) - region.x
        help_box.y = (self.mouse_y - 10) - region.y

        help_box.draw()
        
        # SNAP POINT
        bgl.glPushAttrib(bgl.GL_ENABLE_BIT)
     
        bgl.glColor4f(255, 0.0, 0.0, 1.0)
        bgl.glEnable(bgl.GL_BLEND)
         
        bgl.glPointSize(10)
        bgl.glBegin(bgl.GL_POINTS)
     
        if self.snapping_point_2d:
            bgl.glVertex2f(self.snapping_point_2d[0], self.snapping_point_2d[1])
     
        bgl.glEnd()
        bgl.glPopAttrib()
     
        # restore opengl defaults
        bgl.glDisable(bgl.GL_BLEND)
        bgl.glColor4f(0.0, 0.0, 0.0, 1.0) 
Example #11
Source File: skeleton_ui_draw.py    From object_alignment with GNU General Public License v3.0 5 votes vote down vote up
def draw3d_points(points, color, size, view_loc, view_ortho, zfar=0.997):
    if not points: return
    bgl.glColor4f(*color)
    bgl.glPointSize(size)
    set_depthrange(0.0, zfar, points, view_loc, view_ortho)
    bgl.glBegin(bgl.GL_POINTS)
    for coord in points: bgl.glVertex3f(*coord)
    bgl.glEnd()
    bgl.glPointSize(1.0)

# draws polylines. 
Example #12
Source File: ogl_velocitiesrenderer.py    From BAddons with GNU General Public License v3.0 5 votes vote down vote up
def drawCallback(self):
        mat = self.interfacer.getBlenderObject().matrix_world
        if self.do_draw and self.interfacer.isClean():
            # isClean() is not really needed here, but it shows its
            # behavior and helps reducing (a tiny bit!) the run time...
            bgl.glColor3f(0.0, 1.0, 1.0)
            bgl.glPointSize(5)
            bgl.glBegin(bgl.GL_POINTS)
            for v in self.vertices:
                bgl.glVertex3f(*(mat*v))
            bgl.glEnd()
            bgl.glPointSize(1)

            bgl.glColor3f(1.0, 1.0, 0.0)
            bgl.glLineWidth(2)
            bgl.glBegin(bgl.GL_LINES)
            for v, w in zip(self.vertices, self.velocities):
                bgl.glVertex3f(*(mat*v))
                bgl.glVertex3f(*(mat*(v + w)))
            bgl.glEnd()
            bgl.glLineWidth(1)


# this in toplevel is also requested
# MAIN_CLASS should be set to any callable returning the
# requested instance, which provides the update() method. 
Example #13
Source File: mi_widget_curve.py    From mifthtools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def draw_3d_polyline(points, p_size, p_col, x_ray):
    bgl.glEnable(bgl.GL_BLEND)
    bgl.glLineWidth(1)

    if x_ray is True:
        bgl.glDisable(bgl.GL_DEPTH_TEST)

    bgl.glPointSize(p_size)

    coords = [(point[0], point[1], point[2]) for point in points]
    batch = batch_for_shader(shader3d, 'LINE_STRIP', {"pos": coords})
    shader3d.bind()
    shader3d.uniform_float("color", (p_col[0], p_col[1], p_col[2], p_col[3]))
    batch.draw(shader3d)

    # bgl.glBegin(bgl.GL_LINE_STRIP)
    # bgl.glColor4f(p_col[0], p_col[1], p_col[2], p_col[3])
    # for point in points:
    #     bgl.glVertex3f(point[0], point[1], point[2])

    if x_ray is True:
        bgl.glEnable(bgl.GL_DEPTH_TEST)

    # bgl.glEnd()

    # restore opengl defaults
    bgl.glLineWidth(1)
    bgl.glDisable(bgl.GL_BLEND) 
Example #14
Source File: mi_curve_test.py    From mifthtools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def draw_callback_px_3d(self, context):

    # 50% alpha, 2 pixel width line
    bgl.glEnable(bgl.GL_BLEND)
    bgl.glColor4f(1.0, 1.0, 1.0, 0.5)
    bgl.glLineWidth(2)

   # bgl.glBegin(bgl.GL_LINE_STRIP)
   # bgl.glVertex3f(*ob.matrix_world.translation)
   # bgl.glVertex3f(*context.scene.cursor_location)
   # bgl.glEnd()

    bgl.glBegin(bgl.GL_POLYGON)
    #bgl.glColor4f(0.0, 0.0, 0.0, 0.5)
    bgl.glVertex3f(0.0, 0.0, 0.0)
    bgl.glVertex3f(0.0, 1.0, 0.0)
    bgl.glVertex3f(1.0, 1.0, 0.0)
    bgl.glVertex3f(1.0, 0.0, 0.0)
    bgl.glEnd()

    ##bgl.glEnable(bgl.GL_BLEND)
    ##bgl.glLineWidth(1.5)
    #bgl.glPointSize(4)
##    bgl.glBegin(bgl.GL_LINE_LOOP)
    #bgl.glBegin(bgl.GL_POINTS)
 ##   bgl.glBegin(bgl.GL_POLYGON)
    #bgl.glColor4f(0.5,1.1,1.0,0.5)
    #bgl.glVertex2f(10, 20)
    #bgl.glVertex2f(50,60)
    #bgl.glVertex2f(700,80)
    #bgl.glVertex2f(2,180)
    #bgl.glEnd()

    # restore opengl defaults
    bgl.glLineWidth(1)
    bgl.glDisable(bgl.GL_BLEND)
    bgl.glColor4f(0.0, 0.0, 0.0, 1.0) 
Example #15
Source File: mi_curve_test.py    From mifthtools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def draw_callback_px_2d(self, context):

    # 50% alpha, 2 pixel width line
    bgl.glEnable(bgl.GL_BLEND)
    bgl.glColor4f(1.0, 1.0, 1.0, 0.5)
    bgl.glLineWidth(2)

   # bgl.glBegin(bgl.GL_LINE_STRIP)
   # bgl.glVertex3f(*ob.matrix_world.translation)
   # bgl.glVertex3f(*context.scene.cursor_location)
   # bgl.glEnd()

    #bgl.glBegin(bgl.GL_POLYGON)
    ##bgl.glColor4f(0.0, 0.0, 0.0, 0.5)
    #bgl.glVertex3f(0.0, 0.0, 0.0)
    #bgl.glVertex3f(0.0, 1.0, 0.0)
    #bgl.glVertex3f(1.0, 1.0, 0.0)
    #bgl.glVertex3f(1.0, 0.0, 0.0)
    #bgl.glEnd()

    #bgl.glEnable(bgl.GL_BLEND)
    #bgl.glLineWidth(1.5)
    bgl.glPointSize(4)
#    bgl.glBegin(bgl.GL_LINE_LOOP)
    bgl.glBegin(bgl.GL_POINTS)
 #   bgl.glBegin(bgl.GL_POLYGON)
    bgl.glColor4f(0.5,1.1,1.0,0.5)
    bgl.glVertex2f(10, 20)
    bgl.glVertex2f(50,60)
    bgl.glVertex2f(700,80)
    bgl.glVertex2f(2,180)
    bgl.glEnd()

    # restore opengl defaults
    bgl.glLineWidth(1)
    bgl.glDisable(bgl.GL_BLEND)
    bgl.glColor4f(0.0, 0.0, 0.0, 1.0) 
Example #16
Source File: mi_curve_test.py    From mifthtools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def draw_callback_px_3d(self, context):

    # 50% alpha, 2 pixel width line
    bgl.glEnable(bgl.GL_BLEND)
    bgl.glColor4f(1.0, 1.0, 1.0, 0.5)
    bgl.glLineWidth(2)

   # bgl.glBegin(bgl.GL_LINE_STRIP)
   # bgl.glVertex3f(*ob.matrix_world.translation)
   # bgl.glVertex3f(*context.scene.cursor_location)
   # bgl.glEnd()

    bgl.glBegin(bgl.GL_POLYGON)
    #bgl.glColor4f(0.0, 0.0, 0.0, 0.5)
    bgl.glVertex3f(0.0, 0.0, 0.0)
    bgl.glVertex3f(0.0, 1.0, 0.0)
    bgl.glVertex3f(1.0, 1.0, 0.0)
    bgl.glVertex3f(1.0, 0.0, 0.0)
    bgl.glEnd()

    ##bgl.glEnable(bgl.GL_BLEND)
    ##bgl.glLineWidth(1.5)
    #bgl.glPointSize(4)
##    bgl.glBegin(bgl.GL_LINE_LOOP)
    #bgl.glBegin(bgl.GL_POINTS)
 ##   bgl.glBegin(bgl.GL_POLYGON)
    #bgl.glColor4f(0.5,1.1,1.0,0.5)
    #bgl.glVertex2f(10, 20)
    #bgl.glVertex2f(50,60)
    #bgl.glVertex2f(700,80)
    #bgl.glVertex2f(2,180)
    #bgl.glEnd()

    # restore opengl defaults
    bgl.glLineWidth(1)
    bgl.glDisable(bgl.GL_BLEND)
    bgl.glColor4f(0.0, 0.0, 0.0, 1.0) 
Example #17
Source File: drawing.py    From addon_common with GNU General Public License v3.0 5 votes vote down vote up
def point_size(self, size): bgl.glPointSize(max(1, self.scale(size))) 
Example #18
Source File: view3d_ops.py    From BlenderPro with GNU General Public License v3.0 5 votes vote down vote up
def draw_opengl(self,context):     
        region = self._window_region(context)
        
        if self.placed_first_point:
            help_text = "Command Help:\nLEFT CLICK: Place Second Point\nRIGHT CLICK: Cancel Command"
        else:
            help_text = "Command Help:\nLEFT CLICK: Place First Point\nRIGHT CLICK: Cancel Command"
        
        if self.found_snap_point:
            help_text += "\n SNAP TO VERTEX"
        
        help_box = TextBox(
            x=0,y=0,
            width=500,height=0,
            border=10,margin=100,
            message=help_text)
        help_box.x = (self.mouse_x + (help_box.width) / 2 + 10) - region.x
        help_box.y = (self.mouse_y - 10) - region.y
        
        help_box.draw()
        
        # SNAP POINT
        bgl.glPushAttrib(bgl.GL_ENABLE_BIT)
     
        bgl.glColor4f(255, 0.0, 0.0, 1.0)
        bgl.glEnable(bgl.GL_BLEND)
         
        bgl.glPointSize(10)
        bgl.glBegin(bgl.GL_POINTS)
     
        if self.snapping_point_2d:
            bgl.glVertex2f(self.snapping_point_2d[0], self.snapping_point_2d[1])
     
        bgl.glEnd()
        bgl.glPopAttrib()
     
        # restore opengl defaults
        bgl.glDisable(bgl.GL_BLEND)
        bgl.glColor4f(0.0, 0.0, 0.0, 1.0) 
Example #19
Source File: view3d_ops.py    From BlenderPro with GNU General Public License v3.0 5 votes vote down vote up
def draw_opengl(self,context):     
        region = self._window_region(context)
        
        if self.placed_first_point:
            help_text = "Command Help:\nLEFT CLICK: Place Second Point\nRIGHT CLICK: Cancel Command"
        else:
            help_text = "Command Help:\nLEFT CLICK: Place First Point\nRIGHT CLICK: Cancel Command"
        
        if self.found_snap_point:
            help_text += "\n SNAP TO VERTEX"
        
        help_box = TextBox(
            x=0,y=0,
            width=500,height=0,
            border=10,margin=100,
            message=help_text)
        help_box.x = (self.mouse_x + (help_box.width) / 2 + 10) - region.x
        help_box.y = (self.mouse_y - 10) - region.y
        
        help_box.draw()
        
        # SNAP POINT
        bgl.glPushAttrib(bgl.GL_ENABLE_BIT)
     
        bgl.glColor4f(255, 0.0, 0.0, 1.0)
        bgl.glEnable(bgl.GL_BLEND)
         
        bgl.glPointSize(10)
        bgl.glBegin(bgl.GL_POINTS)
     
        if self.snapping_point_2d:
            bgl.glVertex2f(self.snapping_point_2d[0], self.snapping_point_2d[1])
     
        bgl.glEnd()
        bgl.glPopAttrib()
     
        # restore opengl defaults
        bgl.glDisable(bgl.GL_BLEND)
        bgl.glColor4f(0.0, 0.0, 0.0, 1.0) 
Example #20
Source File: edit_mesh.py    From coa_tools with GNU General Public License v3.0 5 votes vote down vote up
def draw_circle(self,pos,color,size=8):
        pos = pos + Vector((0,-.1,0))
        bgl.glColor4f(color[0], color[1], color[2], 1.0)
        bgl.glPointSize(size)
        bgl.glBegin(bgl.GL_POINTS)
        bgl.glVertex3f(pos[0],pos[1],pos[2])
        bgl.glEnd() 
Example #21
Source File: mi_linear_widget.py    From mifthtools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def draw_lw(context, lw, cross_up_dir, draw_faloff):
    region = context.region
    rv3d = context.region_data

    start_2d = view3d_utils.location_3d_to_region_2d(region, rv3d, lw.start_point.position)
    end_2d = view3d_utils.location_3d_to_region_2d(region, rv3d, lw.end_point.position)
    middle_2d = view3d_utils.location_3d_to_region_2d(region, rv3d, lw.middle_point.position)

    dist_ends = ((lw.start_point.position - lw.end_point.position).length * 0.1) * cross_up_dir
    end_p1 = view3d_utils.location_3d_to_region_2d(region, rv3d, lw.end_point.position + dist_ends)
    end_p2 = view3d_utils.location_3d_to_region_2d(region, rv3d, lw.end_point.position - dist_ends)

    if start_2d and end_2d and end_p1 and end_p2:
        bgl.glEnable(bgl.GL_BLEND)
        bgl.glLineWidth(1)
        bgl.glPointSize(6)

        bgl.glBegin(bgl.GL_LINE_STRIP)
        bgl.glColor4f(0.99, 0.5, 0.99, 1.0)
        bgl.glVertex2f(start_2d[0], start_2d[1])
        bgl.glVertex2f(end_2d[0], end_2d[1])
        bgl.glEnd()

        if draw_faloff:
            bgl.glBegin(bgl.GL_LINE_LOOP)
            bgl.glColor4f(0.99, 0.5, 0.99, 1.0)
            bgl.glVertex2f(start_2d[0], start_2d[1])
            bgl.glVertex2f(end_p1[0], end_p1[1])
            bgl.glVertex2f(end_p2[0], end_p2[1])
            bgl.glEnd()

        bgl.glBegin(bgl.GL_POINTS)
     #   bgl.glBegin(bgl.GL_POLYGON)
        bgl.glColor4f(0.99, 0.8, 0.5, 1.0)
        bgl.glVertex2f(start_2d[0], start_2d[1])
        bgl.glVertex2f(middle_2d[0], middle_2d[1])
        bgl.glVertex2f(end_2d[0], end_2d[1])
        bgl.glEnd()

        # restore opengl defaults
        bgl.glLineWidth(1)
        bgl.glDisable(bgl.GL_BLEND)
        bgl.glColor4f(0.0, 0.0, 0.0, 1.0) 
Example #22
Source File: mi_widget_linear_deform.py    From mifthtools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def draw_lw(context, lw, cross_up_dir, draw_faloff):
    region = context.region
    rv3d = context.region_data

    start_2d = view3d_utils.location_3d_to_region_2d(region, rv3d, lw.start_point.position)
    end_2d = view3d_utils.location_3d_to_region_2d(region, rv3d, lw.end_point.position)
    middle_2d = view3d_utils.location_3d_to_region_2d(region, rv3d, lw.middle_point.position)

    dist_ends = ((lw.start_point.position - lw.end_point.position).length * 0.1) * cross_up_dir
    end_p1 = view3d_utils.location_3d_to_region_2d(region, rv3d, lw.end_point.position + dist_ends)
    end_p2 = view3d_utils.location_3d_to_region_2d(region, rv3d, lw.end_point.position - dist_ends)

    if start_2d and end_2d and end_p1 and end_p2:
        bgl.glEnable(bgl.GL_BLEND)
        bgl.glLineWidth(1)
        bgl.glPointSize(6)

        bgl.glBegin(bgl.GL_LINE_STRIP)
        bgl.glColor4f(0.99, 0.5, 0.99, 1.0)
        bgl.glVertex2f(start_2d[0], start_2d[1])
        bgl.glVertex2f(end_2d[0], end_2d[1])
        bgl.glEnd()

        if draw_faloff:
            bgl.glBegin(bgl.GL_LINE_LOOP)
            bgl.glColor4f(0.99, 0.5, 0.99, 1.0)
            bgl.glVertex2f(start_2d[0], start_2d[1])
            bgl.glVertex2f(end_p1[0], end_p1[1])
            bgl.glVertex2f(end_p2[0], end_p2[1])
            bgl.glEnd()

        bgl.glBegin(bgl.GL_POINTS)
     #   bgl.glBegin(bgl.GL_POLYGON)
        bgl.glColor4f(0.99, 0.8, 0.5, 1.0)
        bgl.glVertex2f(start_2d[0], start_2d[1])
        bgl.glVertex2f(middle_2d[0], middle_2d[1])
        bgl.glVertex2f(end_2d[0], end_2d[1])
        bgl.glEnd()

        # restore opengl defaults
        bgl.glLineWidth(1)
        bgl.glDisable(bgl.GL_BLEND)
        bgl.glColor4f(0.0, 0.0, 0.0, 1.0) 
Example #23
Source File: mi_widget_linear_deform.py    From mifthtools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def draw_lw(context, lw, cross_up_dir, draw_faloff):
    region = context.region
    rv3d = context.region_data

    start_2d = view3d_utils.location_3d_to_region_2d(region, rv3d, lw.start_point.position)
    end_2d = view3d_utils.location_3d_to_region_2d(region, rv3d, lw.end_point.position)
    middle_2d = view3d_utils.location_3d_to_region_2d(region, rv3d, lw.middle_point.position)

    dist_ends = ((lw.start_point.position - lw.end_point.position).length * 0.1) * cross_up_dir
    end_p1 = view3d_utils.location_3d_to_region_2d(region, rv3d, lw.end_point.position + dist_ends)
    end_p2 = view3d_utils.location_3d_to_region_2d(region, rv3d, lw.end_point.position - dist_ends)

    if start_2d and end_2d and end_p1 and end_p2:
        bgl.glEnable(bgl.GL_BLEND)
        bgl.glLineWidth(1)
        bgl.glPointSize(6)

        coords = ((start_2d[0], start_2d[1]), (end_2d[0], end_2d[1]))
        batch = batch_for_shader(shader2d, 'LINE_STRIP', {"pos": coords})
        shader2d.bind()
        shader2d.uniform_float("color", (0.99, 0.5, 0.99, 1.0))
        batch.draw(shader2d)
        # bgl.glBegin(bgl.GL_LINE_STRIP)
        # bgl.glColor4f(0.99, 0.5, 0.99, 1.0)
        # bgl.glVertex2f(start_2d[0], start_2d[1])
        # bgl.glVertex2f(end_2d[0], end_2d[1])
        # bgl.glEnd()

        if draw_faloff:
            coords = ((start_2d[0], start_2d[1]), (end_p1[0], end_p1[1]), (end_p2[0], end_p2[1]))
            batch = batch_for_shader(shader2d, 'LINE_LOOP', {"pos": coords})
            shader2d.bind()
            shader2d.uniform_float("color", (0.99, 0.5, 0.99, 1.0))
            batch.draw(shader2d)
            # bgl.glBegin(bgl.GL_LINE_LOOP)
            # bgl.glVertex2f(start_2d[0], start_2d[1])
            # bgl.glVertex2f(end_p1[0], end_p1[1])
            # bgl.glVertex2f(end_p2[0], end_p2[1])
            # bgl.glEnd()

        coords = ((start_2d[0], start_2d[1]), (middle_2d[0], middle_2d[1]), (end_2d[0], end_2d[1]))
        batch = batch_for_shader(shader2d, 'POINTS', {"pos": coords})
        shader2d.bind()
        shader2d.uniform_float("color", (0.99, 0.8, 0.5, 1.0))
        batch.draw(shader2d)

        # bgl.glBegin(bgl.GL_POINTS)
        # bgl.glColor4f(0.99, 0.8, 0.5, 1.0)
        # bgl.glVertex2f(start_2d[0], start_2d[1])
        # bgl.glVertex2f(middle_2d[0], middle_2d[1])
        # bgl.glVertex2f(end_2d[0], end_2d[1])
        # bgl.glEnd()

        # restore opengl defaults
        bgl.glLineWidth(1)
        bgl.glDisable(bgl.GL_BLEND)
        # bgl.glColor4f(0.0, 0.0, 0.0, 1.0) 
Example #24
Source File: opengl_utils.py    From Blender-Addon-Photogrammetry-Importer with MIT License 4 votes vote down vote up
def render_opengl_image(image_name, cam, point_size):
    draw_manager = DrawManager.get_singleton()
    coords, colors = draw_manager.get_coords_and_colors()

    scene = bpy.context.scene
    render = bpy.context.scene.render

    width = render.resolution_x
    height = render.resolution_y
    # TODO Provide an option to render from the 3D view perspective
    # width = bpy.context.region.width
    # height = bpy.context.region.height

    offscreen = gpu.types.GPUOffScreen(width, height)
    with offscreen.bind():

        bgl.glPointSize(point_size)
        #bgl.glClear(bgl.GL_COLOR_BUFFER_BIT)
        #bgl.glClear(bgl.GL_DEPTH_BUFFER_BIT)

        view_matrix = cam.matrix_world.inverted()
        projection_matrix = cam.calc_matrix_camera(
            bpy.context.evaluated_depsgraph_get(), 
            x=width,
            y=height)
        perspective_matrix = projection_matrix @ view_matrix

        gpu.matrix.load_matrix(perspective_matrix)
        gpu.matrix.load_projection_matrix(Matrix.Identity(4))
        
        shader = gpu.shader.from_builtin('3D_FLAT_COLOR')
        shader.bind()
        batch = batch_for_shader(shader, "POINTS", {"pos": coords, "color": colors})
        batch.draw(shader)
        
        buffer = bgl.Buffer(bgl.GL_BYTE, width * height * 4)
        bgl.glReadPixels(0, 0, width, height, bgl.GL_RGBA, bgl.GL_UNSIGNED_BYTE, buffer)

    offscreen.free()

    image = create_image_lazy(image_name, width, height)
    copy_buffer_to_pixel(buffer, image) 
Example #25
Source File: draw_manager.py    From Blender-Addon-Photogrammetry-Importer with MIT License 4 votes vote down vote up
def draw_points_callback(self, draw_manager, object_anchor, positions, colors):

        handle_is_valid = True
        try:
            # Check if object still exists
            object_anchor_name = object_anchor.name
        except:
            handle_is_valid = False

        if handle_is_valid:
            if object_anchor_name in bpy.data.objects:

                # Use the visibility of the object to enable / 
                # disable the drawing of the point cloud
                if bpy.data.objects[object_anchor_name].visible_get():

                    # Update the batch depending on the anchor pose (only if necessary)
                    object_anchor_has_changed = not np.array_equal(
                        self.object_anchor_pose_previous, object_anchor.matrix_world)
                    if self.batch_cached is None or object_anchor_has_changed:
                        
                        self.object_anchor_pose_previous = np.copy(object_anchor.matrix_world)
                        transf_pos_list = compute_transformed_coords(
                            object_anchor.matrix_world, positions)

                        self.batch_cached = batch_for_shader(
                            self.shader, "POINTS", {"pos": transf_pos_list, "color": colors})

                    self.shader.bind()
                    bgl.glPointSize(self.point_size)
                    bgl.glEnable(bgl.GL_DEPTH_TEST)
                    bgl.glDepthMask(bgl.GL_TRUE)

                    self.batch_cached.draw(self.shader)

        else:
            log_report('INFO', 'Removing draw handler of deleted point cloud handle')
            if self.draw_handler_handle is not None:
                bpy.types.SpaceView3D.draw_handler_remove(
                    self.draw_handler_handle, 'WINDOW')
                self.draw_handler_handle = None
                draw_manager.delete_anchor(object_anchor) 
Example #26
Source File: primitive.py    From BlenderTools with GNU General Public License v2.0 4 votes vote down vote up
def __init__(self, buffer_type, draw_size, shader_type, attr_names):
        """Create buffer instance with given type drawing size and shader type.

        :param buffer_type: type of the buffer from _Buffer.Types
        :type buffer_type: int
        :param draw_size: size of the drawing elements
        :type draw_size: float
        :param shader_type: type of the shader for given buffer from ShaderTypes
        :type shader_type: int
        :param attr_names: tuple of string defining attributes that this buffer is holding
        :type attr_names: tuple[str]
        """
        if buffer_type not in {_Buffer.Types.LINES, _Buffer.Types.POINTS, _Buffer.Types.TRIS}:
            raise TypeError("Unsupported buffer type requested: %s!" % buffer_type)

        self.__type = buffer_type
        self.__draw_size = draw_size
        self.__shader = get_shader(shader_type)
        self.__data = {}

        for att_name in attr_names:
            self.__data[att_name] = []

        # depending on type  setup callbacks executed before and after dispatching
        if buffer_type == _Buffer.Types.LINES:

            self.__bgl_callback = bgl.glLineWidth
            self.__bgl_callback_param_before = self.__draw_size
            self.__bgl_callback_param_after = 1.0
            self.__draw_type = 'LINES'

        elif buffer_type == _Buffer.Types.POINTS:

            self.__bgl_callback = bgl.glPointSize
            self.__bgl_callback_param_before = self.__draw_size
            self.__bgl_callback_param_after = 1.0
            self.__draw_type = 'POINTS'

        elif buffer_type == _Buffer.Types.TRIS:

            self.__bgl_callback = lambda *args: None
            self.__bgl_callback_param_before = None
            self.__bgl_callback_param_after = None
            self.__draw_type = 'TRIS'