Example #1
0
def draw_callback_px(self, context):
    
    #get RegionView3D
    r3d = 0
    for space in context.area.spaces:
        if space.type == 'VIEW_3D':
            r3d = space.region_3d
    
    if r3d == 0:
        print("region_3D NOT FOUND")
        pass
    
    screenWidth = context.area.width
    screenHeight = context.area.height
    
    bgl.glEnable(bgl.GL_BLEND)
    bgl.glEnable(bgl.GL_DEPTH_TEST)
    
    # get old viewport properties
    oldViewport = bgl.Buffer(bgl.GL_INT, 4)
    bgl.glGetIntegerv(bgl.GL_VIEWPORT, oldViewport) 
    
    #oldViewDistance = r3d.view_distance
    
    oldMatrix = bgl.Buffer(bgl.GL_DOUBLE, [4,4])
    bgl.glGetDoublev(bgl.GL_PROJECTION_MATRIX, oldMatrix)
    
    viewportWidth = int(oldViewport[2]/4)
    viewportHeight = int(oldViewport[3]/4)
    
    #bgl.glViewport(screenWidth - viewportWidth, screenHeight - viewportHeight, viewportWidth, viewportHeight)
    bgl.glViewport(screenWidth - viewportWidth, (screenHeight - viewportHeight) + int((viewportHeight-viewportWidth)/2), viewportWidth, viewportHeight)
    
    draw_cube(context, r3d)

    # restore opengl defaults
    #r3d.view_distance = oldViewDistance
    bgl.glLineWidth(1)
    bgl.glDisable(bgl.GL_BLEND)
    bgl.glDisable(bgl.GL_DEPTH_TEST)
    bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
    bgl.glViewport(oldViewport[0],oldViewport[1],oldViewport[2],oldViewport[3])
    
    bgl.glPushMatrix()
    bgl.glLoadMatrixf(oldMatrix)
    bgl.glPopMatrix()
Example #2
0
    def _setModelViewMatrix(self, camera, matrix):
        from bgl import (
                Buffer,
                GL_FLOAT,
                GL_MODELVIEW,
                glMatrixMode,
                glLoadMatrixf,
                )

        matrix *= camera.modelview_matrix
        buf = Buffer(GL_FLOAT, [4, 4])
        for i in range(4):
            for j in range(4):
                # transposed
                buf[i][j] = matrix[j][i]

        glMatrixMode(GL_MODELVIEW)
        glLoadMatrixf(buf)
def draw_measurements_callback(self, context):
    sce = context.scene

    draw = 0
    if hasattr(sce, "measure_panel_draw"):
        draw = sce.measure_panel_draw

    # 2D drawing code example
    #bgl.glBegin(bgl.GL_LINE_STRIP)
    #bgl.glVertex2i(0, 0)
    #bgl.glVertex2i(80, 100)
    #bgl.glEnd()

    # Get measured 3D points and colors.
    line = getMeasurePoints(context)
    if (line and draw):
        p1, p2, color = line

        # Get and convert the Perspective Matrix of the current view/region.
        view3d = bpy.context
        region = view3d.region_data
        perspMatrix = region.perspective_matrix
        tempMat = [perspMatrix[i][j] for i in range(4) for j in range(4)]
        perspBuff = bgl.Buffer(bgl.GL_FLOAT, 16, tempMat)

        # ---
        # Store previous OpenGL settings.
        # Store MatrixMode
        MatrixMode_prev = bgl.Buffer(bgl.GL_INT, [1])
        bgl.glGetIntegerv(bgl.GL_MATRIX_MODE, MatrixMode_prev)
        MatrixMode_prev = MatrixMode_prev[0]

        # Store projection matrix
        ProjMatrix_prev = bgl.Buffer(bgl.GL_DOUBLE, [16])
        bgl.glGetFloatv(bgl.GL_PROJECTION_MATRIX, ProjMatrix_prev)

        # Store Line width
        lineWidth_prev = bgl.Buffer(bgl.GL_FLOAT, [1])
        bgl.glGetFloatv(bgl.GL_LINE_WIDTH, lineWidth_prev)
        lineWidth_prev = lineWidth_prev[0]

        # Store GL_BLEND
        blend_prev = bgl.Buffer(bgl.GL_BYTE, [1])
        bgl.glGetFloatv(bgl.GL_BLEND, blend_prev)
        blend_prev = blend_prev[0]

        line_stipple_prev = bgl.Buffer(bgl.GL_BYTE, [1])
        bgl.glGetFloatv(bgl.GL_LINE_STIPPLE, line_stipple_prev)
        line_stipple_prev = line_stipple_prev[0]

        # Store glColor4f
        color_prev = bgl.Buffer(bgl.GL_FLOAT, [4])
        bgl.glGetFloatv(bgl.GL_COLOR, color_prev)

        # ---
        # Prepare for 3D drawing
        bgl.glLoadIdentity()
        bgl.glMatrixMode(bgl.GL_PROJECTION)
        bgl.glLoadMatrixf(perspBuff)

        bgl.glEnable(bgl.GL_BLEND)
        bgl.glEnable(bgl.GL_LINE_STIPPLE)

        # ---
        # Draw 3D stuff.
        width = 1
        bgl.glLineWidth(width)
        # X
        bgl.glColor4f(1, 0, 0, 0.8)
        bgl.glBegin(bgl.GL_LINE_STRIP)
        bgl.glVertex3f(p1[0], p1[1], p1[2])
        bgl.glVertex3f(p2[0], p1[1], p1[2])
        bgl.glEnd()
        # Y
        bgl.glColor4f(0, 1, 0, 0.8)
        bgl.glBegin(bgl.GL_LINE_STRIP)
        bgl.glVertex3f(p1[0], p1[1], p1[2])
        bgl.glVertex3f(p1[0], p2[1], p1[2])
        bgl.glEnd()
        # Z
        bgl.glColor4f(0, 0, 1, 0.8)
        bgl.glBegin(bgl.GL_LINE_STRIP)
        bgl.glVertex3f(p1[0], p1[1], p1[2])
        bgl.glVertex3f(p1[0], p1[1], p2[2])
        bgl.glEnd()

        # Dist
        width = 2
        bgl.glLineWidth(width)
        bgl.glColor4f(color[0], color[1], color[2], color[3])
        bgl.glBegin(bgl.GL_LINE_STRIP)
        bgl.glVertex3f(p1[0], p1[1], p1[2])
        bgl.glVertex3f(p2[0], p2[1], p2[2])
        bgl.glEnd()

        # ---
        # Restore previous OpenGL settings
        bgl.glLoadIdentity()
        bgl.glMatrixMode(MatrixMode_prev)
        bgl.glLoadMatrixf(ProjMatrix_prev)
        bgl.glLineWidth(lineWidth_prev)
        if not blend_prev:
            bgl.glDisable(bgl.GL_BLEND)
        if not line_stipple_prev:
            bgl.glDisable(bgl.GL_LINE_STIPPLE)
        bgl.glColor4f(color_prev[0],
            color_prev[1],
            color_prev[2],
            color_prev[3])

        # ---
        # Draw (2D) text
        # We do this after drawing the lines so
        # we can draw it OVER the line.
        coord_2d = location_3d_to_region_2d(context.region,
                                            context.space_data.region_3d,
                                            p1.lerp(p2, 0.5),
                                            )
        OFFSET_LINE = 10   # Offset the text a bit to the right.
        OFFSET_Y = 15      # Offset of the lines.
        OFFSET_VALUE = 30  # Offset of value(s) from the text.
        dist = (p1 - p2).length

        # Write distance value into the scene property,
        # so we can display it in the panel & refresh the panel.
        if hasattr(sce, "measure_panel_dist"):
            sce.measure_panel_dist = dist
            context.area.tag_redraw()

        texts = [("Dist:", round(dist, PRECISION)),
            ("X:", round(abs(p1[0] - p2[0]), PRECISION)),
            ("Y:", round(abs(p1[1] - p2[1]), PRECISION)),
            ("Z:", round(abs(p1[2] - p2[2]), PRECISION))]

        # Draw all texts
        # @todo Get user pref for text color in 3D View
        bgl.glColor4f(1.0, 1.0, 1.0, 1.0)
        blf.size(0, 12, 72)  # Prevent font size to randomly change.

        loc_x = coord_2d[0] + OFFSET_LINE
        loc_y = coord_2d[1]
        for t in texts:
            text = t[0]
            value = str(t[1]) + " BU"

            blf.position(0, loc_x, loc_y, 0)
            blf.draw(0, text)
            blf.position(0, loc_x + OFFSET_VALUE, loc_y, 0)
            blf.draw(0, value)

            loc_y -= OFFSET_Y

    # Handle mesh surface area calulations
    if (sce.measure_panel_calc_area):
        # Get a single selected object (or nothing).
        obj = getSingleObject(context)

        if (context.mode == 'EDIT_MESH'):
            obj = context.active_object

            if (obj and obj.type == 'MESH' and obj.data):
                # "Note: a Mesh will return the selection state of the mesh
                # when EditMode was last exited. A Python script operating
                # in EditMode must exit EditMode before getting the current
                # selection state of the mesh."
                # http://www.blender.org/documentation/249PythonDoc/
                # /Mesh.MVert-class.html#sel
                # We can only provide this by existing & re-entering EditMode.
                # @todo: Better way to do this?

                # Get mesh data from Object.
                mesh = obj.data

                # Get transformation matrix from object.
                ob_mat = obj.matrix_world
                # Also make an inversed copy! of the matrix.
                ob_mat_inv = ob_mat.copy()
                Matrix.invert(ob_mat_inv)

                # Get the selected vertices.
                # @todo: Better (more efficient) way to do this?
                verts_selected = [v for v in mesh.vertices if v.select == 1]

                if len(verts_selected) >= 3:
                    # Get selected faces
                    # @todo: Better (more efficient) way to do this?
                    faces_selected = [f for f in mesh.faces
                        if f.select == 1]

                    if len(faces_selected) > 0:
                        area, normal = objectSurfaceArea(obj, True,
                            measureGlobal(sce))
                        if (area >= 0):
                            sce.measure_panel_area1 = area
                            sce.measure_panel_normal1 = normal

        elif (context.mode == 'OBJECT'):
            # We are working in object mode.

            if len(context.selected_objects) > 2:
                return
