def draw_callback(self, context): if self.drag_mode: if self.dragging_node: node = self.dragging_node loc = global_loc(node) x1, y1 = loc[0] - 10, loc[1] + 10 x2, y2 = loc[0] + node.dimensions[0] + 10, loc[ 1] - node.dimensions[1] - 10 shader = gpu.shader.from_builtin('2D_SMOOTH_COLOR') vertices = ((x1, y1), (x2, y1), (x2, y2), (x1, y2), (x1, y1)) vertex_colors = ((1, 1, 1, 0.5), (1, 1, 1, 0.5), (1, 1, 1, 0.5), (1, 1, 1, 0.5), (1, 1, 1, 0.5)) batch = batch_for_shader(shader, 'LINE_STRIP', { "pos": vertices, "color": vertex_colors }) shader.bind() batch.draw(shader) else: draw_circle_2d(self.cursor_pos, (1, 1, 1, 0.5), self.radius)
def draw_cursor(context, tool, xy): if not hasattr(context.scene, "perfect_select_tool_settings"): return if context.scene.perfect_select_tool_settings.show_select_cursor: props = tool.operator_properties("perfect_select.perfect_select") draw_circle_2d(xy, (1.0, ) * 4, props.radius, 32)
def draw(): global circle_pos pos = circle_pos brush_col = bpy.context.scene.tool_settings.unified_paint_settings.color col = (brush_col[0], brush_col[1], brush_col[2], 1) size = bpy.context.scene.tool_settings.unified_paint_settings.size draw_circle_2d(pos, col, size)
def draw_callback_2d(self, context): bgl.glEnable(bgl.GL_BLEND) # Have to add 1 for some reason in order to get proper number of segments. # This could potentially also be a ratio with the radius. circ_segments = 8 + 1 draw_circle_2d(self.m_coord, self.prefs.circ_color, self.prefs.circ_radius, circ_segments) bgl.glDisable(bgl.GL_BLEND)
def perfect_select_draw_callback(self, context): if context.area != self.draw_area: return co = (self.x, self.y) if self._snap_point: co = self._snap_point snap_color = (*bpy.context.preferences.themes[0].view_3d.vertex_select, 1.0) draw_circle_2d(co, snap_color, 6, 16) draw_circle_2d(co, (1.0, ) * 4, self.radius, 32)
def draw(self, context): # Many custom drawing examples can be found in the gpu module doc: # https://docs.blender.org/api/current/gpu.html matrix = context.region_data.perspective_matrix bgl.glEnable(bgl.GL_BLEND) bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glLineWidth(2) cur = context.scene.demo_tool_cursor rad = context.scene.demo_tool_radius draw_circle_2d((cur[0], cur[1]), (1, 1, 1, 1), rad, 200) # restore opengl defaults bgl.glLineWidth(1) bgl.glDisable(bgl.GL_BLEND) bgl.glDisable(bgl.GL_DEPTH_TEST)
def draw_circle_select(m_coords, radius=16, p_col=(0.7, 0.8, 1.0, 0.6), enabled=False, sub=False): if (enabled): f_col = p_col if sub: f_col = (1.0, 0.5, 0.4, 0.6) bgl.glEnable(bgl.GL_BLEND) radius = int(radius) presets.draw_circle_2d(m_coords, (f_col[0], f_col[1], f_col[2], f_col[3]), radius, segments=64) bgl.glLineWidth(1) bgl.glDisable(bgl.GL_BLEND)
def draw_cursor(context, tool, xy): from gpu_extras.presets import draw_circle_2d props = tool.operator_properties("view3d.select_circle") radius = props.radius draw_circle_2d(xy, (1.0, ) * 4, radius, 32)
def draw_2d_circles(radius, color, projected_points): bgl.glEnable(bgl.GL_BLEND) bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA) for point in projected_points: draw_circle_2d(point, color, radius)
# Create and fill offscreen ########################################## offscreen = gpu.types.GPUOffScreen(512, 512) with offscreen.bind(): bgl.glClear(bgl.GL_COLOR_BUFFER_BIT) with gpu.matrix.push_pop(): # reset matrices -> use normalized device coordinates [-1, 1] gpu.matrix.load_matrix(Matrix.Identity(4)) gpu.matrix.load_projection_matrix(Matrix.Identity(4)) amount = 10 for i in range(-amount, amount + 1): x_pos = i / amount draw_circle_2d((x_pos, 0.0), (1, 1, 1, 1), 0.5, 200) # Drawing the generated texture in 3D space ############################################# vertex_shader = ''' uniform mat4 modelMatrix; uniform mat4 viewProjectionMatrix; in vec2 position; in vec2 uv; out vec2 uvInterp; void main()
########################################## offscreen = gpu.types.GPUOffScreen(512, 512) with offscreen.bind(): fb = gpu.state.active_framebuffer_get() fb.clear(color=(0.0, 0.0, 0.0, 0.0)) with gpu.matrix.push_pop(): # reset matrices -> use normalized device coordinates [-1, 1] gpu.matrix.load_matrix(Matrix.Identity(4)) gpu.matrix.load_projection_matrix(Matrix.Identity(4)) amount = 10 for i in range(-amount, amount + 1): x_pos = i / amount draw_circle_2d((x_pos, 0.0), (1, 1, 1, 1), 0.5, segments=200) # Drawing the generated texture in 3D space ############################################# vertex_shader = ''' uniform mat4 modelMatrix; uniform mat4 viewProjectionMatrix; in vec2 position; in vec2 uv; out vec2 uvInterp; void main() {
HEIGHT = 512 RING_AMOUNT = 10 offscreen = gpu.types.GPUOffScreen(WIDTH, HEIGHT) with offscreen.bind(): bgl.glClear(bgl.GL_COLOR_BUFFER_BIT) with gpu.matrix.push_pop(): # reset matrices -> use normalized device coordinates [-1, 1] gpu.matrix.load_matrix(Matrix.Identity(4)) gpu.matrix.load_projection_matrix(Matrix.Identity(4)) for i in range(RING_AMOUNT): draw_circle_2d( (random.uniform(-1, 1), random.uniform(-1, 1)), (1, 1, 1, 1), random.uniform(0.1, 1), 20) buffer = bgl.Buffer(bgl.GL_BYTE, WIDTH * HEIGHT * 4) bgl.glReadBuffer(bgl.GL_BACK) bgl.glReadPixels(0, 0, WIDTH, HEIGHT, bgl.GL_RGBA, bgl.GL_UNSIGNED_BYTE, buffer) offscreen.free() if not IMAGE_NAME in bpy.data.images: bpy.data.images.new(IMAGE_NAME, WIDTH, HEIGHT) image = bpy.data.images[IMAGE_NAME] image.scale(WIDTH, HEIGHT) image.pixels = [v / 255 for v in buffer]
# Create and fill offscreen ########################################## offscreen = gpu.types.GPUOffScreen(512, 512) with offscreen.bind(): bgl.glClear(bgl.GL_COLOR_BUFFER_BIT) with gpu.matrix.push_pop(): # reset matrices -> use normalized device coordinates [-1, 1] gpu.matrix.load_matrix(Matrix.Identity(4)) gpu.matrix.load_projection_matrix(Matrix.Identity(4)) amount = 10 for i in range(-amount, amount + 1): x_pos = i / amount draw_circle_2d((x_pos, 0.0), (1, 1, 1, 1), 0.5, 200) # Drawing the generated texture in 3D space ############################################# vertex_shader = ''' uniform mat4 modelMatrix; uniform mat4 viewProjectionMatrix; in vec2 position; in vec2 uv; out vec2 uvInterp; void main() {
def Draw_2D_Circle(_center, _radius, _segments=32, _color=(0, 0, 0, .8)): draw_circle_2d(_center, _color, _radius, _segments)
RING_AMOUNT = 10 offscreen = gpu.types.GPUOffScreen(WIDTH, HEIGHT) with offscreen.bind(): fb = gpu.state.active_framebuffer_get() fb.clear(color=(0.0, 0.0, 0.0, 0.0)) with gpu.matrix.push_pop(): # reset matrices -> use normalized device coordinates [-1, 1] gpu.matrix.load_matrix(Matrix.Identity(4)) gpu.matrix.load_projection_matrix(Matrix.Identity(4)) for i in range(RING_AMOUNT): draw_circle_2d( (random.uniform(-1, 1), random.uniform(-1, 1)), (1, 1, 1, 1), random.uniform(0.1, 1), segments=20, ) buffer = fb.read_color(0, 0, WIDTH, HEIGHT, 4, 0, 'UBYTE') offscreen.free() if IMAGE_NAME not in bpy.data.images: bpy.data.images.new(IMAGE_NAME, WIDTH, HEIGHT) image = bpy.data.images[IMAGE_NAME] image.scale(WIDTH, HEIGHT) buffer.dimensions = WIDTH * HEIGHT * 4 image.pixels = [v / 255 for v in buffer]