Guest User

Blender Grease Pencil Extras Addon v1.1

a guest
Nov 5th, 2023
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.06 KB | Source Code | 0 0
  1. import bpy
  2. from bpy_extras import view3d_utils
  3. from mathutils import Vector
  4. from bpy.props import FloatProperty, EnumProperty
  5.  
  6. bl_info = {
  7.     "name": "Grease Pencil Extras",
  8.     "author": "pongbuster",
  9.     "version": (1, 1),
  10.     "blender": (3, 6, 5),
  11.     "location": "View3D > Sidebar (N)",
  12.     "description": "A collection of utilities to align grease pencil stroke points and adjust stroke hardness, point strength and pressure.",
  13.     "warning": "",
  14.     "doc_url": "",
  15.     "category": "Grease Pencil",
  16. }
  17.  
  18.  
  19. def init_selected(context, self):
  20.     if context.active_object.type != 'GPENCIL':
  21.         return
  22.     gp = context.active_object.data
  23.    
  24.     #list comprehension go brrr
  25.     self.selected_points = [p
  26.     for lr in gp.layers
  27.         if not lr.lock and not lr.hide
  28.             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
  29.                 for s in fr.strokes
  30.                     if s.select
  31.                         for p in s.points
  32.                             if p.select]
  33.  
  34. class GPExtras_OT_adjust_stroke_properties(bpy.types.Operator):
  35.     """Middle mouse to adjust selected strokes' hardness.
  36.    Hold CTRL to adjust selected points' pressure(radius) instead.
  37.    Hold SHIFT to adjust selected points' strength instead.
  38.    Left click to apply"""
  39.    
  40.     bl_idname = "gpextras.adjust_stroke_properties"
  41.     bl_label = "Adjust Stroke or Point Properties"
  42.     bl_options = {'REGISTER', 'UNDO'}
  43.  
  44.     selected_points = []
  45.    
  46.     @classmethod
  47.     def poll(self, context):
  48.         return (context.mode == 'SCULPT_GPENCIL' or context.mode == 'EDIT_GPENCIL')
  49.    
  50.     def modal(self, context, event):
  51.        
  52.         if event.type == "WHEELUPMOUSE" or event.type == "WHEELDOWNMOUSE":
  53.             incr = -0.01 if event.type == "WHEELDOWNMOUSE" else 0.01
  54.             if event.shift:     #Strength
  55.                 incr *= 2
  56.                 for p in self.selected_points:
  57.                     p.strength += incr
  58.             elif event.ctrl:    #Pressure/Radius
  59.                 incr *= 100
  60.                 for p in self.selected_points:
  61.                     p.pressure += incr
  62.             else:
  63.                 gp =  context.active_object.data
  64.                 for lr in gp.layers:
  65.                     if not lr.lock and not lr.hide:
  66.                         frame_list = [fr for fr in lr.frames if fr.select] if gp.use_multiedit else [lr.active_frame]
  67.                         for fr in frame_list:
  68.                             for s in fr.strokes:
  69.                                 if s.select:
  70.                                     s.hardness += incr
  71.                                     context.area.header_text_set("Hardness: %.4f" % s.hardness)
  72.                    
  73.         elif event.type == "LEFTMOUSE":
  74.             context.area.header_text_set(None)
  75.             context.window.cursor_modal_restore()
  76.             return {'FINISHED'}
  77.        
  78.         return {'RUNNING_MODAL'}
  79.  
  80.     def execute(self, context):
  81.         init_selected(context, self)
  82.         context.window.cursor_modal_set("SCROLL_Y")
  83.         context.window_manager.modal_handler_add(self)
  84.        
  85.         return {'RUNNING_MODAL'}
  86.    
  87. class GPExtras_OT_set_stroke_properties(bpy.types.Operator):
  88.     """Sets the selected strokes' hardness, or the selected points' pressure or strength"""
  89.     bl_idname = "gpextras.set_stroke_properties"
  90.     bl_label = "Set Stroke or Point Properties"
  91.     bl_options = {'REGISTER', 'UNDO'}
  92.  
  93.     selected_points = []
  94.     mode : EnumProperty(items = [('HARDNESS', 'Hardness', "Strokes' Hardness"),
  95.                                  ('PRESSURE', 'Pressure', "Points' Pressure/Radius"),
  96.                                  ('STRENGTH', 'Strength', "Points' Strength")], name = "Mode", description = "Which property the operator should set", default = 'HARDNESS')
  97.  
  98.     @classmethod
  99.     def description(cls, context, properties):
  100.         match properties.mode:
  101.             case 'HARDNESS':
  102.                 return "Sets the selected strokes' Hardness to the given value"
  103.             case 'PRESSURE':
  104.                 return "Sets the selected points' Pressure(Radius) to the given value"
  105.             case 'STRENGTH':
  106.                 return "Sets the selected points' Strength to the given value"
  107.         return "Sets the selected strokes' hardness, or the selected points' pressure or strength"
  108.    
  109.     @classmethod
  110.     def poll(self, context):
  111.         return (context.mode == 'SCULPT_GPENCIL' or context.mode == 'EDIT_GPENCIL')
  112.  
  113.     def execute(self, context):
  114.         amount = context.scene.gp_extras_stroke_prop_set_amount
  115.         match self.mode:
  116.             case 'HARDNESS':
  117.                 gp =  context.active_object.data
  118.                 for lr in gp.layers:
  119.                     if not lr.lock and not lr.hide:
  120.                         frame_list = [fr for fr in lr.frames if fr.select] if gp.use_multiedit else [lr.active_frame]
  121.                         for fr in frame_list:
  122.                             for s in fr.strokes:
  123.                                 if s.select:
  124.                                     s.hardness = amount
  125.             case 'PRESSURE':
  126.                 init_selected(context, self)
  127.                 for p in self.selected_points:
  128.                     p.pressure = amount
  129.             case 'STRENGTH':
  130.                 init_selected(context, self)
  131.                 for p in self.selected_points:
  132.                     p.strength = amount
  133.         return {'FINISHED'}
  134.  
  135. class GPExtras_OT_converge_points(bpy.types.Operator):
  136.     arg: bpy.props.StringProperty()
  137.    
  138.     bl_idname = "gpextras.converge_points"
  139.     bl_label = "Converge Selection"
  140.     bl_options = {'REGISTER', 'UNDO'}
  141.    
  142.     selectedPoint = None
  143.     selected_points = []
  144.     align : EnumProperty(items = [('CONVERGE', 'Converge', "Converge to point"),
  145.                                  ('ALIGN_HORIZONTAL', 'Align Horizontal', "Align points horizontally to point"),
  146.                                  ('ALIGN_VERTICAL', 'Align Vertical', "Align points vertically to point")], name = "Align", description = "Type of converging", default = 'CONVERGE')
  147.  
  148.     @classmethod
  149.     def description(cls, context, properties):
  150.         match properties.align:
  151.             case 'CONVERGE':
  152.                 txt = "Converge"
  153.             case 'ALIGN_HORIZONTAL':
  154.                 txt = "Align horizontally"
  155.             case 'ALIGN_VERTICAL':                  
  156.                 txt = "Align vertically"
  157.        
  158.         txt += """ selected points to the closest selected point from mouse.
  159.    Left click to apply.
  160.    Right click to cancel"""
  161.        
  162.         return txt
  163.  
  164.     @classmethod
  165.     def poll(self, context):
  166.         return (context.mode == 'SCULPT_GPENCIL' or context.mode == 'EDIT_GPENCIL')
  167.    
  168.     def modal(self, context, event):
  169.  
  170.         if event.type == "MOUSEMOVE":
  171.             pos = view3d_utils.region_2d_to_location_3d(context.region, context.space_data.region_3d,
  172.                 (event.mouse_region_x, event.mouse_region_y), (0,0,0))
  173.  
  174.             for p in self.selected_points:
  175.                 v = Vector((pos[0] - p.co[0], 0, pos[2] - p.co[2]))
  176.                 self.selectedPoint = None
  177.                 if v.length < 0.04:
  178.                     self.selectedPoint = p
  179.                     break
  180.                
  181.             if self.selectedPoint:
  182.                context.window.cursor_modal_set("CROSSHAIR")
  183.             else:
  184.                 context.window.cursor_modal_set("PAINT_CROSS")
  185.  
  186.         elif event.type == "LEFTMOUSE":
  187.             context.window.cursor_modal_restore()
  188.             context.window.cursor_modal_restore()
  189.  
  190.             if self.selectedPoint:
  191.                 for p in self.selected_points:
  192.                     if self.align == 'CONVERGE' or self.align == 'ALIGN_HORIZONTAL':
  193.                         p.co[0] = self.selectedPoint.co[0]
  194.                     if self.align == 'CONVERGE' or self.align == 'ALIGN_VERTICAL':
  195.                         p.co[2] = self.selectedPoint.co[2]
  196.                    
  197.                 return {'FINISHED'}
  198.             return {'CANCELLED'}
  199.            
  200.         elif event.type in {'RIGHTMOUSE', 'ESC'}:
  201.             context.window.cursor_modal_restore()
  202.  
  203.             return {'CANCELLED'}
  204.        
  205.         return {'RUNNING_MODAL'}    
  206.    
  207.     def execute(self, context):
  208.         init_selected(context, self)    
  209.         context.window.cursor_modal_set("PAINT_CROSS")
  210.         context.window_manager.modal_handler_add(self)
  211.        
  212.         return {'RUNNING_MODAL'}
  213.  
  214. g_sample_length = 0.01
  215.  
  216. class GPExtras_OT_quick_stroke_sample(bpy.types.Operator):
  217.     """Sample selected stroke with new points.
  218.    Scroll mouse wheel to add / remove number of points.
  219.    Hold shift for higher detail.
  220.    Left click to apply.
  221.    Right click to cancel"""
  222.    
  223.     bl_idname = "gpextras.quick_stroke_sample"
  224.     bl_label = "Sample Stroke"
  225.     bl_options = {'REGISTER', 'UNDO'}
  226.  
  227.     sample_interval = 0.05
  228.     sample_length_bak = g_sample_length
  229.    
  230.     @classmethod
  231.     def poll(self, context):
  232.         return (context.mode == 'SCULPT_GPENCIL' or context.mode == 'EDIT_GPENCIL')
  233.    
  234.     def modal(self, context, event):
  235.         global g_sample_length
  236.  
  237.         if event.shift:
  238.             self.sample_interval = 0.001
  239.         else:
  240.             self.sample_interval = 0.01
  241.  
  242.         if event.type == 'WHEELUPMOUSE':
  243.             if g_sample_length - self.sample_interval > 0:
  244.                 g_sample_length -= self.sample_interval
  245.             context.area.header_text_set("Sample Length: %.4f" % g_sample_length)
  246.             bpy.ops.gpencil.stroke_sample(length=g_sample_length)
  247.  
  248.         elif event.type == 'WHEELDOWNMOUSE':
  249.             g_sample_length += self.sample_interval
  250.             context.area.header_text_set("Sample Length: %.4f" % g_sample_length)
  251.             bpy.ops.gpencil.stroke_sample(length=g_sample_length)
  252.  
  253.         elif event.type == "LEFTMOUSE":
  254.             context.area.header_text_set(None)
  255.             return {'FINISHED'}
  256.  
  257.         elif event.type in {'RIGHTMOUSE', 'ESC'}:
  258.             g_sample_length = self.sample_length_bak
  259.             context.area.header_text_set(None)
  260.             bpy.ops.ed.undo()
  261.             return {'CANCELLED'}
  262.  
  263.         return {'RUNNING_MODAL'}    
  264.    
  265.     def execute(self, context):
  266.         global g_sample_length
  267.        
  268.         context.area.header_text_set("Sample Length: %.4f" % g_sample_length)
  269.            
  270.         context.tool_settings.use_gpencil_select_mask_stroke=False
  271.         context.tool_settings.use_gpencil_select_mask_point=True
  272.  
  273.         bpy.ops.gpencil.stroke_sample(length=g_sample_length)
  274.        
  275.         context.window_manager.modal_handler_add(self)
  276.        
  277.         return {'RUNNING_MODAL'}
  278.    
  279. class GPExtras_PT_sidebar(bpy.types.Panel):
  280.     bl_label = "Grease Pencil Extras"
  281.     bl_space_type = "VIEW_3D"
  282.     bl_region_type = "UI"
  283.     bl_category = "Grease Pencil"
  284.  
  285.     def draw(self, context):
  286.         layout = self.layout
  287.         row = layout.row(align=True)
  288.         # Converge selection
  289.         row.operator('gpextras.converge_points', icon = 'SNAP_MIDPOINT', text = '' ).align = 'CONVERGE'
  290.         # Align Horizontally
  291.         row.operator('gpextras.converge_points', icon = 'TRIA_LEFT_BAR', text = '' ).align = 'ALIGN_HORIZONTAL'
  292.         # Align Vertically
  293.         row.operator('gpextras.converge_points', icon = 'TRIA_DOWN_BAR', text = '' ).align = 'ALIGN_VERTICAL'
  294.         # Sample
  295.         row.separator()
  296.         row.operator('gpextras.quick_stroke_sample', icon = 'SEQ_LUMA_WAVEFORM', text = "")
  297.         # Adjust properties, like hardness etc
  298.         row.separator()
  299.         row.operator('gpextras.adjust_stroke_properties', icon = 'PROPERTIES', text = "")
  300.         # Set Properties
  301.         # box = layout.box()
  302.         col = layout.column(align=True)
  303.         # col.label(text="Set Properties")
  304.         row = col.row(align=True)
  305.         row.prop(context.scene, "gp_extras_stroke_prop_set_amount", text="")
  306.         row.operator('gpextras.set_stroke_properties', icon='MOD_OUTLINE',text = '',text_ctxt="Sets the selected strokes' hardness").mode = 'HARDNESS'
  307.         row.operator('gpextras.set_stroke_properties', icon='STYLUS_PRESSURE',text = '',text_ctxt="Sets the selected points' pressure").mode = 'PRESSURE'
  308.         row.operator('gpextras.set_stroke_properties', icon='GP_MULTIFRAME_EDITING',text = '',text_ctxt="Sets the selected points' strength").mode = 'STRENGTH'
  309.        
  310.  
  311. # Class list to register
  312. _classes = [
  313.     GPExtras_OT_adjust_stroke_properties,
  314.     GPExtras_OT_set_stroke_properties,
  315.     GPExtras_OT_converge_points,
  316.     GPExtras_OT_quick_stroke_sample,
  317.     GPExtras_PT_sidebar
  318. ]
  319. def register():
  320.     for cls in _classes:
  321.         bpy.utils.register_class(cls)
  322.     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")
  323.    
  324. def unregister():
  325.     del bpy.types.Scene.gp_extras_stroke_prop_set_amount
  326.     for cls in _classes:
  327.         bpy.utils.unregister_class(cls)
  328.        
  329. if __name__ == "__main__":
  330.     register()
Advertisement
Add Comment
Please, Sign In to add comment