From 8c345bd18dcb43332109b4867ae3d93c74591ef4 Mon Sep 17 00:00:00 2001 From: Alex Zastrow Date: Mon, 5 Feb 2024 18:12:56 +0100 Subject: [PATCH 1/9] Fix messages not showing --- phobos/blender/display.py | 68 ++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/phobos/blender/display.py b/phobos/blender/display.py index ace3a3b1..e107bf88 100644 --- a/phobos/blender/display.py +++ b/phobos/blender/display.py @@ -10,7 +10,8 @@ import collections -import bgl +import gpu +from gpu_extras.batch import batch_for_shader import blf import bpy from bpy.props import BoolProperty @@ -104,24 +105,17 @@ def draw_2dpolygon(points, linecolor=None, fillcolor=None, distance=0.2, linewid """ # background - bgl.glEnable(bgl.GL_BLEND) - bgl.glLineWidth(linewidth) + shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR') if fillcolor: - #bgl.glColor4f(*fillcolor) - bgl.glBegin(bgl.GL_POLYGON) - for p in points: - bgl.glVertex3f(*p, distance) - bgl.glEnd() + print("polygon", fillcolor) + shader.uniform_float("color", fillcolor) + batch = batch_for_shader(shader, 'TRI_FAN', {"pos": points}) + batch.draw(shader) # frame if linecolor: - #bgl.glColor4f(*linecolor) - bgl.glBegin(bgl.GL_LINE_STRIP) - for p in points: - bgl.glVertex3f(*p, distance) - bgl.glVertex3f(*points[0], distance) - bgl.glEnd() - bgl.glLineWidth(1) - bgl.glDisable(bgl.GL_BLEND) + shader.uniform_float("color", linecolor) + batch = batch_for_shader(shader, 'LINE_STRIP', {"pos": points}) + batch.draw(shader) def draw_text(text, position, color=(1.0, 1.0, 1.0, 1.0), size=14, dpi=150, font_id=0): @@ -130,9 +124,7 @@ def draw_text(text, position, color=(1.0, 1.0, 1.0, 1.0), size=14, dpi=150, font Args: text: position: - color: (Default value = (1.0) - 1.0: - 1.0): + color: (Default value = (1.0, 1.0, 1.0, 1.0)): size: (Default value = 14) dpi: (Default value = 150) font_id: (Default value = 0) @@ -145,6 +137,22 @@ def draw_text(text, position, color=(1.0, 1.0, 1.0, 1.0), size=14, dpi=150, font blf.size(font_id, size, dpi) blf.draw(font_id, text) +def get_text_width(text, size=14, dpi=150, font_id=0): + """ + + Args: + text: + size: (Default value = 14) + dpi: (Default value = 150) + font_id: (Default value = 0) + + Returns: + + """ + #bgl.glColor4f(*color) + blf.size(font_id, size, dpi) + return blf.dimensions(font_id, text)[0] + def draw_textbox( text, @@ -209,16 +217,17 @@ def draw_message(text, msgtype, slot, opacity=1.0, offset=0): """ blf.size(0, 6, 150) - width = bpy.context.region.width - start = width - blf.dimensions(0, text)[0] - 6 + margin = 2 + width = get_text_width(text, size=6) #bpy.context.region.width + start = 20#width - blf.dimensions(0, text)[0] - 6 points = ( - (start, slotlower[slot]), - (width - 2, slotlower[slot]), - (width - 2, slotlower[slot] + slotheight - 4), - (start, slotlower[slot] + slotheight - 4), + (start - margin, slotlower[slot]), + (width + start + margin, slotlower[slot]), + (width + start + margin, slotlower[slot] + slotheight - 4), + (start - margin, slotlower[slot] + slotheight - 4), ) draw_2dpolygon(points, fillcolor=(*colors[msgtype], 0.2 * opacity)) - draw_text(text, (start + 2, slotlower[slot] + 4), size=6, color=(1, 1, 1, opacity)) + draw_text(text, (start, slotlower[slot] + 4), size=6, color=(1, 1, 1, opacity)) if slot == 0 and offset > 0: # draw_text(str(offset) + ' \u25bc', (start - 30, slotlower[0] + 4), size=6, color=(1, 1, 1, opacity)) draw_text('+' + str(offset), (start - 30, slotlower[0] + 4), size=6, color=(1, 1, 1, 1)) @@ -363,8 +372,6 @@ def draw_callback_2d(self, context): # bgl.glMatrixMode(bgl.GL_MODELVIEW) # bgl.glLoadIdentity() - bgl.glEnable(bgl.GL_BLEND) - # submechanisms if objects and wm.draw_submechanisms: submechanism_roots = [obj for obj in bpy.data.objects if 'submechanism/name' in obj] @@ -452,11 +459,6 @@ def draw_callback_2d(self, context): except IndexError: pass - # restore opengl defaults - bgl.glLineWidth(1) - bgl.glDisable(bgl.GL_BLEND) - #bgl.glColor4f(0.0, 0.0, 0.0, 1.0) - def setProgress(value, info=None): """ From fff43eb33d34333412efe6b799413757dd8f43cd Mon Sep 17 00:00:00 2001 From: Alex Zastrow Date: Tue, 6 Feb 2024 17:44:01 +0100 Subject: [PATCH 2/9] Fix text and 2d polygon color --- phobos/blender/display.py | 41 +++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/phobos/blender/display.py b/phobos/blender/display.py index e107bf88..dee515ec 100644 --- a/phobos/blender/display.py +++ b/phobos/blender/display.py @@ -91,7 +91,7 @@ def to2d(coords): return view3d_utils.location_3d_to_region_2d(*getRegionData(), coords) -def draw_2dpolygon(points, linecolor=None, fillcolor=None, distance=0.2, linewidth=1): +def draw_2dpolygon(left, top, width, height, linecolor=None, fillcolor=None, distance=0.2, linewidth=1): """ Args: @@ -104,17 +104,23 @@ def draw_2dpolygon(points, linecolor=None, fillcolor=None, distance=0.2, linewid Returns: """ + + points = ( + (left, top), + (left + width, top), + (left + width, top + height), + (left, top + height) + ) + # background - shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR') + shader = gpu.shader.from_builtin('2D_SMOOTH_COLOR') + color = len(points)*[fillcolor] if fillcolor: - print("polygon", fillcolor) - shader.uniform_float("color", fillcolor) - batch = batch_for_shader(shader, 'TRI_FAN', {"pos": points}) + batch = batch_for_shader(shader, 'TRI_FAN', {"pos": points, "color": color}) batch.draw(shader) # frame if linecolor: - shader.uniform_float("color", linecolor) - batch = batch_for_shader(shader, 'LINE_STRIP', {"pos": points}) + batch = batch_for_shader(shader, 'LINE_STRIP', {"pos": points, "color": color}) batch.draw(shader) @@ -132,7 +138,7 @@ def draw_text(text, position, color=(1.0, 1.0, 1.0, 1.0), size=14, dpi=150, font Returns: """ - #bgl.glColor4f(*color) + blf.color(font_id, *color) blf.position(font_id, *position, 0.25) blf.size(font_id, size, dpi) blf.draw(font_id, text) @@ -217,17 +223,14 @@ def draw_message(text, msgtype, slot, opacity=1.0, offset=0): """ blf.size(0, 6, 150) - margin = 2 - width = get_text_width(text, size=6) #bpy.context.region.width - start = 20#width - blf.dimensions(0, text)[0] - 6 - points = ( - (start - margin, slotlower[slot]), - (width + start + margin, slotlower[slot]), - (width + start + margin, slotlower[slot] + slotheight - 4), - (start - margin, slotlower[slot] + slotheight - 4), - ) - draw_2dpolygon(points, fillcolor=(*colors[msgtype], 0.2 * opacity)) - draw_text(text, (start, slotlower[slot] + 4), size=6, color=(1, 1, 1, opacity)) + margin = 4 + start = 20 + blocksize = 10 + left, top = start, slotlower[slot] + 4 + width, height = blocksize, blocksize + draw_2dpolygon(left, top, width, height, fillcolor=(*colors[msgtype], 0.2 * opacity)) + draw_text(text, (start + margin + blocksize - 1, slotlower[slot] + 4 - 1), size=6, color=(0, 0, 0, opacity)) + draw_text(text, (start + margin + blocksize, slotlower[slot] + 4), size=6, color=(1, 1, 1, opacity)) if slot == 0 and offset > 0: # draw_text(str(offset) + ' \u25bc', (start - 30, slotlower[0] + 4), size=6, color=(1, 1, 1, opacity)) draw_text('+' + str(offset), (start - 30, slotlower[0] + 4), size=6, color=(1, 1, 1, 1)) From 118c243d85df6bcef41d7e8826e3bba571fabdac Mon Sep 17 00:00:00 2001 From: Alex Zastrow Date: Tue, 6 Feb 2024 18:00:25 +0100 Subject: [PATCH 3/9] Fix 2d polygon color opacity --- phobos/blender/display.py | 1 + 1 file changed, 1 insertion(+) diff --git a/phobos/blender/display.py b/phobos/blender/display.py index dee515ec..6bbd3431 100644 --- a/phobos/blender/display.py +++ b/phobos/blender/display.py @@ -113,6 +113,7 @@ def draw_2dpolygon(left, top, width, height, linecolor=None, fillcolor=None, dis ) # background + gpu.state.blend_set("ALPHA") shader = gpu.shader.from_builtin('2D_SMOOTH_COLOR') color = len(points)*[fillcolor] if fillcolor: From 403eade147b9b44b0486cb9b20dc73c3e5bb822d Mon Sep 17 00:00:00 2001 From: Alex Zastrow Date: Tue, 6 Feb 2024 18:58:16 +0100 Subject: [PATCH 4/9] Fix draw_joint and draw_textbox --- phobos/blender/display.py | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/phobos/blender/display.py b/phobos/blender/display.py index 6bbd3431..0fd06272 100644 --- a/phobos/blender/display.py +++ b/phobos/blender/display.py @@ -200,13 +200,9 @@ def draw_textbox( origin = origin + Vector((-width - 2 * hborder, 0)) + offset else: origin = origin + offset - points = ( - origin + Vector((-hborder, -vborder * 1.5)), - origin + Vector((width + hborder, -vborder * 1.5)), - origin + Vector((width + hborder, height + vborder)), - origin + Vector((-hborder, height + vborder)), - ) - draw_2dpolygon(points, fillcolor=backgroundcolor, linecolor=textcolor, linewidth=linewidth) + l, t = origin[0]-hborder, origin[1]-vborder * 1.5 + w, h = width + 2*hborder, height + 2.5*vborder + draw_2dpolygon(l, t, w, h, fillcolor=backgroundcolor, linecolor=textcolor, linewidth=linewidth) draw_text(text, position=origin, size=textsize, color=textcolor) @@ -286,12 +282,12 @@ def draw_joint(joint, length): axis = joint.matrix_world @ (length * joint.data.bones[0].vector.normalized()) endpoint = axis - #bgl.glColor4f(0.0, 1.0, 0.0, 0.5) - bgl.glLineWidth(2) - bgl.glBegin(bgl.GL_LINE_STRIP) - bgl.glVertex3f(*origin) - bgl.glVertex3f(*endpoint) - bgl.glEnd() + points = (origin, endpoint) + color = 2*[(0.0, 1.0, 0.0, 0.5)] + shader = gpu.shader.from_builtin("3D_SMOOTH_COLOR") + batch = batch_for_shader(shader, "LINE_STRIP", {"pos": points, "color": color}) + batch.draw(shader) + def draw_path(path, color=colors['white'], dim3=False, width=4): @@ -333,11 +329,10 @@ def draw_callback_3d(self, context): Returns: """ - active = context.object selected = context.selected_objects wm = context.window_manager - bgl.glEnable(bgl.GL_BLEND) + gpu.state.blend_set("ALPHA") # joint axes if len(selected) > 0: @@ -345,11 +340,6 @@ def draw_callback_3d(self, context): for j in [o for o in selected if o.phobostype == 'link']: draw_joint(j, wm.jointaxes_length) - # restore opengl defaults - bgl.glLineWidth(1) - bgl.glDisable(bgl.GL_BLEND) - #bgl.glColor4f(0.0, 0.0, 0.0, 1.0) - def draw_callback_2d(self, context): """Callback function for 2d drawing. From b1aaa800420c8223ca716800ad2a9fa1f39b96d5 Mon Sep 17 00:00:00 2001 From: Alex Zastrow Date: Tue, 6 Feb 2024 19:18:12 +0100 Subject: [PATCH 5/9] Update display.py --- phobos/blender/display.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/phobos/blender/display.py b/phobos/blender/display.py index 0fd06272..8c11b023 100644 --- a/phobos/blender/display.py +++ b/phobos/blender/display.py @@ -91,14 +91,16 @@ def to2d(coords): return view3d_utils.location_3d_to_region_2d(*getRegionData(), coords) -def draw_2dpolygon(left, top, width, height, linecolor=None, fillcolor=None, distance=0.2, linewidth=1): +def draw_2dpolygon(left, top, width, height, linecolor=None, fillcolor=None, linewidth=1): """ Args: - points: + left: + top: + width: + height: linecolor: (Default value = None) fillcolor: (Default value = None) - distance: (Default value = 0.2) linewidth: (Default value = 1) Returns: @@ -289,7 +291,6 @@ def draw_joint(joint, length): batch.draw(shader) - def draw_path(path, color=colors['white'], dim3=False, width=4): """ From a2d8287fecc318ec4a8c4a08fc08aa6628cb2a67 Mon Sep 17 00:00:00 2001 From: Alex Zastrow Date: Tue, 6 Feb 2024 20:28:06 +0100 Subject: [PATCH 6/9] Fix draw submechanisms --- phobos/blender/display.py | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/phobos/blender/display.py b/phobos/blender/display.py index 8c11b023..23355051 100644 --- a/phobos/blender/display.py +++ b/phobos/blender/display.py @@ -307,18 +307,20 @@ def draw_path(path, color=colors['white'], dim3=False, width=4): for e in range(len(path)): origins.append(path[e].matrix_world.to_translation()) - bgl.glEnable(bgl.GL_BLEND) - bgl.glLineWidth(width) - - bgl.glBegin(bgl.GL_LINE_STRIP) - #bgl.glColor4f(*color) + gpu.state.blend_set("ALPHA") + points = [] for o in origins: if dim3: - bgl.glVertex3f(o) + points.append(o) else: - bgl.glVertex2f(*to2d(o)) - bgl.glEnd() - bgl.glDisable(bgl.GL_BLEND) + points.append(to2d(o)) + c = len(points)*[color] + if dim3: + shader = gpu.shader.from_builtin("3D_SMOOTH_COLOR") + else: + shader = gpu.shader.from_builtin("2D_SMOOTH_COLOR") + batch = batch_for_shader(shader, "LINE_STRIP", {"pos": points, "color": c}) + batch.draw(shader) def draw_callback_3d(self, context): @@ -369,11 +371,12 @@ def draw_callback_2d(self, context): # submechanisms if objects and wm.draw_submechanisms: - submechanism_roots = [obj for obj in bpy.data.objects if 'submechanism/name' in obj] + submechanism_roots = [obj for obj in bpy.data.objects if obj.phobostype == "submechanism"] # draw spanning trees for root in submechanism_roots: - if 'submechanism/spanningtree' in root: - if set(root['submechanism/spanningtree']).intersection( + if 'jointnames_spanningtree' in root: + spanningTree = root['jointnames_spanningtree'] + if set(spanningTree).intersection( set(bpy.context.selected_objects) ): linecolor = colors['submechanism'] @@ -381,15 +384,15 @@ def draw_callback_2d(self, context): else: linecolor = (*colors['submechanism'][:3], 0.4) linewidth = 3 - draw_path(root['submechanism/spanningtree'], color=linecolor, width=linewidth) + draw_path(spanningTree, color=linecolor, width=linewidth) # joint['submechanism/independent'], # joint['submechanism/active']) avgpos = Vector() - for obj in root['submechanism/spanningtree']: + for obj in spanningTree: avgpos += obj.matrix_world.translation - origin = to2d(avgpos / len(root['submechanism/spanningtree'])) + origin = to2d(avgpos / len(spanningTree)) draw_textbox( - root['submechanism/name'], + root['name'], origin, textsize=8, textcolor=linecolor, From 3dc29130e9e0dbd187caeb801f82249c76366f06 Mon Sep 17 00:00:00 2001 From: Alex Zastrow Date: Wed, 7 Feb 2024 11:59:06 +0100 Subject: [PATCH 7/9] Fix draw interfaces --- phobos/blender/display.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phobos/blender/display.py b/phobos/blender/display.py index 23355051..66e59d3e 100644 --- a/phobos/blender/display.py +++ b/phobos/blender/display.py @@ -434,7 +434,7 @@ def draw_callback_2d(self, context): nUtils.getObjectName(interface), to2d(interface.matrix_world.translation), textsize=6, - textcolor=(*color, 1.0 if interface.show_name else 0.4), + textcolor=(*color[0:3], 1.0 if interface.show_name else 0.4), backgroundcolor=(*bgcolor, 1.0) if interface.show_name else colors['background'], linewidth=3 if interface.show_name else 2, ) From e1d8ea874ca1804168d25f642a3561b54d2ee5a8 Mon Sep 17 00:00:00 2001 From: Alex Zastrow Date: Wed, 7 Feb 2024 14:14:26 +0100 Subject: [PATCH 8/9] Remove unused imports --- phobos/blender/display.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/phobos/blender/display.py b/phobos/blender/display.py index 66e59d3e..bfc9a242 100644 --- a/phobos/blender/display.py +++ b/phobos/blender/display.py @@ -14,8 +14,6 @@ from gpu_extras.batch import batch_for_shader import blf import bpy -from bpy.props import BoolProperty -from bpy.types import Operator from bpy_extras import view3d_utils from mathutils import Vector From 13847b78d4f3c5021eb7c82128b3e86b76d95736 Mon Sep 17 00:00:00 2001 From: Alex Zastrow Date: Wed, 7 Feb 2024 14:30:04 +0100 Subject: [PATCH 9/9] Rename messages offset to scroll --- phobos/blender/display.py | 19 ++++++++++--------- phobos/blender/phobosgui.py | 8 ++++---- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/phobos/blender/display.py b/phobos/blender/display.py index bfc9a242..edc68916 100644 --- a/phobos/blender/display.py +++ b/phobos/blender/display.py @@ -206,7 +206,7 @@ def draw_textbox( draw_text(text, position=origin, size=textsize, color=textcolor) -def draw_message(text, msgtype, slot, opacity=1.0, offset=0): +def draw_message(text, msgtype, slot, opacity=1.0, scroll=0): """ Args: @@ -214,7 +214,7 @@ def draw_message(text, msgtype, slot, opacity=1.0, offset=0): msgtype: slot: opacity: (Default value = 1.0) - offset: (Default value = 0) + scroll: (Default value = 0) Returns: @@ -222,15 +222,16 @@ def draw_message(text, msgtype, slot, opacity=1.0, offset=0): blf.size(0, 6, 150) margin = 4 start = 20 + if slot == 0 and scroll > 0: + start = start + get_text_width('+' + str(scroll), size=6) + margin blocksize = 10 left, top = start, slotlower[slot] + 4 width, height = blocksize, blocksize draw_2dpolygon(left, top, width, height, fillcolor=(*colors[msgtype], 0.2 * opacity)) draw_text(text, (start + margin + blocksize - 1, slotlower[slot] + 4 - 1), size=6, color=(0, 0, 0, opacity)) draw_text(text, (start + margin + blocksize, slotlower[slot] + 4), size=6, color=(1, 1, 1, opacity)) - if slot == 0 and offset > 0: - # draw_text(str(offset) + ' \u25bc', (start - 30, slotlower[0] + 4), size=6, color=(1, 1, 1, opacity)) - draw_text('+' + str(offset), (start - 30, slotlower[0] + 4), size=6, color=(1, 1, 1, 1)) + if slot == 0 and scroll > 0: + draw_text('+' + str(scroll), (10, slotlower[0] + 4), size=6, color=(1, 1, 1, 1)) def draw_progressbar(value): @@ -445,13 +446,13 @@ def draw_callback_2d(self, context): if wm.draw_messages: for m in range(wm.phobos_msg_count): opacity = 1.0 - if 1 >= m <= wm.phobos_msg_offset - 1 or m >= wm.phobos_msg_count - 2: + if 1 >= m <= wm.phobos_msg_scroll - 1 or m >= wm.phobos_msg_count - 2: opacity = 0.5 - if wm.phobos_msg_offset > 1 > m or m >= wm.phobos_msg_count - 1: + if wm.phobos_msg_scroll > 1 > m or m >= wm.phobos_msg_count - 1: opacity = 0.1 try: - msg = messages[m + wm.phobos_msg_offset] - draw_message(msg['text'], msg['type'], m, opacity, offset=wm.phobos_msg_offset) + msg = messages[m + wm.phobos_msg_scroll] + draw_message(msg['text'], msg['type'], m, opacity, scroll=wm.phobos_msg_scroll) except IndexError: pass diff --git a/phobos/blender/phobosgui.py b/phobos/blender/phobosgui.py index d5c571f9..bf086e8b 100644 --- a/phobos/blender/phobosgui.py +++ b/phobos/blender/phobosgui.py @@ -1662,7 +1662,7 @@ def draw(self, context): dc1.prop(wm, "draw_progress") dc2.prop(wm, "draw_messages") dc2.prop(wm, 'phobos_msg_count') - dc2.prop(wm, 'phobos_msg_offset') + dc2.prop(wm, 'phobos_msg_scroll') layout.separator() @@ -1812,12 +1812,12 @@ def register(): description="How many Phobos log messages to show on screen", ) - bpy.types.WindowManager.phobos_msg_offset = IntProperty( - name='offset', + bpy.types.WindowManager.phobos_msg_scroll = IntProperty( + name='scroll', default=0, min=0, max=50, - description="The Phobos log message index to start with", + description="Use this to scroll through the messages", ) bpy.types.WindowManager.draw_progress = BoolProperty(name='Progress', default=True)