Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blender Gui Widget update #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 8 additions & 2 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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__":
Expand Down
Binary file modified __pycache__/__init__.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/__init__.cpython-39.pyc
Binary file not shown.
Binary file added __pycache__/bl_ui_background.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/bl_ui_background.cpython-39.pyc
Binary file not shown.
Binary file modified __pycache__/bl_ui_button.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/bl_ui_button.cpython-39.pyc
Binary file not shown.
Binary file modified __pycache__/bl_ui_checkbox.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/bl_ui_checkbox.cpython-39.pyc
Binary file not shown.
Binary file modified __pycache__/bl_ui_drag_panel.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/bl_ui_drag_panel.cpython-39.pyc
Binary file not shown.
Binary file modified __pycache__/bl_ui_draw_op.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/bl_ui_draw_op.cpython-39.pyc
Binary file not shown.
Binary file modified __pycache__/bl_ui_label.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/bl_ui_label.cpython-39.pyc
Binary file not shown.
Binary file modified __pycache__/bl_ui_slider.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/bl_ui_slider.cpython-39.pyc
Binary file not shown.
Binary file modified __pycache__/bl_ui_up_down.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/bl_ui_up_down.cpython-39.pyc
Binary file not shown.
Binary file modified __pycache__/bl_ui_widget.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/bl_ui_widget.cpython-39.pyc
Binary file not shown.
Binary file modified __pycache__/drag_panel_op.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/drag_panel_op.cpython-39.pyc
Binary file not shown.
Binary file added __pycache__/neo_buts_op.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/neo_buts_op.cpython-39.pyc
Binary file not shown.
Binary file added bitmap.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bitmap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
124 changes: 124 additions & 0 deletions bl_ui_background.py
Original file line number Diff line number Diff line change
@@ -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
46 changes: 28 additions & 18 deletions bl_ui_widget.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import gpu
import bgl
import bpy

from gpu_extras.batch import batch_for_shader

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -175,4 +185,4 @@ def mouse_exit(self, event, x, y):
self.call_mouse_exit()

def mouse_move(self, x, y):
pass
pass
101 changes: 101 additions & 0 deletions neo_buts_op.py
Original file line number Diff line number Diff line change
@@ -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
Binary file added screen_2021-06-30 03-14-29.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screen_2021-06-30 03-15-28.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.