The code:
import bpy, bmesh
import bgl
import blf
import gpu
from gpu_extras.batch import batch_for_shader
vertices = (
(60, 425), (310, 425),
(60, 580), (310, 580))
indices = (
(0, 1, 2), (2, 1, 3))
shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR')
batch = batch_for_shader(shader, 'TRIS', {"pos": vertices}, indices=indices)
def draw_bevel(self, context):
#box
shader.bind()
shader.uniform_float("color", (0.3, 0.3, 0.3, 0.8))
batch.draw(shader)
#text
font_id = 0
x_offset = 0
y_offset = 0
active = context.active_object
bevelWV = active.modifiers["Bevel"].width
bevelWV = round(bevelWV, 4)
bevelSG = active.modifiers["Bevel"].segments
bevelSG = round(bevelSG, 4)
blf.color(font_id, 0, 0.292, 1, 0.7)
blf.position(font_id, 78, 510, 0)
blf.size(font_id, 27, 176)
blf.draw(font_id, "BEVEL" )
blf.color(font_id, 0, 1, 0.232, 0.7)
blf.position(font_id, 83, 480, 0)
blf.size(font_id, 27, 81)
blf.draw(font_id, "WIDTH" )
blf.color(font_id, 0, 1, 0.232, 0.7)
blf.position(font_id, 83, 450, 0)
blf.size(font_id, 27, 81)
blf.draw(font_id, "SEGS" )
blf.color(font_id, 1, 1, 1, 0.7)
blf.position(font_id, 195, 480, 0)
blf.size(font_id, 27, 81)
blf.draw(font_id, str(bevelWV))
blf.color(font_id, 1, 1, 1, 0.7)
blf.position(font_id, 195, 450, 0)
blf.size(font_id, 27, 81)
blf.draw(font_id, str(bevelSG))
class BevelSetModal(bpy.types.Operator):
"""Move an object with the mouse, example"""
bl_idname = "bevel.set_operator"
bl_label = "Bevel Set modal"
first_mouse_x = IntProperty()
first_value = FloatProperty()
@classmethod
def poll(cls, context):
mod_types = set()
for o in context.selected_objects:
for m in o.modifiers:
mod_types.add(m.type)
return 'BEVEL' in mod_types and context.object.type == 'MESH'
def modal(self, context, event):
context.area.tag_redraw()
if event.type == 'MOUSEMOVE':
self.mouse_path.append((event.mouse_region_x, event.mouse_region_x))
delta = self.first_mouse_x - event.mouse_x
bpy.context.object.modifiers["Bevel"].width = self.first_value + delta * -0.001
elif event.type == 'MOUSEMOVE'+'P':
self.mouse_path.append((event.mouse_region_x, event.mouse_region_x))
delta = self.first_mouse_x - event.mouse_x
bpy.context.object.modifiers["Bevel"].profile = self.first_value + delta * -0.001
elif event.type == 'V' and event.value=='PRESS':
bpy.context.object.modifiers["Bevel"].use_only_vertices = True
elif event.type == "WHEELUPMOUSE":
bpy.context.object.modifiers["Bevel"].segments += 1
elif event.type == "WHEELDOWNMOUSE":
bpy.context.object.modifiers["Bevel"].segments -= 1
elif event.type == 'LEFTMOUSE':
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
return {'FINISHED'}
elif event.type in {'RIGHTMOUSE', 'ESC'}:
bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
bpy.context.object.modifiers["Bevel"].width = self.first_value
return {'CANCELLED'}
return {'RUNNING_MODAL'}
def invoke(self, context, event):
if context.area.type == 'VIEW_3D':
args = (self, context)
self._handle = bpy.types.SpaceView3D.draw_handler_add(draw_bevel, args, 'WINDOW', 'POST_PIXEL')
self.mouse_path = []
if context.object:
self.first_mouse_x = event.mouse_x
self.first_value = bpy.context.object.modifiers["Bevel"].width
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
else:
self.report({'WARNING'}, "No active object, could not finish")
return {'CANCELLED'}
Basically what this does is draw a 2d polygon box using the new gpu module in 2.8 and some blf text which displays the bevel modifier data, in a modal operator (if there is a bevel modifier present on the mesh object).
As you can see the coordinates of the box and the blf text are both hardcoded. My goal is make the elements get aligned to the right side of the cursor and move along with it. I absolutely have no idea on how to even approach this.
I tried adding the solution provided by @brockmann but this is what happens:
The box is just moving diagonally instead of following the mouse x,y. The example script works fine on its own but the mouse coordinates seem to be confliciting with the modal operator mouse maps.
bpy.context
.. if context is an argument of method use that instead. Also look into using other methods thanob.modifiers["Bevel"]
which throws an error if no modifiers, if modifier named "Bevel" doesn't exist or has been renamed, or is named "Bevel" but isn't a bevel modifier. egmods = [m for m in ob.modifiers if m.type == 'BEVEL']
is a list of all bevel mods on object. Do nothing if there is none. $\endgroup$