# @todo Make this work again.
#                # We have more that 2 objects selected...
#
#                mesh_objects = [o for o in context.selected_objects
#                    if (o.type == 'MESH')]

#                if (len(mesh_objects) > 0):
#                    # ... and at least one of them is a mesh.
#
#                    for o in mesh_objects:
#                        area = objectSurfaceArea(o, False,
#                            measureGlobal(sce))
#                        if (area >= 0):
#                            #row.label(text=o.name, icon='OBJECT_DATA')
#                            #row.label(text=str(round(area, PRECISION))
#                            #    + " BU^2")

            elif len(context.selected_objects) == 2:
                # 2 objects selected.

                obj1, obj2 = context.selected_objects

                # Calculate surface area of the objects.
                area1, normal1 = objectSurfaceArea(obj1, False,
                    measureGlobal(sce))
                area2, normal2 = objectSurfaceArea(obj2, False,
                    measureGlobal(sce))
                sce.measure_panel_area1 = area1
                sce.measure_panel_area2 = area2
                sce.measure_panel_normal1 = normal1
                sce.measure_panel_normal2 = normal2

            elif (obj):
                # One object selected.

                # Calculate surface area of the object.
                area, normal = objectSurfaceArea(obj, False,
                    measureGlobal(sce))

                if (area >= 0):
                    sce.measure_panel_area1 = area
                    sce.measure_panel_normal1 = normal

    if (sce.measure_panel_calc_volume):
        obj = getSingleObject(context)

        if (context.mode == 'OBJECT'):
            # We are working in object mode.

            #if len(context.selected_objects) > 2:       # TODO

            #el
            if len(context.selected_objects) == 2:
                # 2 objects selected.

                obj1, obj2 = context.selected_objects

                # Calculate surface area of the objects.
                volume1 = objectVolume(obj1, measureGlobal(sce))
                volume2 = objectVolume(obj2, measureGlobal(sce))

                sce.measure_panel_volume1 = volume1
                sce.measure_panel_volume2 = volume2

            elif (obj):
                # One object selected.

                # Calculate surface area of the object.
                volume1 = objectVolume(obj, measureGlobal(sce))

                sce.measure_panel_volume1 = volume1
Example #4
0
def DrawNorth_callback(self, context):

    if not Sun.SP.ShowNorth and North.isActive:
        North.deactivate()
        return

    # ------------------------------------------------------------------
    # Set up the compass needle using the current north offset angle
    # less 90 degrees.  This forces the unit circle to begin at the
    # 12 O'clock instead of 3 O'clock position.
    # ------------------------------------------------------------------
    color = (0.2, 0.6, 1.0, 0.7)
    radius = 100
    angle = -(Sun.NorthOffset - math.pi / 2)
    x = math.cos(angle) * radius
    y = math.sin(angle) * radius

    p1, p2 = (0, 0, 0), (x, y, 0)  # Start & end of needle

    #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    # Thanks to Buerbaum Martin for the following which draws openGL
    # lines.  ( From his script space_view3d_panel_measure.py )
    #<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    # ------------------------------------------------------------------
    # Convert the Perspective Matrix of the current view/region.
    # ------------------------------------------------------------------
    view3d = bpy.context
    region = view3d.region_data
    perspMatrix = region.perspective_matrix
    tempMat = [perspMatrix[j][i] for i in range(4) for j in range(4)]
    perspBuff = bgl.Buffer(bgl.GL_FLOAT, 16, tempMat)

    # ---------------------------------------------------------
    # Store previous OpenGL settings.
    # ---------------------------------------------------------
    MatrixMode_prev = bgl.Buffer(bgl.GL_INT, [1])
    bgl.glGetIntegerv(bgl.GL_MATRIX_MODE, MatrixMode_prev)
    MatrixMode_prev = MatrixMode_prev[0]

    # Store projection matrix
    ProjMatrix_prev = bgl.Buffer(bgl.GL_DOUBLE, [16])
    bgl.glGetFloatv(bgl.GL_PROJECTION_MATRIX, ProjMatrix_prev)

    # Store Line width
    lineWidth_prev = bgl.Buffer(bgl.GL_FLOAT, [1])
    bgl.glGetFloatv(bgl.GL_LINE_WIDTH, lineWidth_prev)
    lineWidth_prev = lineWidth_prev[0]

    # Store GL_BLEND
    blend_prev = bgl.Buffer(bgl.GL_BYTE, [1])
    bgl.glGetFloatv(bgl.GL_BLEND, blend_prev)
    blend_prev = blend_prev[0]

    line_stipple_prev = bgl.Buffer(bgl.GL_BYTE, [1])
    bgl.glGetFloatv(bgl.GL_LINE_STIPPLE, line_stipple_prev)
    line_stipple_prev = line_stipple_prev[0]

    # Store glColor4f
    color_prev = bgl.Buffer(bgl.GL_FLOAT, [4])
    bgl.glGetFloatv(bgl.GL_COLOR, color_prev)

    # ---------------------------------------------------------
    # Prepare for 3D drawing
    # ---------------------------------------------------------
    bgl.glLoadIdentity()
    bgl.glMatrixMode(bgl.GL_PROJECTION)
    bgl.glLoadMatrixf(perspBuff)

    bgl.glEnable(bgl.GL_BLEND)
    bgl.glEnable(bgl.GL_LINE_STIPPLE)

    # ------------------
    # and draw the line
    # ------------------
    width = 2
    bgl.glLineWidth(width)
    bgl.glColor4f(color[0], color[1], color[2], color[3])
    bgl.glBegin(bgl.GL_LINE_STRIP)
    bgl.glVertex3f(p1[0], p1[1], p1[2])
    bgl.glVertex3f(p2[0], p2[1], p2[2])
    bgl.glEnd()

    # ---------------------------------------------------------
    # Restore previous OpenGL settings
    # ---------------------------------------------------------
    bgl.glLoadIdentity()
    bgl.glMatrixMode(MatrixMode_prev)
    bgl.glLoadMatrixf(ProjMatrix_prev)
    bgl.glLineWidth(lineWidth_prev)

    if not blend_prev:
        bgl.glDisable(bgl.GL_BLEND)
    if not line_stipple_prev:
        bgl.glDisable(bgl.GL_LINE_STIPPLE)

    bgl.glColor4f(color_prev[0], color_prev[1], color_prev[2], color_prev[3])
