Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import bpy
- from bpy_extras import view3d_utils
- from mathutils import Vector
- from bpy.props import FloatProperty, EnumProperty
- bl_info = {
- "name": "Grease Pencil Extras",
- "author": "pongbuster",
- "version": (1, 1),
- "blender": (3, 6, 5),
- "location": "View3D > Sidebar (N)",
- "description": "A collection of utilities to align grease pencil stroke points and adjust stroke hardness, point strength and pressure.",
- "warning": "",
- "doc_url": "",
- "category": "Grease Pencil",
- }
- def init_selected(context, self):
- if context.active_object.type != 'GPENCIL':
- return
- gp = context.active_object.data
- #list comprehension go brrr
- self.selected_points = [p
- for lr in gp.layers
- if not lr.lock and not lr.hide
- for fr in ([fr for fr in lr.frames if fr.select] if gp.use_multiedit else [lr.active_frame]) #Respect layer locking, visibility, and multiframe editing settings
- for s in fr.strokes
- if s.select
- for p in s.points
- if p.select]
- class GPExtras_OT_adjust_stroke_properties(bpy.types.Operator):
- """Middle mouse to adjust selected strokes' hardness.
- Hold CTRL to adjust selected points' pressure(radius) instead.
- Hold SHIFT to adjust selected points' strength instead.
- Left click to apply"""
- bl_idname = "gpextras.adjust_stroke_properties"
- bl_label = "Adjust Stroke or Point Properties"
- bl_options = {'REGISTER', 'UNDO'}
- selected_points = []
- @classmethod
- def poll(self, context):
- return (context.mode == 'SCULPT_GPENCIL' or context.mode == 'EDIT_GPENCIL')
- def modal(self, context, event):
- if event.type == "WHEELUPMOUSE" or event.type == "WHEELDOWNMOUSE":
- incr = -0.01 if event.type == "WHEELDOWNMOUSE" else 0.01
- if event.shift: #Strength
- incr *= 2
- for p in self.selected_points:
- p.strength += incr
- elif event.ctrl: #Pressure/Radius
- incr *= 100
- for p in self.selected_points:
- p.pressure += incr
- else:
- gp = context.active_object.data
- for lr in gp.layers:
- if not lr.lock and not lr.hide:
- frame_list = [fr for fr in lr.frames if fr.select] if gp.use_multiedit else [lr.active_frame]
- for fr in frame_list:
- for s in fr.strokes:
- if s.select:
- s.hardness += incr
- context.area.header_text_set("Hardness: %.4f" % s.hardness)
- elif event.type == "LEFTMOUSE":
- context.area.header_text_set(None)
- context.window.cursor_modal_restore()
- return {'FINISHED'}
- return {'RUNNING_MODAL'}
- def execute(self, context):
- init_selected(context, self)
- context.window.cursor_modal_set("SCROLL_Y")
- context.window_manager.modal_handler_add(self)
- return {'RUNNING_MODAL'}
- class GPExtras_OT_set_stroke_properties(bpy.types.Operator):
- """Sets the selected strokes' hardness, or the selected points' pressure or strength"""
- bl_idname = "gpextras.set_stroke_properties"
- bl_label = "Set Stroke or Point Properties"
- bl_options = {'REGISTER', 'UNDO'}
- selected_points = []
- mode : EnumProperty(items = [('HARDNESS', 'Hardness', "Strokes' Hardness"),
- ('PRESSURE', 'Pressure', "Points' Pressure/Radius"),
- ('STRENGTH', 'Strength', "Points' Strength")], name = "Mode", description = "Which property the operator should set", default = 'HARDNESS')
- @classmethod
- def description(cls, context, properties):
- match properties.mode:
- case 'HARDNESS':
- return "Sets the selected strokes' Hardness to the given value"
- case 'PRESSURE':
- return "Sets the selected points' Pressure(Radius) to the given value"
- case 'STRENGTH':
- return "Sets the selected points' Strength to the given value"
- return "Sets the selected strokes' hardness, or the selected points' pressure or strength"
- @classmethod
- def poll(self, context):
- return (context.mode == 'SCULPT_GPENCIL' or context.mode == 'EDIT_GPENCIL')
- def execute(self, context):
- amount = context.scene.gp_extras_stroke_prop_set_amount
- match self.mode:
- case 'HARDNESS':
- gp = context.active_object.data
- for lr in gp.layers:
- if not lr.lock and not lr.hide:
- frame_list = [fr for fr in lr.frames if fr.select] if gp.use_multiedit else [lr.active_frame]
- for fr in frame_list:
- for s in fr.strokes:
- if s.select:
- s.hardness = amount
- case 'PRESSURE':
- init_selected(context, self)
- for p in self.selected_points:
- p.pressure = amount
- case 'STRENGTH':
- init_selected(context, self)
- for p in self.selected_points:
- p.strength = amount
- return {'FINISHED'}
- class GPExtras_OT_converge_points(bpy.types.Operator):
- arg: bpy.props.StringProperty()
- bl_idname = "gpextras.converge_points"
- bl_label = "Converge Selection"
- bl_options = {'REGISTER', 'UNDO'}
- selectedPoint = None
- selected_points = []
- align : EnumProperty(items = [('CONVERGE', 'Converge', "Converge to point"),
- ('ALIGN_HORIZONTAL', 'Align Horizontal', "Align points horizontally to point"),
- ('ALIGN_VERTICAL', 'Align Vertical', "Align points vertically to point")], name = "Align", description = "Type of converging", default = 'CONVERGE')
- @classmethod
- def description(cls, context, properties):
- match properties.align:
- case 'CONVERGE':
- txt = "Converge"
- case 'ALIGN_HORIZONTAL':
- txt = "Align horizontally"
- case 'ALIGN_VERTICAL':
- txt = "Align vertically"
- txt += """ selected points to the closest selected point from mouse.
- Left click to apply.
- Right click to cancel"""
- return txt
- @classmethod
- def poll(self, context):
- return (context.mode == 'SCULPT_GPENCIL' or context.mode == 'EDIT_GPENCIL')
- def modal(self, context, event):
- if event.type == "MOUSEMOVE":
- pos = view3d_utils.region_2d_to_location_3d(context.region, context.space_data.region_3d,
- (event.mouse_region_x, event.mouse_region_y), (0,0,0))
- for p in self.selected_points:
- v = Vector((pos[0] - p.co[0], 0, pos[2] - p.co[2]))
- self.selectedPoint = None
- if v.length < 0.04:
- self.selectedPoint = p
- break
- if self.selectedPoint:
- context.window.cursor_modal_set("CROSSHAIR")
- else:
- context.window.cursor_modal_set("PAINT_CROSS")
- elif event.type == "LEFTMOUSE":
- context.window.cursor_modal_restore()
- context.window.cursor_modal_restore()
- if self.selectedPoint:
- for p in self.selected_points:
- if self.align == 'CONVERGE' or self.align == 'ALIGN_HORIZONTAL':
- p.co[0] = self.selectedPoint.co[0]
- if self.align == 'CONVERGE' or self.align == 'ALIGN_VERTICAL':
- p.co[2] = self.selectedPoint.co[2]
- return {'FINISHED'}
- return {'CANCELLED'}
- elif event.type in {'RIGHTMOUSE', 'ESC'}:
- context.window.cursor_modal_restore()
- return {'CANCELLED'}
- return {'RUNNING_MODAL'}
- def execute(self, context):
- init_selected(context, self)
- context.window.cursor_modal_set("PAINT_CROSS")
- context.window_manager.modal_handler_add(self)
- return {'RUNNING_MODAL'}
- g_sample_length = 0.01
- class GPExtras_OT_quick_stroke_sample(bpy.types.Operator):
- """Sample selected stroke with new points.
- Scroll mouse wheel to add / remove number of points.
- Hold shift for higher detail.
- Left click to apply.
- Right click to cancel"""
- bl_idname = "gpextras.quick_stroke_sample"
- bl_label = "Sample Stroke"
- bl_options = {'REGISTER', 'UNDO'}
- sample_interval = 0.05
- sample_length_bak = g_sample_length
- @classmethod
- def poll(self, context):
- return (context.mode == 'SCULPT_GPENCIL' or context.mode == 'EDIT_GPENCIL')
- def modal(self, context, event):
- global g_sample_length
- if event.shift:
- self.sample_interval = 0.001
- else:
- self.sample_interval = 0.01
- if event.type == 'WHEELUPMOUSE':
- if g_sample_length - self.sample_interval > 0:
- g_sample_length -= self.sample_interval
- context.area.header_text_set("Sample Length: %.4f" % g_sample_length)
- bpy.ops.gpencil.stroke_sample(length=g_sample_length)
- elif event.type == 'WHEELDOWNMOUSE':
- g_sample_length += self.sample_interval
- context.area.header_text_set("Sample Length: %.4f" % g_sample_length)
- bpy.ops.gpencil.stroke_sample(length=g_sample_length)
- elif event.type == "LEFTMOUSE":
- context.area.header_text_set(None)
- return {'FINISHED'}
- elif event.type in {'RIGHTMOUSE', 'ESC'}:
- g_sample_length = self.sample_length_bak
- context.area.header_text_set(None)
- bpy.ops.ed.undo()
- return {'CANCELLED'}
- return {'RUNNING_MODAL'}
- def execute(self, context):
- global g_sample_length
- context.area.header_text_set("Sample Length: %.4f" % g_sample_length)
- context.tool_settings.use_gpencil_select_mask_stroke=False
- context.tool_settings.use_gpencil_select_mask_point=True
- bpy.ops.gpencil.stroke_sample(length=g_sample_length)
- context.window_manager.modal_handler_add(self)
- return {'RUNNING_MODAL'}
- class GPExtras_PT_sidebar(bpy.types.Panel):
- bl_label = "Grease Pencil Extras"
- bl_space_type = "VIEW_3D"
- bl_region_type = "UI"
- bl_category = "Grease Pencil"
- def draw(self, context):
- layout = self.layout
- row = layout.row(align=True)
- # Converge selection
- row.operator('gpextras.converge_points', icon = 'SNAP_MIDPOINT', text = '' ).align = 'CONVERGE'
- # Align Horizontally
- row.operator('gpextras.converge_points', icon = 'TRIA_LEFT_BAR', text = '' ).align = 'ALIGN_HORIZONTAL'
- # Align Vertically
- row.operator('gpextras.converge_points', icon = 'TRIA_DOWN_BAR', text = '' ).align = 'ALIGN_VERTICAL'
- # Sample
- row.separator()
- row.operator('gpextras.quick_stroke_sample', icon = 'SEQ_LUMA_WAVEFORM', text = "")
- # Adjust properties, like hardness etc
- row.separator()
- row.operator('gpextras.adjust_stroke_properties', icon = 'PROPERTIES', text = "")
- # Set Properties
- # box = layout.box()
- col = layout.column(align=True)
- # col.label(text="Set Properties")
- row = col.row(align=True)
- row.prop(context.scene, "gp_extras_stroke_prop_set_amount", text="")
- row.operator('gpextras.set_stroke_properties', icon='MOD_OUTLINE',text = '',text_ctxt="Sets the selected strokes' hardness").mode = 'HARDNESS'
- row.operator('gpextras.set_stroke_properties', icon='STYLUS_PRESSURE',text = '',text_ctxt="Sets the selected points' pressure").mode = 'PRESSURE'
- row.operator('gpextras.set_stroke_properties', icon='GP_MULTIFRAME_EDITING',text = '',text_ctxt="Sets the selected points' strength").mode = 'STRENGTH'
- # Class list to register
- _classes = [
- GPExtras_OT_adjust_stroke_properties,
- GPExtras_OT_set_stroke_properties,
- GPExtras_OT_converge_points,
- GPExtras_OT_quick_stroke_sample,
- GPExtras_PT_sidebar
- ]
- def register():
- for cls in _classes:
- bpy.utils.register_class(cls)
- bpy.types.Scene.gp_extras_stroke_prop_set_amount = FloatProperty(default=1,min=0,description="Amount to set the selected strokes/points' hardness/pressure/strength to")
- def unregister():
- del bpy.types.Scene.gp_extras_stroke_prop_set_amount
- for cls in _classes:
- bpy.utils.unregister_class(cls)
- if __name__ == "__main__":
- register()
Advertisement
Add Comment
Please, Sign In to add comment