diff --git a/README.md b/README.md index c63030c..1091dfb 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,7 @@ # Blender UI Widgets + +Hi! I admire to present my latest fixes and extensions for the Blender Gui Widgets Addon, fistly made by Jayanam. + Addon with UI Widgets like a dragable panel, buttons or sliders for Blender 2.8. For drawing the GPU module of Blender 2.8 is used. diff --git a/__init__.py b/__init__.py index 6bb8538..46e88b2 100644 --- a/__init__.py +++ b/__init__.py @@ -13,12 +13,18 @@ from bpy.props import * from . drag_panel_op import DP_OT_draw_operator - +from . neo_buts_op import DP_OT_draw_operator_fork_test addon_keymaps = [] +def menu_func_neo_butt_1(self, context): + layout = self.layout + oper = self.layout.operator("object.draw_operator_fork_test", text="NEO_Buttons", icon="MONKEY") + def register(): bpy.utils.register_class(DP_OT_draw_operator) + bpy.utils.register_class(DP_OT_draw_operator_fork_test) + bpy.types.VIEW3D_MT_view.append(menu_func_neo_butt_1) kcfg = bpy.context.window_manager.keyconfigs.addon if kcfg: km = kcfg.keymaps.new(name='3D View', space_type='VIEW_3D') @@ -31,7 +37,7 @@ def unregister(): for km, kmi in addon_keymaps: km.keymap_items.remove(kmi) addon_keymaps.clear() - + bpy.utils.unregister_class(DP_OT_draw_operator_fork_test) bpy.utils.unregister_class(DP_OT_draw_operator) if __name__ == "__main__": diff --git a/__pycache__/__init__.cpython-37.pyc b/__pycache__/__init__.cpython-37.pyc index 0f85de4..cb13c90 100644 Binary files a/__pycache__/__init__.cpython-37.pyc and b/__pycache__/__init__.cpython-37.pyc differ diff --git a/__pycache__/__init__.cpython-39.pyc b/__pycache__/__init__.cpython-39.pyc new file mode 100644 index 0000000..0ab9781 Binary files /dev/null and b/__pycache__/__init__.cpython-39.pyc differ diff --git a/__pycache__/bl_ui_background.cpython-37.pyc b/__pycache__/bl_ui_background.cpython-37.pyc new file mode 100644 index 0000000..34df227 Binary files /dev/null and b/__pycache__/bl_ui_background.cpython-37.pyc differ diff --git a/__pycache__/bl_ui_background.cpython-39.pyc b/__pycache__/bl_ui_background.cpython-39.pyc new file mode 100644 index 0000000..80a8efb Binary files /dev/null and b/__pycache__/bl_ui_background.cpython-39.pyc differ diff --git a/__pycache__/bl_ui_button.cpython-37.pyc b/__pycache__/bl_ui_button.cpython-37.pyc index 6debdc6..a762e4b 100644 Binary files a/__pycache__/bl_ui_button.cpython-37.pyc and b/__pycache__/bl_ui_button.cpython-37.pyc differ diff --git a/__pycache__/bl_ui_button.cpython-39.pyc b/__pycache__/bl_ui_button.cpython-39.pyc new file mode 100644 index 0000000..ba5928b Binary files /dev/null and b/__pycache__/bl_ui_button.cpython-39.pyc differ diff --git a/__pycache__/bl_ui_checkbox.cpython-37.pyc b/__pycache__/bl_ui_checkbox.cpython-37.pyc index 899c165..312bce9 100644 Binary files a/__pycache__/bl_ui_checkbox.cpython-37.pyc and b/__pycache__/bl_ui_checkbox.cpython-37.pyc differ diff --git a/__pycache__/bl_ui_checkbox.cpython-39.pyc b/__pycache__/bl_ui_checkbox.cpython-39.pyc new file mode 100644 index 0000000..c5c1896 Binary files /dev/null and b/__pycache__/bl_ui_checkbox.cpython-39.pyc differ diff --git a/__pycache__/bl_ui_drag_panel.cpython-37.pyc b/__pycache__/bl_ui_drag_panel.cpython-37.pyc index 8b5c4b2..0bfcafd 100644 Binary files a/__pycache__/bl_ui_drag_panel.cpython-37.pyc and b/__pycache__/bl_ui_drag_panel.cpython-37.pyc differ diff --git a/__pycache__/bl_ui_drag_panel.cpython-39.pyc b/__pycache__/bl_ui_drag_panel.cpython-39.pyc new file mode 100644 index 0000000..5092776 Binary files /dev/null and b/__pycache__/bl_ui_drag_panel.cpython-39.pyc differ diff --git a/__pycache__/bl_ui_draw_op.cpython-37.pyc b/__pycache__/bl_ui_draw_op.cpython-37.pyc index 8e72439..fe9d043 100644 Binary files a/__pycache__/bl_ui_draw_op.cpython-37.pyc and b/__pycache__/bl_ui_draw_op.cpython-37.pyc differ diff --git a/__pycache__/bl_ui_draw_op.cpython-39.pyc b/__pycache__/bl_ui_draw_op.cpython-39.pyc new file mode 100644 index 0000000..e0f97c4 Binary files /dev/null and b/__pycache__/bl_ui_draw_op.cpython-39.pyc differ diff --git a/__pycache__/bl_ui_label.cpython-37.pyc b/__pycache__/bl_ui_label.cpython-37.pyc index 8e06b06..e374d23 100644 Binary files a/__pycache__/bl_ui_label.cpython-37.pyc and b/__pycache__/bl_ui_label.cpython-37.pyc differ diff --git a/__pycache__/bl_ui_label.cpython-39.pyc b/__pycache__/bl_ui_label.cpython-39.pyc new file mode 100644 index 0000000..0d6f6ab Binary files /dev/null and b/__pycache__/bl_ui_label.cpython-39.pyc differ diff --git a/__pycache__/bl_ui_slider.cpython-37.pyc b/__pycache__/bl_ui_slider.cpython-37.pyc index ea65be4..d57d8c4 100644 Binary files a/__pycache__/bl_ui_slider.cpython-37.pyc and b/__pycache__/bl_ui_slider.cpython-37.pyc differ diff --git a/__pycache__/bl_ui_slider.cpython-39.pyc b/__pycache__/bl_ui_slider.cpython-39.pyc new file mode 100644 index 0000000..afd9613 Binary files /dev/null and b/__pycache__/bl_ui_slider.cpython-39.pyc differ diff --git a/__pycache__/bl_ui_up_down.cpython-37.pyc b/__pycache__/bl_ui_up_down.cpython-37.pyc index 3628fdf..5ef83c1 100644 Binary files a/__pycache__/bl_ui_up_down.cpython-37.pyc and b/__pycache__/bl_ui_up_down.cpython-37.pyc differ diff --git a/__pycache__/bl_ui_up_down.cpython-39.pyc b/__pycache__/bl_ui_up_down.cpython-39.pyc new file mode 100644 index 0000000..2c07a29 Binary files /dev/null and b/__pycache__/bl_ui_up_down.cpython-39.pyc differ diff --git a/__pycache__/bl_ui_widget.cpython-37.pyc b/__pycache__/bl_ui_widget.cpython-37.pyc index cd0518c..52f4215 100644 Binary files a/__pycache__/bl_ui_widget.cpython-37.pyc and b/__pycache__/bl_ui_widget.cpython-37.pyc differ diff --git a/__pycache__/bl_ui_widget.cpython-39.pyc b/__pycache__/bl_ui_widget.cpython-39.pyc new file mode 100644 index 0000000..c72a974 Binary files /dev/null and b/__pycache__/bl_ui_widget.cpython-39.pyc differ diff --git a/__pycache__/drag_panel_op.cpython-37.pyc b/__pycache__/drag_panel_op.cpython-37.pyc index 01405df..772d056 100644 Binary files a/__pycache__/drag_panel_op.cpython-37.pyc and b/__pycache__/drag_panel_op.cpython-37.pyc differ diff --git a/__pycache__/drag_panel_op.cpython-39.pyc b/__pycache__/drag_panel_op.cpython-39.pyc new file mode 100644 index 0000000..5aed15d Binary files /dev/null and b/__pycache__/drag_panel_op.cpython-39.pyc differ diff --git a/__pycache__/neo_buts_op.cpython-37.pyc b/__pycache__/neo_buts_op.cpython-37.pyc new file mode 100644 index 0000000..e024971 Binary files /dev/null and b/__pycache__/neo_buts_op.cpython-37.pyc differ diff --git a/__pycache__/neo_buts_op.cpython-39.pyc b/__pycache__/neo_buts_op.cpython-39.pyc new file mode 100644 index 0000000..c3d69f5 Binary files /dev/null and b/__pycache__/neo_buts_op.cpython-39.pyc differ diff --git a/bitmap.jpg b/bitmap.jpg new file mode 100644 index 0000000..e03d65a Binary files /dev/null and b/bitmap.jpg differ diff --git a/bitmap.png b/bitmap.png new file mode 100644 index 0000000..a641b5d Binary files /dev/null and b/bitmap.png differ diff --git a/bl_ui_background.py b/bl_ui_background.py new file mode 100644 index 0000000..f5369f7 --- /dev/null +++ b/bl_ui_background.py @@ -0,0 +1,124 @@ +from . bl_ui_widget import * + +import blf +import bpy +from bpy.types import Operator +import sys +import os + +class BL_UI_Background(BL_UI_Widget): + def __init__(self, x, y, width, height): + super().__init__(x, y, width, height) + self._text_color = (1.0, 1.0, 1.0, 1.0) + self._hover_bg_color = (0.5, 0.5, 0.5, 1.0) + self._select_bg_color = (0.7, 0.7, 0.7, 1.0) + + self._text = "Test" + self._text_size = 16 + self._textpos = (x, y) + + self.__state = 0 + self.__image = None + self.__image_size = (24, 24) + self.__image_position = (4, 2) + @property + def hover_bg_color(self): + return self._hover_bg_color + + @hover_bg_color.setter + def hover_bg_color(self, value): + self._hover_bg_color = value + + @property + def select_bg_color(self): + return self._select_bg_color + + @select_bg_color.setter + def select_bg_color(self, value): + self._select_bg_color = value + + def set_image_size(self, imgage_size): + self.__image_size = imgage_size + + def set_image_position(self, image_position): + self.__image_position = image_position + + def set_image(self, rel_filepath): + try: + self.__image = bpy.data.images.load(rel_filepath, check_existing=True) + self.__image.gl_load() + print (self.__image) + except: + pass + + def update(self, x, y): + super().update(x, y) + self._textpos = [x, y] + + def draw(self): + if not self.visible: + return + + area_height = self.get_area_height() + + self.shader.bind() + + self.set_colors() + + bgl.glEnable(bgl.GL_BLEND) + + self.batch_panel.draw(self.shader) + + self.draw_image() + + bgl.glDisable(bgl.GL_BLEND) + + # Draw text + #self.draw_text(area_height) + + def set_colors(self): + color = self._bg_color + text_color = self._text_color + + # pressed + if self.__state == 1: + color = self._select_bg_color + + # hover + elif self.__state == 2: + color = self._hover_bg_color + + self.shader.uniform_float("color", color) + + def draw_image(self): + if self.__image is not None: + try: + y_screen_flip = self.get_area_height() - self.y_screen + + off_x, off_y = self.__image_position + sx, sy = self.__image_size + + # bottom left, top left, top right, bottom right + vertices = ( + (self.x_screen + off_x, y_screen_flip - off_y), + (self.x_screen + off_x, y_screen_flip - sy - off_y), + (self.x_screen + off_x + sx, y_screen_flip - sy - off_y), + (self.x_screen + off_x + sx, y_screen_flip - off_y)) + + self.shader_img = gpu.shader.from_builtin('2D_IMAGE') + self.batch_img = batch_for_shader(self.shader_img, 'TRI_FAN', + { "pos" : vertices, + "texCoord": ((0, 1), (0, 0), (1, 0), (1, 1)) + },) + + bgl.glActiveTexture(bgl.GL_TEXTURE0) + bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.__image.bindcode) + + self.shader_img.bind() + self.shader_img.uniform_int("image", 0) + self.batch_img.draw(self.shader_img) + return True + except: + pass + + return False diff --git a/bl_ui_widget.py b/bl_ui_widget.py index 6a713ef..faf8811 100644 --- a/bl_ui_widget.py +++ b/bl_ui_widget.py @@ -1,5 +1,6 @@ import gpu import bgl +import bpy from gpu_extras.batch import batch_for_shader @@ -51,15 +52,19 @@ def tag(self, value): self._tag = value def draw(self): - if not self.visible: + if bpy.context != None and bpy.context == bpy.context: + if not self.visible: + return + + self.shader.bind() + self.shader.uniform_float("color", self._bg_color) + + bgl.glEnable(bgl.GL_BLEND) + self.batch_panel.draw(self.shader) + bgl.glDisable(bgl.GL_BLEND) + else: return - self.shader.bind() - self.shader.uniform_float("color", self._bg_color) - - bgl.glEnable(bgl.GL_BLEND) - self.batch_panel.draw(self.shader) - bgl.glDisable(bgl.GL_BLEND) def init(self, context): self.context = context @@ -125,18 +130,23 @@ def get_input_keys(self) : return [] def get_area_height(self): - return self.context.area.height + if self.context.area != None: + return self.context.area.height + else: + return 10 def is_in_rect(self, x, y): - area_height = self.get_area_height() - - widget_y = area_height - self.y_screen - if ( - (self.x_screen <= x <= (self.x_screen + self.width)) and - (widget_y >= y >= (widget_y - self.height)) - ): - return True - + #Add check if the user had change workspace or layout + if bpy.context != None and bpy.context == self.context and self.context.area !=None: + area_height = self.get_area_height() + + widget_y = area_height - self.y_screen + if ( + (self.x_screen <= x <= (self.x_screen + self.width)) and + (widget_y >= y >= (widget_y - self.height)) + ): + return True + return False return False def text_input(self, event): @@ -175,4 +185,4 @@ def mouse_exit(self, event, x, y): self.call_mouse_exit() def mouse_move(self, x, y): - pass \ No newline at end of file + pass diff --git a/neo_buts_op.py b/neo_buts_op.py new file mode 100644 index 0000000..4eade8b --- /dev/null +++ b/neo_buts_op.py @@ -0,0 +1,101 @@ +import blf +import bpy +from bpy.types import Operator +import sys +import os + +from . bl_ui_label import * +from . bl_ui_button import * +from . bl_ui_checkbox import * +from . bl_ui_slider import * +from . bl_ui_up_down import * +from . bl_ui_drag_panel import * +from . bl_ui_draw_op import * +from . bl_ui_background import * + + +bl_info = { + "name": "BL UI Widgets_TEST_NEO", + "description": "UI Widgets to draw in the 3D view", + "author": "Jayanam, Olkeyn", + "version": (0, 6, 4, 2), + "blender": (2, 80, 0), + "location": "View3D", + "category": "Object"} + +# Blender imports + +from bpy.props import * +from bpy_extras.object_utils import AddObjectHelper +my_fork_addon_keymaps = [] +DEFAULT_WIDGET_AREA = None +class DP_OT_draw_operator_fork_test(BL_UI_OT_draw_operator): + bl_idname = "object.draw_operator_fork_test" + bl_label = "bl ui widgets custom operator" + bl_description = "Demo operator for bl ui widgets" + bl_options = {'REGISTER'} + def __init__(self): + print ('self ',self) + if bpy.context.screen != None: + print ("test_init") + super().__init__() + start_sreen = bpy.context.screen + areas = [] + start_area = None + for ar in start_sreen.areas.items(): # Checking if our layout have such area for our widgets + if ar[1].type == 'VIEW_3D': + areas.append(ar[1]) + start_area = ar[1] + DEFAULT_WIDGET_AREA = ar[1] + self.backgroundtest = BL_UI_Background (0, 0, 1, 1) + #Position and size of the container. It means that you can find a point where you cannot to set the cursor or make selection by mouse + script_file = os.path.realpath(__file__) + directory = os.path.dirname(script_file) + image_path = directory+"//bitmap.png" + print ('image', self.backgroundtest.set_image(image_path)) # + self.backgroundtest.set_image_size((start_area.width-1, start_area.height-1)) # adjust size of the picture with the size of working area + self.backgroundtest.set_image_position((0,0)) + self.backgroundtest.bg_color = (0.2, 0.9, 0.2, 0.5) + self.button0 = BL_UI_Button(100, start_area.height-55, 100, 30) + self.button0.bg_color = (0.9, 0.2, 0.2, 0.5) + self.button0.text = 'NEO' + self.button0.set_mouse_down(self.button0_press) + + self.button1 = BL_UI_Button(300, start_area.height-55, 100, 30) + self.button1.bg_color = (0.2, 0.9, 0.2, 0.5) + self.button1.text = 'TRINITY' + self.button1.set_mouse_down(self.button1_press) + + self.button2 = BL_UI_Button(500, start_area.height-55, 100, 30) + self.button2.bg_color = (0.2, 0.2, 0.9, 0.5) + self.button2.text = 'MORPHEUS' + self.button2.set_mouse_down(self.button2_press) + + self.label = BL_UI_Label(20, 10, 100, 15) + self.label.text = "Size:" + self.label2 = BL_UI_Label(10, 20, 50, 15) + self.label2.text = "Size:" + else: + print ('We have checked that layout not load yet and do nothing to avoid errors.') + def on_invoke(self, context, event): + # Add new widgets here (TODO: perhaps a better, more automated solution?) + widgets = [self.button0,self.button1,self.button2,self.backgroundtest] + self.init_widgets(context, widgets) + def button0_press(self, widget): + print("Button '{0}' is pressed".format(widget.text)) + gg = bpy.ops.mesh.primitive_ico_sphere_add() + bpy.ops.ed.undo_push() + # very complicated moment, without it you can find disapointment to do almos all undo + return gg + def button1_press(self, widget): + print("Button '{0}' is pressed".format(widget.text)) + gg = bpy.ops.mesh.primitive_cube_add() + bpy.ops.ed.undo_push() + # very complicated moment, without it you can find disapointment to do almos all undo + return gg + def button2_press(self, widget): + print("Button '{0}' is pressed".format(widget.text)) + gg = bpy.ops.mesh.primitive_monkey_add() + bpy.ops.ed.undo_push() + # very complicated moment, without it you can find disapointment to do almos all undo + return gg diff --git a/screen_2021-06-30 03-14-29.png b/screen_2021-06-30 03-14-29.png new file mode 100644 index 0000000..c65c3cd Binary files /dev/null and b/screen_2021-06-30 03-14-29.png differ diff --git a/screen_2021-06-30 03-15-28.png b/screen_2021-06-30 03-15-28.png new file mode 100644 index 0000000..4931d09 Binary files /dev/null and b/screen_2021-06-30 03-15-28.png differ