Example #5
0
def draw_callback_px(self, context):
    #print("mouse points", len(self.mouse_path))

    #font_id = 0  # XXX, need to find out how best to get this.

    # draw some text
    #blf.position(font_id, 15, 30, 0)
    #blf.size(font_id, 20, 72)
    #blf.draw(font_id, "Hello Word " + str(len(self.mouse_path)))

    image = context.window_manager.love2d3d.image_front  # Image ID
    if image == "":
        return
    self.image = context.blend_data.images[image]  # Get image

    # 50% alpha, 2 pixel width line
    #bgl.glLineWidth(0)
    bgl.glEnable(bgl.GL_BLEND)
    #bgl.glClearColor(0.0, 0.0, 0.0, 0.0);
    #bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
    self.image.gl_load()
    bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.image.bindcode[0])
    bgl.glColor4f(1.0, 1.0, 1.0, 0.5)
    bgl.glEnable(bgl.GL_TEXTURE_2D)
    bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER,
                        bgl.GL_NEAREST)
    bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER,
                        bgl.GL_NEAREST)
    #bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR)
    #bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR)

    #self.image.bind()
    #bgl.glLineWidth(2)

    #    bgl.glBegin(bgl.GL_LINE_STRIP)
    #    for x, y in self.mouse_path:
    #        bgl.glVertex2i(x, y)
    #
    #    bgl.glEnd()
    #camera = context.space_data.camera.location
    #sclip = context.space_data.clip_start
    #eclip = context.space_data.clip_end
    #lens = context.space_data.lens
    #pers = context.region_data.perspective_matrix
    #fovy = math.atan(pers[5]) * 2
    #aspect = pers[5] / pers[0]
    #bgl.gluPerspective(fovy, aspect, sclip, eclip);
    bgl.glMatrixMode(bgl.GL_MODELVIEW)
    #print(context.region_data.view_matrix)
    #ob = context.active_object
    #buff = bgl.Buffer(bgl.GL_FLOAT, [4, 4], context.region_data.view_matrix.transposed())
    #buff = bgl.Buffer(bgl.GL_FLOAT, [4, 4], ob.matrix_world.transposed())
    #mat = Matrix.Identity(4)
    mat = Matrix.Translation(context.space_data.cursor_location)
    view_align = context.window_manager.love2d3d.view_align
    if view_align:
        iview = Matrix(
            context.region_data.view_matrix).inverted_safe().to_3x3().to_4x4()
    else:
        iview = Matrix.Identity(4)
    buff = bgl.Buffer(bgl.GL_FLOAT, [4, 4], (mat * iview).transposed())
    bgl.glLoadMatrixf(buff)
    #bgl.glLoadIdentity()
    #camera = context.region_data.view_location
    #bgl.gluLookAt(camera.x, camera.y, camera.z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
    bgl.glMatrixMode(bgl.GL_PROJECTION)
    #bgl.glLoadIdentity();
    buff = bgl.Buffer(bgl.GL_FLOAT, [4, 4],
                      context.region_data.perspective_matrix.transposed())
    bgl.glLoadMatrixf(buff)

    scale = context.window_manager.love2d3d.scale
    w, h = self.image.size
    w *= scale
    h *= scale
    #lb = location_3d_to_region_2d(context.region, context.space_data.region_3d, (-w / 2.0, 0, -h / 2.0))
    #rb = location_3d_to_region_2d(context.region, context.space_data.region_3d, (w / 2.0, 0, -h / 2.0))
    #rt = location_3d_to_region_2d(context.region, context.space_data.region_3d, (w / 2.0, 0, h / 2.0))
    #lt = location_3d_to_region_2d(context.region, context.space_data.region_3d, (-w / 2.0, 0, h /2.0))
    if view_align:
        lb = Vector((-w / 2.0, -h / 2.0, 0))
        rb = Vector((w / 2.0, -h / 2.0, 0))
        rt = Vector((w / 2.0, h / 2.0, 0))
        lt = Vector((-w / 2.0, h / 2.0, 0))
    else:
        lb = Vector((-w / 2.0, 0, -h / 2.0))
        rb = Vector((w / 2.0, 0, -h / 2.0))
        rt = Vector((w / 2.0, 0, h / 2.0))
        lt = Vector((-w / 2.0, 0, h / 2.0))
    #print(lt)
    bgl.glBegin(bgl.GL_QUADS)
    bgl.glTexCoord2d(0.0, 0.0)
    bgl.glVertex3f(lb.x, lb.y, lb.z)
    bgl.glTexCoord2d(1.0, 0.0)
    bgl.glVertex3f(rb.x, rb.y, lb.z)
    bgl.glTexCoord2d(1.0, 1.0)
    bgl.glVertex3f(rt.x, rt.y, rt.z)
    bgl.glTexCoord2d(0.0, 1.0)
    bgl.glVertex3f(lt.x, lt.y, lt.z)
    bgl.glEnd()
    self.image.gl_free()
    # restore opengl defaults
    bgl.glLineWidth(1)
    bgl.glDisable(bgl.GL_TEXTURE_2D)
    bgl.glDisable(bgl.GL_BLEND)
    bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
def draw_measurements_callback(self, context):
    sce = context.scene

    draw = 0
    if hasattr(sce, "measure_panel_draw"):
        draw = sce.measure_panel_draw

    # 2D drawing code example
    #bgl.glBegin(bgl.GL_LINE_STRIP)
    #bgl.glVertex2i(0, 0)
    #bgl.glVertex2i(80, 100)
    #bgl.glEnd()

    # Get measured 3D points and colors.
    line = getMeasurePoints(context)
    if (line and draw):
        p1, p2, color = line

        # Get and convert the Perspective Matrix of the current view/region.
        view3d = bpy.context
        region = view3d.region_data
        perspMatrix = region.perspective_matrix
        tempMat = [perspMatrix[i][j] for i in range(4) for j in range(4)]
        perspBuff = bgl.Buffer(bgl.GL_FLOAT, 16, tempMat)

        # ---
        # Store previous OpenGL settings.
        # Store MatrixMode
        MatrixMode_prev = bgl.Buffer(bgl.GL_INT, [1])
        bgl.glGetIntegerv(bgl.GL_MATRIX_MODE, MatrixMode_prev)
        MatrixMode_prev = MatrixMode_prev[0]

        # Store projection matrix
        ProjMatrix_prev = bgl.Buffer(bgl.GL_DOUBLE, [16])
        bgl.glGetFloatv(bgl.GL_PROJECTION_MATRIX, ProjMatrix_prev)

        # Store Line width
        lineWidth_prev = bgl.Buffer(bgl.GL_FLOAT, [1])
        bgl.glGetFloatv(bgl.GL_LINE_WIDTH, lineWidth_prev)
        lineWidth_prev = lineWidth_prev[0]

        # Store GL_BLEND
        blend_prev = bgl.Buffer(bgl.GL_BYTE, [1])
        bgl.glGetFloatv(bgl.GL_BLEND, blend_prev)
        blend_prev = blend_prev[0]

        line_stipple_prev = bgl.Buffer(bgl.GL_BYTE, [1])
        bgl.glGetFloatv(bgl.GL_LINE_STIPPLE, line_stipple_prev)
        line_stipple_prev = line_stipple_prev[0]

        # Store glColor4f
        color_prev = bgl.Buffer(bgl.GL_FLOAT, [4])
        bgl.glGetFloatv(bgl.GL_COLOR, color_prev)

        # ---
        # Prepare for 3D drawing
        bgl.glLoadIdentity()
        bgl.glMatrixMode(bgl.GL_PROJECTION)
        bgl.glLoadMatrixf(perspBuff)

        bgl.glEnable(bgl.GL_BLEND)
        bgl.glEnable(bgl.GL_LINE_STIPPLE)

        # ---
        # Draw 3D stuff.
        width = 1
        bgl.glLineWidth(width)
        # X
        bgl.glColor4f(1, 0, 0, 0.8)
        bgl.glBegin(bgl.GL_LINE_STRIP)
        bgl.glVertex3f(p1[0], p1[1], p1[2])
        bgl.glVertex3f(p2[0], p1[1], p1[2])
        bgl.glEnd()
        # Y
        bgl.glColor4f(0, 1, 0, 0.8)
        bgl.glBegin(bgl.GL_LINE_STRIP)
        bgl.glVertex3f(p1[0], p1[1], p1[2])
        bgl.glVertex3f(p1[0], p2[1], p1[2])
        bgl.glEnd()
        # Z
        bgl.glColor4f(0, 0, 1, 0.8)
        bgl.glBegin(bgl.GL_LINE_STRIP)
        bgl.glVertex3f(p1[0], p1[1], p1[2])
        bgl.glVertex3f(p1[0], p1[1], p2[2])
        bgl.glEnd()

        # Dist
        width = 2
        bgl.glLineWidth(width)
        bgl.glColor4f(color[0], color[1], color[2], color[3])
        bgl.glBegin(bgl.GL_LINE_STRIP)
        bgl.glVertex3f(p1[0], p1[1], p1[2])
        bgl.glVertex3f(p2[0], p2[1], p2[2])
        bgl.glEnd()

        # ---
        # Restore previous OpenGL settings
        bgl.glLoadIdentity()
        bgl.glMatrixMode(MatrixMode_prev)
        bgl.glLoadMatrixf(ProjMatrix_prev)
        bgl.glLineWidth(lineWidth_prev)
        if not blend_prev:
            bgl.glDisable(bgl.GL_BLEND)
        if not line_stipple_prev:
            bgl.glDisable(bgl.GL_LINE_STIPPLE)
        bgl.glColor4f(color_prev[0], color_prev[1], color_prev[2],
                      color_prev[3])

        # ---
        # Draw (2D) text
        # We do this after drawing the lines so
        # we can draw it OVER the line.
        coord_2d = location_3d_to_region_2d(
            context.region,
            context.space_data.region_3d,
            p1.lerp(p2, 0.5),
        )
        OFFSET_LINE = 10  # Offset the text a bit to the right.
        OFFSET_Y = 15  # Offset of the lines.
        OFFSET_VALUE = 30  # Offset of value(s) from the text.
        dist = (p1 - p2).length

        # Write distance value into the scene property,
        # so we can display it in the panel & refresh the panel.
        if hasattr(sce, "measure_panel_dist"):
            sce.measure_panel_dist = dist
            context.area.tag_redraw()

        texts = [("Dist:", round(dist, PRECISION)),
                 ("X:", round(abs(p1[0] - p2[0]), PRECISION)),
                 ("Y:", round(abs(p1[1] - p2[1]), PRECISION)),
                 ("Z:", round(abs(p1[2] - p2[2]), PRECISION))]

        # Draw all texts
        # @todo Get user pref for text color in 3D View
        bgl.glColor4f(1.0, 1.0, 1.0, 1.0)
        blf.size(0, 12, 72)  # Prevent font size to randomly change.

        loc_x = coord_2d[0] + OFFSET_LINE
        loc_y = coord_2d[1]
        for t in texts:
            text = t[0]
            value = str(t[1]) + " BU"

            blf.position(0, loc_x, loc_y, 0)
            blf.draw(0, text)
            blf.position(0, loc_x + OFFSET_VALUE, loc_y, 0)
            blf.draw(0, value)

            loc_y -= OFFSET_Y

    # Handle mesh surface area calulations
    if (sce.measure_panel_calc_area):
        # Get a single selected object (or nothing).
        obj = getSingleObject(context)

        if (context.mode == 'EDIT_MESH'):
            obj = context.active_object

            if (obj and obj.type == 'MESH' and obj.data):
                # "Note: a Mesh will return the selection state of the mesh
                # when EditMode was last exited. A Python script operating
                # in EditMode must exit EditMode before getting the current
                # selection state of the mesh."
                # http://www.blender.org/documentation/249PythonDoc/
                # /Mesh.MVert-class.html#sel
                # We can only provide this by existing & re-entering EditMode.
                # @todo: Better way to do this?

                # Get mesh data from Object.
                mesh = obj.data

                # Get transformation matrix from object.
                ob_mat = obj.matrix_world
                # Also make an inversed copy! of the matrix.
                ob_mat_inv = ob_mat.copy()
                Matrix.invert(ob_mat_inv)

                # Get the selected vertices.
                # @todo: Better (more efficient) way to do this?
                verts_selected = [v for v in mesh.vertices if v.select == 1]

                if len(verts_selected) >= 3:
                    # Get selected faces
                    # @todo: Better (more efficient) way to do this?
                    faces_selected = [f for f in mesh.faces if f.select == 1]

                    if len(faces_selected) > 0:
                        area, normal = objectSurfaceArea(
                            obj, True, measureGlobal(sce))
                        if (area >= 0):
                            sce.measure_panel_area1 = area
                            sce.measure_panel_normal1 = normal

        elif (context.mode == 'OBJECT'):
            # We are working in object mode.

            if len(context.selected_objects) > 2:
                return


