Brushstroke Tools: Initial Version #328

Merged
Simon Thommes merged 229 commits from SimonThommes/blender-studio-tools:brushstroke_tools-initial-version into main 2024-11-06 15:03:47 +01:00
Showing only changes of commit 53a5e3aebc - Show all commits

View File

@ -3,20 +3,26 @@ from . import utils
from mathutils import Vector from mathutils import Vector
from bpy.types import WorkSpaceTool from bpy.types import WorkSpaceTool
def preserve_draw_settings(context, restore=False):
props_list = ['curve_type', 'depth_mode', 'use_pressure_radius', 'use_project_only_selected', ]
if restore:
draw_settings_dict = context.scene['BSBST-TMP-draw_settings_dict']
for k, v in draw_settings_dict.items():
setattr(context.tool_settings.curve_paint_settings, k, v)
del context.scene['BSBST-TMP-draw_settings_dict']
else:
draw_settings_dict = dict()
for item in props_list:
draw_settings_dict[item] = getattr(context.tool_settings.curve_paint_settings, item)
context.scene['BSBST-TMP-draw_settings_dict'] = draw_settings_dict
def node_group_settings(ng_settings, op_settings): def node_group_settings(ng_settings, op_settings):
ng_settings.nodes['Color'].value = [*op_settings.brush_color, 1.] ng_settings.nodes['Color'].value = [*op_settings.brush_color, 1.]
ng_settings.nodes['Smear'].outputs[0].default_value = op_settings.brush_smear ng_settings.nodes['Smear'].outputs[0].default_value = op_settings.brush_smear
#ng_settings.nodes["Object Info"].inputs[0].default_value = surface_object #ng_settings.nodes["Object Info"].inputs[0].default_value = surface_object
class BSBST_OT_draw(bpy.types.Operator): class BSBST_tool_settings(bpy.types.PropertyGroup):
"""
Custom draw operation for hair curves
"""
bl_idname = "brushstroke_tools.draw"
bl_label = "Custom Draw"
bl_options = {'REGISTER', 'UNDO'}
brush_color: bpy.props.FloatVectorProperty(name='Brush Color', brush_color: bpy.props.FloatVectorProperty(name='Brush Color',
size=3, size=3,
subtype='COLOR', subtype='COLOR',
@ -25,6 +31,38 @@ class BSBST_OT_draw(bpy.types.Operator):
soft_max=1, soft_max=1,
update=None, update=None,
) )
class BSBST_OT_draw(bpy.types.Macro):
"""
Custom draw operation for hair curves
"""
bl_idname = "brushstroke_tools.draw"
bl_label = "Custom Draw"
bl_options = {'REGISTER', 'UNDO'}
class BSBST_OT_pre_process_brushstroke(bpy.types.Operator):
"""
Set up custom draw tool for curve drawing
"""
bl_idname = "brushstroke_tools.pre_process_brushstroke"
bl_label = "Custom Draw Pre Process"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
preserve_draw_settings(context)
context.tool_settings.curve_paint_settings.curve_type = 'POLY'
context.tool_settings.curve_paint_settings.depth_mode = 'SURFACE'
context.tool_settings.curve_paint_settings.use_pressure_radius = True
context.tool_settings.curve_paint_settings.use_project_only_selected = True
return {'FINISHED'}
class BSBST_OT_post_process_brushstroke(bpy.types.Operator):
"""
"""
bl_idname = "brushstroke_tools.post_process_brushstroke"
bl_label = "Custom Draw Post Process"
bl_options = {'REGISTER', 'UNDO'}
brush_smear: bpy.props.FloatProperty(name='Smear', brush_smear: bpy.props.FloatProperty(name='Smear',
subtype='FACTOR', subtype='FACTOR',
default=0, default=0,
@ -34,45 +72,36 @@ class BSBST_OT_draw(bpy.types.Operator):
) )
ng_process = None ng_process = None
init = True
gp = None gp = None
def modal(self, context, event): def execute(self, context):
if self.init: if not self.ng_process:
#bpy.ops.grease_pencil.brush_stroke('INVOKE_DEFAULT') preserve_draw_settings(context, restore=True)
bpy.ops.curves.draw('INVOKE_DEFAULT', wait_for_input=False) return {'CANCELLED'}
self.init = False tool_settings = context.scene.BSBST_tool_settings
else:
self.ng_process.nodes['settings.color'].value = [*self.brush_color, 1.]
self.ng_process.nodes['view_vector'].vector = context.space_data.region_3d.view_rotation @ Vector((0.0, 0.0, 1.0))
self.ng_process.nodes['new_key'].boolean = context.scene.tool_settings.use_keyframe_insert_auto
if 'BSBST_surface_object' in context.object.keys():
if context.object['BSBST_surface_object']:
self.ng_process.nodes['surface_object'].inputs[0].default_value = context.object['BSBST_surface_object']
#node_group_settings(self.ng_settings, self)
bpy.ops.geometry.execute_node_group(name="set_brush_stroke_color", session_uid=self.ng_process.session_uid)
return {'FINISHED'}
return {'RUNNING_MODAL'}
self.ng_process.nodes['settings.color'].value = [*tool_settings.brush_color, 1.]
self.ng_process.nodes['view_vector'].vector = context.space_data.region_3d.view_rotation @ Vector((0.0, 0.0, 1.0))
self.ng_process.nodes['new_key'].boolean = context.scene.tool_settings.use_keyframe_insert_auto
if 'BSBST_surface_object' in context.object.keys():
if context.object['BSBST_surface_object']:
self.ng_process.nodes['surface_object'].inputs[0].default_value = context.object['BSBST_surface_object']
bpy.ops.geometry.execute_node_group(name="set_brush_stroke_color", session_uid=self.ng_process.session_uid)
preserve_draw_settings(context, restore=True)
return {'FINISHED'}
def invoke(self, context, event): def invoke(self, context, event):
utils.ensure_resources() utils.ensure_resources()
if True: self.gp = None
BSBST_OT_draw.gp = None self.ng_process = bpy.data.node_groups['.brushstroke_tools.processing']
BSBST_OT_draw.ng_process = bpy.data.node_groups['.brushstroke_tools.processing'] return self.execute(context)
context.tool_settings.curve_paint_settings.curve_type = 'POLY' # TODO: restore later
context.tool_settings.curve_paint_settings.depth_mode = 'SURFACE' # TODO: restore later
context.tool_settings.curve_paint_settings.use_pressure_radius = True # TODO: restore later
context.tool_settings.curve_paint_settings.use_project_only_selected = True # TODO: restore later
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
else:
self.report({'WARNING'}, "Not editable")
return {'CANCELLED'}
def register_custom_draw_macro():
op = BSBST_OT_draw.define("brushstroke_tools.pre_process_brushstroke")
op = BSBST_OT_draw.define("curves.draw")
op.properties.wait_for_input = False
op = BSBST_OT_draw.define("brushstroke_tools.post_process_brushstroke")
class BrushstrokesCurves(WorkSpaceTool): class BrushstrokesCurves(WorkSpaceTool):
bl_space_type = 'VIEW_3D' bl_space_type = 'VIEW_3D'
@ -92,20 +121,27 @@ class BrushstrokesCurves(WorkSpaceTool):
def draw_settings(context, layout, tool): def draw_settings(context, layout, tool):
props = tool.operator_properties("brushstroke_tools.draw") props = tool.operator_properties("brushstroke_tools.draw")
tool_settings = context.scene.BSBST_tool_settings
layout.prop(context.tool_settings.curve_paint_settings , "radius_max") layout.prop(context.tool_settings.curve_paint_settings , "radius_max")
layout.prop(context.tool_settings.curve_paint_settings , "surface_offset") layout.prop(context.tool_settings.curve_paint_settings , "surface_offset")
layout.prop(props, "brush_color") layout.prop(tool_settings, "brush_color")
classes = [ classes = [
BSBST_tool_settings,
BSBST_OT_pre_process_brushstroke,
BSBST_OT_post_process_brushstroke,
BSBST_OT_draw, BSBST_OT_draw,
] ]
def register(): def register():
for c in classes: for c in classes:
bpy.utils.register_class(c) bpy.utils.register_class(c)
bpy.types.Scene.BSBST_tool_settings = bpy.props.PointerProperty(type=BSBST_tool_settings)
register_custom_draw_macro()
bpy.utils.register_tool(BrushstrokesCurves, after={"builtin.draw"}, group=True) bpy.utils.register_tool(BrushstrokesCurves, after={"builtin.draw"}, group=True)
def unregister(): def unregister():
for c in reversed(classes): for c in reversed(classes):
bpy.utils.unregister_class(c) bpy.utils.unregister_class(c)
bpy.utils.unregister_tool(BrushstrokesCurves) bpy.utils.unregister_tool(BrushstrokesCurves)
del bpy.types.Scene.BSBST_tool_settings