# @todo Make this work again.
#                # We have more that 2 objects selected...
#
#                mesh_objects = [o for o in context.selected_objects
#                    if (o.type == 'MESH')]

#                if (len(mesh_objects) > 0):
#                    # ... and at least one of them is a mesh.
#
#                    for o in mesh_objects:
#                        area = objectSurfaceArea(o, False,
#                            measureGlobal(sce))
#                        if (area >= 0):
#                            #row.label(text=o.name, icon='OBJECT_DATA')
#                            #row.label(text=str(round(area, PRECISION))
#                            #    + " BU^2")

            elif len(context.selected_objects) == 2:
                # 2 objects selected.

                obj1, obj2 = context.selected_objects

                # Calculate surface area of the objects.
                area1, normal1 = objectSurfaceArea(obj1, False,
                                                   measureGlobal(sce))
                area2, normal2 = objectSurfaceArea(obj2, False,
                                                   measureGlobal(sce))
                sce.measure_panel_area1 = area1
                sce.measure_panel_area2 = area2
                sce.measure_panel_normal1 = normal1
                sce.measure_panel_normal2 = normal2

            elif (obj):
                # One object selected.

                # Calculate surface area of the object.
                area, normal = objectSurfaceArea(obj, False,
                                                 measureGlobal(sce))

                sce.measure_panel_area1 = area
                sce.measure_panel_normal1 = normal

    if (sce.measure_panel_calc_volume):
        obj = getSingleObject(context)

        if (context.mode == 'OBJECT'):
            # We are working in object mode.

            #if len(context.selected_objects) > 2:       # TODO

            #el
            if len(context.selected_objects) == 2:
                # 2 objects selected.

                obj1, obj2 = context.selected_objects

                # Calculate surface area of the objects.
                volume1 = objectVolume(obj1, measureGlobal(sce))
                volume2 = objectVolume(obj2, measureGlobal(sce))

                sce.measure_panel_volume1 = volume1
                sce.measure_panel_volume2 = volume2

            elif (obj):
                # One object selected.

                # Calculate surface area of the object.
                volume1 = objectVolume(obj, measureGlobal(sce))

                sce.measure_panel_volume1 = volume1
def draw_sunpath_callback(self, context):
    def drawLine(points, loop=False, color=(0.1, 0.4, 0.8, 0.8), thickness=2):
        bgl.glLineWidth(thickness)
        bgl.glBlendColor(color[0], color[1], color[2], color[3])
        bgl.glBegin(bgl.GL_LINE_STRIP)
        for p in points:
            bgl.glVertex3f(p[0], p[1], p[2])
        if loop:
            p = points[0]
            bgl.glVertex3f(p[0], p[1], p[2])
        bgl.glEnd()
        return points

    def dayPoints(Month, Day):
        sc = bpy.context.scene
        N = sc.Site.northAxis
        Long = sc.Site.longitude
        Lat = sc.Site.latitude
        TimeZone = sc.Site.timezone
        dt = 4  # timesteps per hour
        coords = [(0.0, 0.0, 0.0)] * (24 * dt)
        i = 0
        for Hour in range(24):
            for T in range(dt):
                Minute = T * (60 / dt)
                (Az, El) = Solar_Pos(Long, Lat, TimeZone, Month, Day, Hour,
                                     Minute)
                if sc.ODS_SUN.sunpath.flat:
                    if sc.ODS_SUN.sunpath.equi:
                        coords[i] = azElToPolar(radians(Az) + N, radians(El))
                    else:
                        coords[i] = azElToXYZ(radians(Az) + N, radians(El))
                        coords[i][2] = 0.0
                else:
                    coords[i] = azElToXYZ(radians(Az) + N, radians(El))
                # Finally shift the coordinate to the offset centre
                for j in range(3):
                    coords[i][j] += sc.ODS_SUN.sunpath.pos[j]
                i += 1
        return coords

    def drawCompassRose(color=(0.8, 0.8, 0.8, 1.0)):
        def tick(ang, f):
            X = sin(ang)
            Y = cos(ang)
            return [(R * X + O[0], R * Y + O[1], O[2]),
                    (R * f * X + O[0], R * f * Y + O[1], O[2])]

        sc = bpy.context.scene
        R = sc.ODS_SUN.arcRadius
        N = sc.Site.northAxis
        O = sc.ODS_SUN.sunpath.pos
        T = [N + (i * 2 * pi / 360) for i in range(360)]
        # Draw inner concentric circles
        if sc.ODS_SUN.sunpath.circles:
            for i in range(1, 9):
                if sc.ODS_SUN.sunpath.flat and sc.ODS_SUN.sunpath.equi:
                    r = (i * R / 9)
                else:
                    r = R * sin((pi / 2) * (i / 9))
                P = [(r * sin(t) + O[0], r * cos(t) + O[1], O[2]) for t in T]
                drawLine(P, loop=True, color=color, thickness=0.7)
        # Draw the thick outer circle (90 degrees)
        P = [(R * sin(t) + O[0], R * cos(t) + O[1], O[2]) for t in T]
        drawLine(P, loop=True, color=color)
        # Draw the ticks
        drawLine(tick(N, 1.2), color=color)
        for i in range(8):
            T = N + (i * 2 * pi / 8)
            drawLine(tick(T, 1.1), color=color)
        for i in range(36):
            T = N + (i * 2 * pi / 36)
            drawLine(tick(T, 1.05), color=color)
        return

    def calcSunPath():
        datum = datetime.datetime(2010, 1, 1)
        for day in range(365):
            t = datum + datetime.timedelta(day)
            dp = dayPoints(t.month, t.day)
            points[day] = dp
            if t.day == 1:
                daylines[t.month - 1] = dp
        for i in range(0, 24):
            loops[i] = [points[day][i * 4] for day in range(365)]
        return None

    def getTextLocation():
        context = bpy.context
        scene = bpy.context.scene
        X = 62
        Y = 4
        pos_x = int((context.region.width) - X)
        pos_y = int(Y)  #(context.region.height)
        return (pos_x, pos_y)

    def drawTime():
        font_size = 20
        (r, g, b, alpha) = (1.0, 1.0, 1.0, 1.0)
        blf.size(0, font_size, 72)
        (pos_x, pos_y) = getTextLocation()
        blf.position(0, pos_x, pos_y, 0)
        bgl.glBlendColor(r, g, b, alpha)
        (hour, minute) = frameToTime(bpy.context.scene.frame_current)
        if hour >= 24:
            minute = 0
        blf.draw(0, "%02i:%02i" % (min(hour, 24), minute))
        return None

    def drawSunPath():
        for line in daylines:
            drawLine(line)
        for line in loops:
            drawLine(line, loop=True, color=(1.0, 0.8, 0.0, 1.0))
        drawCompassRose()
        return None

    # Draw the time
    if bpy.context.scene.ODS_SUN.sunpath.time:
        drawTime()

    if not bpy.context.scene.ODS_SUN.sunpath.path:
        return None

    # Get & convert the Perspective Matrix of the current view/region.
    #bgl.glClear(bgl.GL_COLOR_BUFFER_BIT)
    view3d = bpy.context
    region = view3d.region_data
    perspMatrix = region.perspective_matrix
    tempMat = [perspMatrix[j][i] for i in range(4) for j in range(4)]
    perspBuff = bgl.Buffer(bgl.GL_FLOAT, 16, tempMat)

    # ---
    # Store previous OpenGL settings.
    # Store MatrixMode
    MatrixMode_prev = bgl.Buffer(bgl.GL_INT, [1])
    bgl.glGetIntegerv(2976, MatrixMode_prev)  # bgl.GL_MATRIX_MODE
    MatrixMode_prev = MatrixMode_prev[0]

    # Store projection matrix
    ProjMatrix_prev = bgl.Buffer(bgl.GL_DOUBLE, [16])
    bgl.glGetFloatv(2983, ProjMatrix_prev)  # bgl.GL_PROJECTION_MATRIX

    # Store Line width
    lineWidth_prev = bgl.Buffer(bgl.GL_FLOAT, [1])
    bgl.glGetFloatv(bgl.GL_LINE_WIDTH, lineWidth_prev)
    lineWidth_prev = lineWidth_prev[0]

    # Store GL_BLEND
    blend_prev = bgl.Buffer(bgl.GL_BYTE, [1])
    bgl.glGetFloatv(bgl.GL_BLEND, blend_prev)
    blend_prev = blend_prev[0]

    line_stipple_prev = bgl.Buffer(bgl.GL_BYTE, [1])
    bgl.glGetFloatv(2852, line_stipple_prev)  # bgl.GL_LINE_STIPPLE
    line_stipple_prev = line_stipple_prev[0]

    # Store glColor4f
    color_prev = bgl.Buffer(bgl.GL_FLOAT, [4])
    bgl.glGetFloatv(bgl.GL_COLOR, color_prev)

    # ---
    # Prepare for 3D drawing
    bgl.glLoadIdentity()
    bgl.glMatrixMode(5889)  # bgl.GL_PROJECTION
    bgl.glLoadMatrixf(perspBuff)

    bgl.glEnable(bgl.GL_BLEND)
    bgl.glEnable(bgl.GL_LINE_SMOOTH)
    #bgl.glEnable(bgl.GL_LINE_STIPPLE)
    if not bpy.context.scene.ODS_SUN.sunpath.xray:
        bgl.glEnable(bgl.GL_DEPTH_TEST)

    # Draw the day sunpath lines
    if bpy.context.scene.ODS_SUN.sunpath.recalc:
        calcSunPath()
        drawSunPath()
        bpy.context.scene.ODS_SUN.sunpath.recalc = False
    else:
        drawSunPath()

    # ---
    # Restore previous OpenGL settings
    if not bpy.context.scene.ODS_SUN.sunpath.xray:
        bgl.glDisable(bgl.GL_DEPTH_TEST)
    bgl.glLoadIdentity()
    bgl.glMatrixMode(MatrixMode_prev)
    bgl.glLoadMatrixf(ProjMatrix_prev)
    bgl.glLineWidth(lineWidth_prev)
    if not blend_prev:
        bgl.glDisable(bgl.GL_BLEND)
    if not line_stipple_prev:
        bgl.glDisable(2852)  # bgl.GL_LINE_STIPPLE
    bgl.glBlendColor(color_prev[0], color_prev[1], color_prev[2],
                     color_prev[3])

    return None
Example #8
0
def draw_callback_viewUV(area, UV_TO_VIEW, id):
    # print(id, area.spaces[0].image, area.spaces[0].show_uvedit )

    settings = bpy.context.scene.uv_highlight
    prefs = bpy.context.user_preferences.addons[__package__].preferences
    mode = bpy.context.scene.tool_settings.uv_select_mode

    # remove closed areas
    if len(area.regions) == 0 or area.type != "IMAGE_EDITOR":
        bpy.types.SpaceImageEditor.draw_handler_remove(IMAGE_EDITORS[area], 'WINDOW')
        IMAGE_EDITORS.pop(area, None)
        # print("removing Image_Editor from drawing: %s" % id)
        return

    # dont show this if the area is in Image mode :D
    if not main.isEditingUVs() or area.spaces[0].mode != "VIEW" or not area.spaces[0].show_uvedit:
        # print("skipping Image_Editor from drawing: %s" % id)
        return

    sync_mode = bpy.context.scene.tool_settings.use_uv_select_sync

    viewport_info = bgl.Buffer(bgl.GL_INT, 4)
    bgl.glGetIntegerv(bgl.GL_VIEWPORT, viewport_info)

    for region in area.regions:
        if region.type == "WINDOW":
            width = region.width
            height = region.height
            region_x = region.x
            region_y = region.y

    bgl.glViewport(region_x, region_y, width, height)

    bgl.glMatrixMode(bgl.GL_MODELVIEW)
    bgl.glPushMatrix()
    # bgl.glLoadIdentity()

    origin = UV_TO_VIEW(0, 0, False)
    axis = UV_TO_VIEW(1.0, 0, False)[0] - origin[0]

    M = (axis, 0, 0, 0,
         0, axis, 0, 0,
         0, 0, 1.0, 0,
         origin[0], origin[1], 0, 1.0)
    m = bgl.Buffer(bgl.GL_FLOAT, 16, M)
    bgl.glLoadMatrixf(m)



    if settings.show_hidden_faces and not sync_mode:
        bgl.glBlendFunc(bgl.GL_ONE, bgl.GL_ONE)
        draw_vertex_array("hidden_edges", bgl.GL_LINES, 2, prefs.uv_hidden_faces)

    if settings.show_udim_indices:
        draw_udim_tiles(M, prefs.udim_markers)

    # PRE HIGHLIGHT VERTS
    if settings.show_preselection and main.UV_MOUSE and UV_TO_VIEW and not sync_mode:
        if mode == 'VERTEX' and main.closest_vert and main.closest_vert[1]:
            bgl.glLoadIdentity()
            bgl.glPointSize(5.0)
            bgl.glBegin(bgl.GL_POINTS)
            bgl.glColor4f(*prefs.uv_preselection_color_verts_edges)

            if main.other_vert:
                bgl.glVertex2i(*UV_TO_VIEW(*main.other_vert))

            bgl.glVertex2i(*UV_TO_VIEW(main.closest_vert[1][0], main.closest_vert[1][1], False))

            bgl.glEnd()

            # print("MOUSE: %s, ClosestVert: %s - %s" % (UV_MOUSE, closestVert[1], view))
        elif mode == 'EDGE':
            # draw dark first, then overpaint with brighter colour
            bgl.glLoadIdentity()
            bgl.glLineWidth(3.5)
            bgl.glEnable(bgl.GL_LINE_SMOOTH)
            bgl.glEnable(bgl.GL_BLEND)
            bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
            bgl.glBegin(bgl.GL_LINES)
            # edge
            if main.closest_edge and main.closest_edge[1][0] and main.closest_edge[1][1]:
                bgl.glColor3f(*COLOR_BLACK)
                bgl.glVertex2i(*(UV_TO_VIEW(*main.closest_edge[1][0], False)))
                bgl.glVertex2i(*(UV_TO_VIEW(*main.closest_edge[1][1], False)))
            # matching edge
            if main.other_edge and main.other_edge[1][0] and main.other_edge[1][1]:
                bgl.glColor3f(*COLOR_BLACK)
                bgl.glVertex2i(*(UV_TO_VIEW(*main.other_edge[1][0], False)))
                bgl.glVertex2i(*(UV_TO_VIEW(*main.other_edge[1][1], False)))
            bgl.glEnd()

            bgl.glLineWidth(2)
            bgl.glBegin(bgl.GL_LINES)
            # edge
            if main.closest_edge and main.closest_edge[1][0] and main.closest_edge[1][1]:
                bgl.glColor4f(*prefs.uv_preselection_color_verts_edges)
                bgl.glVertex2i(*(UV_TO_VIEW(*main.closest_edge[1][0], False)))
                bgl.glVertex2i(*(UV_TO_VIEW(*main.closest_edge[1][1], False)))
            # matching edge
            if main.other_edge and main.other_edge[1][0] and main.other_edge[1][1]:
                bgl.glColor4f(*prefs.uv_preselection_color_verts_edges)
                bgl.glVertex2i(*(UV_TO_VIEW(*main.other_edge[1][0], False)))
                bgl.glVertex2i(*(UV_TO_VIEW(*main.other_edge[1][1], False)))
            bgl.glEnd()

        else:

            bgl.glDisable((bgl.GL_CULL_FACE))
            bgl.glEnable(bgl.GL_BLEND)
            bgl.glBlendFunc(bgl.GL_ONE, bgl.GL_ONE)

            draw_vertex_array("closest_face_uvs", bgl.GL_TRIANGLES, 2, prefs.uv_preselection_color_faces)

    bgl.glViewport(*tuple(viewport_info))
    bgl.glMatrixMode(bgl.GL_MODELVIEW)
    bgl.glPopMatrix()
    bgl.glMatrixMode(bgl.GL_PROJECTION)
    bgl.glPopMatrix()

    restore_opengl_defaults()
Example #9
0
    def draw(self, area, region_data):
        """
        Draw avatar view in given context
        """
        # TODO: Add this color to addon option
        color = (1.0, 1.0, 0.5, 1.0)
        alpha = 2.0*math.atan((18.0/2.0)/self.lens.value[0])
        dist = 0.5/(math.tan(alpha/2.0))
        height = 1.0
        if self.height.value[0] == 0:
            width = 0.7
        else:
            width = self.width.value[0]/self.height.value[0]
                    
        points = {}
        points['border'] = [None, None, None, None]
        points['center'] = [None]
        
        # Points of face
        points['right_eye'] = [mathutils.Vector((0.25, 0.25, self.distance.value[0] - dist)), \
            mathutils.Vector((0.3, 0.25, self.distance.value[0] - dist)), \
            mathutils.Vector((0.3, 0.0, self.distance.value[0] - dist)), \
            mathutils.Vector((0.25, 0.0, self.distance.value[0] - dist)), \
            mathutils.Vector((0.25, 0.25, self.distance.value[0] - dist))]
        points['left_eye'] = [mathutils.Vector((-0.25, 0.25, self.distance.value[0] - dist)), \
            mathutils.Vector((-0.3, 0.25, self.distance.value[0] - dist)), \
            mathutils.Vector((-0.3, 0.0, self.distance.value[0] - dist)), \
            mathutils.Vector((-0.25, 0.0, self.distance.value[0] - dist)), \
            mathutils.Vector((-0.25, 0.25, self.distance.value[0] - dist))]
        
        points['mouth'] = [mathutils.Vector((-0.40912365913391113, -0.11777058243751526, self.distance.value[0] - dist)), \
            mathutils.Vector((-0.3441678285598755, -0.15873458981513977, self.distance.value[0] - dist)), \
            mathutils.Vector((-0.2563667893409729, -0.1998385488986969, self.distance.value[0] - dist)), \
            mathutils.Vector((-0.18191590905189514, -0.22385218739509583, self.distance.value[0] - dist)), \
            mathutils.Vector((-0.10375960171222687, -0.23957833647727966, self.distance.value[0] - dist)), \
            mathutils.Vector((0.0, -0.2464955747127533, self.distance.value[0] - dist)), \
            mathutils.Vector((0.10375960171222687, -0.23957833647727966, self.distance.value[0] - dist)), \
            mathutils.Vector((0.18191590905189514, -0.22385218739509583, self.distance.value[0] - dist)), \
            mathutils.Vector((0.2563667893409729, -0.1998385488986969, self.distance.value[0] - dist)), \
            mathutils.Vector((0.3441678285598755, -0.15873458981513977, self.distance.value[0] - dist)), \
            mathutils.Vector((0.40912365913391113, -0.11777058243751526, self.distance.value[0] - dist))]            
                
        # Put border points of camera to basic position
        points['border'][0] = mathutils.Vector((-width/2.0, \
            -0.5, \
            self.distance.value[0] - dist,
            1.0))
        points['border'][1] = mathutils.Vector((width/2.0, \
            -0.5, \
            self.distance.value[0] - dist,
            1.0))
        points['border'][2] = mathutils.Vector((width/2.0, \
            0.5, \
            self.distance.value[0] - dist, \
            1.0))
        points['border'][3] = mathutils.Vector((-width/2.0, \
            0.5, \
            self.distance.value[0] - dist, \
            1.0))
        
        # Center of view
        points['center'][0] = mathutils.Vector((0.0, \
            0.0, \
            self.distance.value[0], \
            1.0))        
        
        # Create transformation (rotation) matrix
        rot_matrix = mathutils.Quaternion(self.rotation.value).to_matrix().to_4x4()
        
        # Transform points in all point groups
        for point_group in points.values():
            for index in range(len(point_group)):
                # Rotate points
                point_group[index] = (rot_matrix*point_group[index]).to_3d()
                # Move points
                point_group[index] += mathutils.Vector(self.location.value)
        
        # Get & convert the Perspective Matrix of the current view/region.
        perspMatrix = region_data.perspective_matrix
        tempMat = [perspMatrix[j][i] for i in range(4) for j in range(4)]
        perspBuff = bgl.Buffer(bgl.GL_FLOAT, 16, tempMat)
    
        # Store previous OpenGL settings.
        # Store MatrixMode
        MatrixMode_prev = bgl.Buffer(bgl.GL_INT, [1])
        bgl.glGetIntegerv(bgl.GL_MATRIX_MODE, MatrixMode_prev)
        MatrixMode_prev = MatrixMode_prev[0]
    
        # Store projection matrix
        ProjMatrix_prev = bgl.Buffer(bgl.GL_DOUBLE, [16])
        bgl.glGetFloatv(bgl.GL_PROJECTION_MATRIX, ProjMatrix_prev)
    
        # Store Line width
        lineWidth_prev = bgl.Buffer(bgl.GL_FLOAT, [1])
        bgl.glGetFloatv(bgl.GL_LINE_WIDTH, lineWidth_prev)
        lineWidth_prev = lineWidth_prev[0]
    
        # Store GL_BLEND
        blend_prev = bgl.Buffer(bgl.GL_BYTE, [1])
        bgl.glGetFloatv(bgl.GL_BLEND, blend_prev)
        blend_prev = blend_prev[0]
        
        # Store GL_DEPTH_TEST
        depth_test_prev = bgl.Buffer(bgl.GL_BYTE, [1])
        bgl.glGetFloatv(bgl.GL_DEPTH_TEST, depth_test_prev)
        depth_test_prev = depth_test_prev[0]
            
        # Store GL_LINE_STIPPLE
        line_stipple_prev = bgl.Buffer(bgl.GL_BYTE, [1])
        bgl.glGetFloatv(bgl.GL_LINE_STIPPLE, line_stipple_prev)
        line_stipple_prev = line_stipple_prev[0]
    
        # Store glColor4f
        col_prev = bgl.Buffer(bgl.GL_FLOAT, [4])
        bgl.glGetFloatv(bgl.GL_COLOR, col_prev)
        
        # Prepare for 3D drawing
        bgl.glLoadIdentity()
        bgl.glMatrixMode(bgl.GL_PROJECTION)
        bgl.glLoadMatrixf(perspBuff)
        bgl.glEnable(bgl.GL_BLEND)
        bgl.glEnable(bgl.GL_DEPTH_TEST)
                
        # Draw "Look At" point
        bgl.glLineWidth(1)
        bgl.glBegin(bgl.GL_LINES)
        bgl.glColor4f(color[0], color[1], color[2], color[3])
        
        bgl.glVertex3f(self.location.value[0]+0.1, \
            self.location.value[1], \
            self.location.value[2])
        bgl.glVertex3f(self.location.value[0]-0.1, \
            self.location.value[1], \
            self.location.value[2])
        
        bgl.glVertex3f(self.location.value[0], \
            self.location.value[1]+0.1, \
            self.location.value[2])
        bgl.glVertex3f(self.location.value[0], \
            self.location.value[1]-0.1, \
            self.location.value[2])
        
        bgl.glVertex3f(self.location.value[0], \
            self.location.value[1], \
            self.location.value[2]+0.1)
        bgl.glVertex3f(self.location.value[0], \
            self.location.value[1], \
            self.location.value[2]-0.1)
        
        bgl.glEnd()
        
        border = points['border']
        center = points['center']
        
        # Draw border of camera
        bgl.glBegin(bgl.GL_LINE_STRIP)
        bgl.glVertex3f(border[0][0], border[0][1], border[0][2])
        bgl.glVertex3f(border[1][0], border[1][1], border[1][2])
        bgl.glVertex3f(border[2][0], border[2][1], border[2][2])
        bgl.glVertex3f(border[3][0], border[3][1], border[3][2])
        bgl.glVertex3f(border[0][0], border[0][1], border[0][2])
        bgl.glEnd()
        
        # Draw left eye
        bgl.glBegin(bgl.GL_LINE_STRIP)
        for point in points['left_eye']:
            bgl.glVertex3f(point[0], point[1], point[2])
        bgl.glEnd()

        # Draw right eye
        bgl.glBegin(bgl.GL_LINE_STRIP)
        for point in points['right_eye']:
            bgl.glVertex3f(point[0], point[1], point[2])
        bgl.glEnd()
        
        # Draw mouth
        bgl.glBegin(bgl.GL_LINE_STRIP)
        for point in points['mouth']:
            bgl.glVertex3f(point[0], point[1], point[2])
        bgl.glEnd()
        
        # Draw dashed lines from center of "camera" to border of camera        
        bgl.glEnable(bgl.GL_LINE_STIPPLE)
        bgl.glBegin(bgl.GL_LINES)
        bgl.glVertex3f(border[0][0], border[0][1], border[0][2])
        bgl.glVertex3f(center[0][0], center[0][1], center[0][2])
        bgl.glVertex3f(border[1][0], border[1][1], border[1][2])
        bgl.glVertex3f(center[0][0], center[0][1], center[0][2])
        bgl.glVertex3f(border[2][0], border[2][1], border[2][2])
        bgl.glVertex3f(center[0][0], center[0][1], center[0][2])
        bgl.glVertex3f(border[3][0], border[3][1], border[3][2])
        bgl.glVertex3f(center[0][0], center[0][1], center[0][2])
        bgl.glEnd()
        
        # Draw dashed line from Look At point and center of camera
        bgl.glBegin(bgl.GL_LINES)
        bgl.glVertex3f(self.location.value[0], \
            self.location.value[1], \
            self.location.value[2])
        bgl.glVertex3f(center[0][0], center[0][1], center[0][2])
        bgl.glEnd()
        bgl.glDisable(bgl.GL_LINE_STIPPLE)    

        # Restore previous OpenGL settings
        bgl.glLoadIdentity()
        bgl.glMatrixMode(MatrixMode_prev)
        bgl.glLoadMatrixf(ProjMatrix_prev)
        bgl.glLineWidth(lineWidth_prev)
        if not blend_prev:
            bgl.glDisable(bgl.GL_BLEND)
        if not line_stipple_prev:
            bgl.glDisable(bgl.GL_LINE_STIPPLE)
        if not depth_test_prev:
            bgl.glDisable(bgl.GL_DEPTH_TEST)

        # Draw username
        coord_2d = location_3d_to_region_2d(
            area.regions[4],
            region_data,
            center[0])
        font_id, font_size, my_dpi = 0, 12, 72 # TODO: add to addon options
        blf.size(font_id, font_size, my_dpi)
        text_width, text_height = blf.dimensions(font_id, self.username)
        blf.position(font_id, coord_2d[0], coord_2d[1], 0)
        blf.draw(font_id, self.username)

        bgl.glColor4f(col_prev[0], col_prev[1], col_prev[2], col_prev[3])
Example #10
0
def DrawNorth_callback(self, context):

    if not Sun.SP.ShowNorth and North.isActive:
        North.deactivate()
        return

    # ------------------------------------------------------------------
    # Set up the compass needle using the current north offset angle
    # less 90 degrees.  This forces the unit circle to begin at the
    # 12 O'clock instead of 3 O'clock position.
    # ------------------------------------------------------------------
    color = (0.2, 0.6, 1.0, 0.7)
    radius = 100
    angle = -(Sun.NorthOffset - math.pi / 2)
    x = math.cos(angle) * radius
    y = math.sin(angle) * radius

    p1, p2 = (0, 0, 0), (x, y, 0)   # Start & end of needle

    #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    # Thanks to Buerbaum Martin for the following which draws openGL
    # lines.  ( From his script space_view3d_panel_measure.py )
    #<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    # ------------------------------------------------------------------
    # Convert the Perspective Matrix of the current view/region.
    # ------------------------------------------------------------------
    view3d = bpy.context
    region = view3d.region_data
    perspMatrix = region.perspective_matrix
    tempMat = [perspMatrix[j][i] for i in range(4) for j in range(4)]
    perspBuff = bgl.Buffer(bgl.GL_FLOAT, 16, tempMat)

    # ---------------------------------------------------------
    # Store previous OpenGL settings.
    # ---------------------------------------------------------
    MatrixMode_prev = bgl.Buffer(bgl.GL_INT, [1])
    bgl.glGetIntegerv(bgl.GL_MATRIX_MODE, MatrixMode_prev)
    MatrixMode_prev = MatrixMode_prev[0]

    # Store projection matrix
    ProjMatrix_prev = bgl.Buffer(bgl.GL_DOUBLE, [16])
    bgl.glGetFloatv(bgl.GL_PROJECTION_MATRIX, ProjMatrix_prev)

    # Store Line width
    lineWidth_prev = bgl.Buffer(bgl.GL_FLOAT, [1])
    bgl.glGetFloatv(bgl.GL_LINE_WIDTH, lineWidth_prev)
    lineWidth_prev = lineWidth_prev[0]

    # Store GL_BLEND
    blend_prev = bgl.Buffer(bgl.GL_BYTE, [1])
    bgl.glGetFloatv(bgl.GL_BLEND, blend_prev)
    blend_prev = blend_prev[0]

    line_stipple_prev = bgl.Buffer(bgl.GL_BYTE, [1])
    bgl.glGetFloatv(bgl.GL_LINE_STIPPLE, line_stipple_prev)
    line_stipple_prev = line_stipple_prev[0]

    # Store glColor4f
    color_prev = bgl.Buffer(bgl.GL_FLOAT, [4])
    bgl.glGetFloatv(bgl.GL_COLOR, color_prev)

    # ---------------------------------------------------------
    # Prepare for 3D drawing
    # ---------------------------------------------------------
    bgl.glLoadIdentity()
    bgl.glMatrixMode(bgl.GL_PROJECTION)
    bgl.glLoadMatrixf(perspBuff)

    bgl.glEnable(bgl.GL_BLEND)
    bgl.glEnable(bgl.GL_LINE_STIPPLE)

    # ------------------
    # and draw the line
    # ------------------
    width = 2
    bgl.glLineWidth(width)
    bgl.glColor4f(color[0], color[1], color[2], color[3])
    bgl.glBegin(bgl.GL_LINE_STRIP)
    bgl.glVertex3f(p1[0], p1[1], p1[2])
    bgl.glVertex3f(p2[0], p2[1], p2[2])
    bgl.glEnd()

    # ---------------------------------------------------------
    # Restore previous OpenGL settings
    # ---------------------------------------------------------
    bgl.glLoadIdentity()
    bgl.glMatrixMode(MatrixMode_prev)
    bgl.glLoadMatrixf(ProjMatrix_prev)
    bgl.glLineWidth(lineWidth_prev)

    if not blend_prev:
        bgl.glDisable(bgl.GL_BLEND)
    if not line_stipple_prev:
        bgl.glDisable(bgl.GL_LINE_STIPPLE)

    bgl.glColor4f(color_prev[0],
                  color_prev[1],
                  color_prev[2],
                  color_prev[3])
Example #11
0
 def restore_3D(self):
     bgl.glLoadIdentity()
     bgl.glMatrixMode(self.matrix_mode)
     bgl.glLoadMatrixf(self.projection_matrix)
Example #12
0
 def prepare_3D(self):
     bgl.glLoadIdentity()
     bgl.glMatrixMode(bgl.GL_PROJECTION)
     bgl.glLoadMatrixf(self.persmat_buffer)
Example #13
0
    def draw(self, context):
        """
        Draw avatar view in given context
        """
        # TODO: Add this color to Add-on option
        color = (1.0, 1.0, 0.5, 1.0)
        alpha = 2.0 * math.atan((18.0 / 2.0) / self.lens.value[0])
        dist = 0.5 / (math.tan(alpha / 2.0))
        if self.height.value[0] == 0:
            width = 0.7
        else:
            width = self.width.value[0] / self.height.value[0]

        points = dict()
        points['border'] = [None, None, None, None]
        points['center'] = [None]

        # Points of face
        points['right_eye'] = [
            mathutils.Vector((0.25, 0.25, self.distance.value[0] - dist)),
            mathutils.Vector((0.3, 0.25, self.distance.value[0] - dist)),
            mathutils.Vector((0.3, 0.0, self.distance.value[0] - dist)),
            mathutils.Vector((0.25, 0.0, self.distance.value[0] - dist)),
            mathutils.Vector((0.25, 0.25, self.distance.value[0] - dist))
        ]
        points['left_eye'] = [
            mathutils.Vector((-0.25, 0.25, self.distance.value[0] - dist)),
            mathutils.Vector((-0.3, 0.25, self.distance.value[0] - dist)),
            mathutils.Vector((-0.3, 0.0, self.distance.value[0] - dist)),
            mathutils.Vector((-0.25, 0.0, self.distance.value[0] - dist)),
            mathutils.Vector((-0.25, 0.25, self.distance.value[0] - dist))
        ]

        points['mouth'] = [
            mathutils.Vector((-0.40912365913391113, -0.11777058243751526,
                              self.distance.value[0] - dist)),
            mathutils.Vector((-0.3441678285598755, -0.15873458981513977,
                              self.distance.value[0] - dist)),
            mathutils.Vector((-0.2563667893409729, -0.1998385488986969,
                              self.distance.value[0] - dist)),
            mathutils.Vector((-0.18191590905189514, -0.22385218739509583,
                              self.distance.value[0] - dist)),
            mathutils.Vector((-0.10375960171222687, -0.23957833647727966,
                              self.distance.value[0] - dist)),
            mathutils.Vector(
                (0.0, -0.2464955747127533, self.distance.value[0] - dist)),
            mathutils.Vector((0.10375960171222687, -0.23957833647727966,
                              self.distance.value[0] - dist)),
            mathutils.Vector((0.18191590905189514, -0.22385218739509583,
                              self.distance.value[0] - dist)),
            mathutils.Vector((0.2563667893409729, -0.1998385488986969,
                              self.distance.value[0] - dist)),
            mathutils.Vector((0.3441678285598755, -0.15873458981513977,
                              self.distance.value[0] - dist)),
            mathutils.Vector((0.40912365913391113, -0.11777058243751526,
                              self.distance.value[0] - dist))
        ]

        # Put border points of camera to basic position
        points['border'][0] = mathutils.Vector(
            (-width / 2.0, -0.5, self.distance.value[0] - dist, 1.0))
        points['border'][1] = mathutils.Vector(
            (width / 2.0, -0.5, self.distance.value[0] - dist, 1.0))
        points['border'][2] = mathutils.Vector(
            (width / 2.0, 0.5, self.distance.value[0] - dist, 1.0))
        points['border'][3] = mathutils.Vector(
            (-width / 2.0, 0.5, self.distance.value[0] - dist, 1.0))

        # Center of view
        points['center'][0] = mathutils.Vector(
            (0.0, 0.0, self.distance.value[0], 1.0))

        # Create transformation (rotation) matrix
        rot_matrix = mathutils.Quaternion(
            self.rotation.value).to_matrix().to_4x4()

        # Transform points in all point groups
        for point_group in points.values():
            for index in range(len(point_group)):
                # Rotate points
                point_group[index] = (rot_matrix * point_group[index]).to_3d()
                # Move points
                point_group[index] += mathutils.Vector(self.location.value)

        border = points['border']
        center = points['center']

        # Store glColor4f
        col_prev = bgl.Buffer(bgl.GL_FLOAT, [4])
        bgl.glGetFloatv(bgl.GL_COLOR, col_prev)

        bgl.glColor4f(color[0], color[1], color[2], color[3])

        # Draw username
        coord_2d = location_3d_to_region_2d(context.region,
                                            context.space_data.region_3d,
                                            center[0])

        # When coordinates are not outside window, then draw the name of avatar
        if coord_2d is not None:
            # TODO: add to Add-on options
            font_id, font_size, my_dpi = 0, 12, 72
            blf.size(font_id, font_size, my_dpi)
            blf.position(font_id, coord_2d[0] + 2, coord_2d[1] + 2, 0)
            blf.draw(font_id, str(self.username))

        # Get & convert the Perspective Matrix of the current view/region.
        persp_matrix = context.space_data.region_3d.perspective_matrix
        temp_mat = [persp_matrix[j][i] for i in range(4) for j in range(4)]
        persp_buff = bgl.Buffer(bgl.GL_FLOAT, 16, temp_mat)

        # Store previous OpenGL settings.
        # Store MatrixMode
        matrix_mode_prev = bgl.Buffer(bgl.GL_INT, [1])
        bgl.glGetIntegerv(bgl.GL_MATRIX_MODE, matrix_mode_prev)
        matrix_mode_prev = matrix_mode_prev[0]

        # Store projection matrix
        proj_matrix_prev = bgl.Buffer(bgl.GL_DOUBLE, [16])
        bgl.glGetFloatv(bgl.GL_PROJECTION_MATRIX, proj_matrix_prev)

        # Store Line width
        line_width_prev = bgl.Buffer(bgl.GL_FLOAT, [1])
        bgl.glGetFloatv(bgl.GL_LINE_WIDTH, line_width_prev)
        line_width_prev = line_width_prev[0]

        # Store GL_BLEND
        blend_prev = bgl.Buffer(bgl.GL_BYTE, [1])
        bgl.glGetFloatv(bgl.GL_BLEND, blend_prev)
        blend_prev = blend_prev[0]

        # Store GL_DEPTH_TEST
        depth_test_prev = bgl.Buffer(bgl.GL_BYTE, [1])
        bgl.glGetFloatv(bgl.GL_DEPTH_TEST, depth_test_prev)
        depth_test_prev = depth_test_prev[0]

        # Store GL_LINE_STIPPLE
        line_stipple_prev = bgl.Buffer(bgl.GL_BYTE, [1])
        bgl.glGetFloatv(bgl.GL_LINE_STIPPLE, line_stipple_prev)
        line_stipple_prev = line_stipple_prev[0]

        # Prepare for 3D drawing
        bgl.glLoadIdentity()
        bgl.glMatrixMode(bgl.GL_PROJECTION)
        bgl.glLoadMatrixf(persp_buff)
        bgl.glEnable(bgl.GL_BLEND)
        bgl.glEnable(bgl.GL_DEPTH_TEST)

        # Draw "Look At" point
        bgl.glLineWidth(1)
        bgl.glBegin(bgl.GL_LINES)
        bgl.glColor4f(color[0], color[1], color[2], color[3])

        bgl.glVertex3f(self.location.value[0] + 0.1, self.location.value[1],
                       self.location.value[2])
        bgl.glVertex3f(self.location.value[0] - 0.1, self.location.value[1],
                       self.location.value[2])

        bgl.glVertex3f(self.location.value[0], self.location.value[1] + 0.1,
                       self.location.value[2])
        bgl.glVertex3f(self.location.value[0], self.location.value[1] - 0.1,
                       self.location.value[2])

        bgl.glVertex3f(self.location.value[0], self.location.value[1],
                       self.location.value[2] + 0.1)
        bgl.glVertex3f(self.location.value[0], self.location.value[1],
                       self.location.value[2] - 0.1)

        bgl.glEnd()

        # Draw border of camera
        bgl.glBegin(bgl.GL_LINE_STRIP)
        bgl.glVertex3f(border[0][0], border[0][1], border[0][2])
        bgl.glVertex3f(border[1][0], border[1][1], border[1][2])
        bgl.glVertex3f(border[2][0], border[2][1], border[2][2])
        bgl.glVertex3f(border[3][0], border[3][1], border[3][2])
        bgl.glVertex3f(border[0][0], border[0][1], border[0][2])
        bgl.glEnd()

        # Draw left eye
        bgl.glBegin(bgl.GL_LINE_STRIP)
        for point in points['left_eye']:
            bgl.glVertex3f(point[0], point[1], point[2])
        bgl.glEnd()

        # Draw right eye
        bgl.glBegin(bgl.GL_LINE_STRIP)
        for point in points['right_eye']:
            bgl.glVertex3f(point[0], point[1], point[2])
        bgl.glEnd()

        # Draw mouth
        bgl.glBegin(bgl.GL_LINE_STRIP)
        for point in points['mouth']:
            bgl.glVertex3f(point[0], point[1], point[2])
        bgl.glEnd()

        # Draw dashed lines from center of "camera" to border of camera
        bgl.glEnable(bgl.GL_LINE_STIPPLE)
        bgl.glBegin(bgl.GL_LINES)
        bgl.glVertex3f(border[0][0], border[0][1], border[0][2])
        bgl.glVertex3f(center[0][0], center[0][1], center[0][2])
        bgl.glVertex3f(border[1][0], border[1][1], border[1][2])
        bgl.glVertex3f(center[0][0], center[0][1], center[0][2])
        bgl.glVertex3f(border[2][0], border[2][1], border[2][2])
        bgl.glVertex3f(center[0][0], center[0][1], center[0][2])
        bgl.glVertex3f(border[3][0], border[3][1], border[3][2])
        bgl.glVertex3f(center[0][0], center[0][1], center[0][2])
        bgl.glEnd()

        # Draw dashed line from Look At point and center of camera
        bgl.glBegin(bgl.GL_LINES)
        bgl.glVertex3f(self.location.value[0], self.location.value[1],
                       self.location.value[2])
        bgl.glVertex3f(center[0][0], center[0][1], center[0][2])
        bgl.glEnd()
        bgl.glDisable(bgl.GL_LINE_STIPPLE)

        # Restore previous OpenGL settings
        bgl.glLoadIdentity()
        bgl.glMatrixMode(matrix_mode_prev)
        bgl.glLoadMatrixf(proj_matrix_prev)
        bgl.glLineWidth(line_width_prev)
        if not blend_prev:
            bgl.glDisable(bgl.GL_BLEND)
        if not line_stipple_prev:
            bgl.glDisable(bgl.GL_LINE_STIPPLE)
        if not depth_test_prev:
            bgl.glDisable(bgl.GL_DEPTH_TEST)

        bgl.glColor4f(col_prev[0], col_prev[1], col_prev[2], col_prev[3])