Background / context: I am writing a simple addon for quicker manipulation of text sequences, to more easily do things like set colour, location, size, and duration.
Given this background and the API documentation, I have found the object (TextSequence) properties that I need to modify, and created a script with operators that modify the property (for testing purposes: randomly). The script in its present, testing form is appended to the post.
(Side note: the script currently uses global enums for the preset colours/positions, I think the better practice is to move those into PropertyGroups)
I am familiar with python, and I am fine with calling functions/methods with arguments; what Iām not sure of is how to do the same thing in the Blender context!
Example:
Say a text sequence is selected, and Iād like to change the text colour to green using the SetTextColour operator Iāve implemented, by pressing a hotkey (eg ctrl+shift+g
) or a leader key sequence (eg <leader> t c g
). How can I pass one of my preset colours to the operator? ie how can I do SetTextColour(Colours.GREEN)
?
Obviously there are workarounds- I can create operators for each variant: SetTextColourGreen
, SetTextColourRed
; or I could implement operators which cycle through the presets (eg SetTextColourNextPreset
/ SetTextColourPreviousPreset
). This feels a bit clumsy!
I looked through the quickstart / overview but didnāt find what I was looking for. Searches took me to a QA on BSE, but even there the custom argument is set in the execute()
method.
I feel like Iām missing something obvious, so appreciate any guidance If what Iāve written isnāt clear I am happy to clarify. Thanks in advance!
Current test script
(as noted above, this randomly modifies the properties- this is not the final behaviour!!)
"""quicker-text-editing.py -- text addon for Blender VSE"""
bl_info = {
"name": "Quicker Text Editing for VSE",
"author": "bertieb",
"version": (0, 1),
"blender": (3, 3, 0),
"location": "Video Sequence Editor > Text Strip",
"description": "Quicker editing of text strips: position, colour, size, duration",
"warning": "",
"doc_url": "",
"category": "Sequencer",
}
from enum import Enum
import bpy
import random
class Colours(Enum):
"""some predefined colours - array of 4 floats (RGBA)"""
GREEN = [0.03529411926865578, 0.6117647290229797, 0.03921568766236305, 1.0]
PURPLE = [0.43800756335258484, 0.0, 0.6117647290229797, 1.0]
BLUE = [0.12156863510608673, 0.41568630933761597, 0.6117647290229797, 1.0]
class Locations(Enum):
"""predefined locations - array of 2 floats (x,y)"""
ONE = [0.5, 0.1]
TWO = [0.5, 0.22]
THREE = [0.5, 0.34]
FOUR = [0.5, 0.45]
class TextSequenceAction(bpy.types.Operator):
"""Implements operations for quickly manipulating text sequences in VSE"""
bl_idname = "sequencer.textsequenceaction"
bl_label = "Text Sequence Action"
def execute(self, context):
return {"FINISHED"}
@classmethod
def poll(cls, context):
"""Ensure we're in the VSE with at least one sequence selected"""
return (context.scene and context.scene.sequence_editor
and context.selected_editable_sequences is not None)
class SetTextColour(TextSequenceAction):
"""Set colour of text sequence[s]"""
bl_idname = "sequencer.settextcolor"
bl_label = "Set Text Colour"
def execute(self, context):
for strip in bpy.context.selected_editable_sequences:
if strip.type == "TEXT":
strip.color = random.choice(list(Colours)).value
return {'FINISHED'}
class SetTextLocation(TextSequenceAction):
"""Set location of text sequence[s]"""
bl_idname = "sequencer.settextlocation"
bl_label = "Set Text Location"
def execute(self, context):
for strip in bpy.context.selected_editable_sequences:
if strip.type == "TEXT":
strip.location = random.choice(list(Locations)).value
return {'FINISHED'}
class SetTextDuration(TextSequenceAction):
"""Set location of text sequence[s]"""
bl_idname = "sequencer.settextduration"
bl_label = "Set Text Duration"
def execute(self, context):
for strip in bpy.context.selected_editable_sequences:
if strip.type == "TEXT":
strip.frame_final_duration += random.randint(-10, 10)
return {'FINISHED'}
class SetTextSize(TextSequenceAction):
"""Set size of text sequence[s]"""
bl_idname = "sequencer.settextsize"
bl_label = "Set Text Size"
def execute(self, context):
for strip in bpy.context.selected_editable_sequences:
if strip.type == "TEXT":
strip.font_size += random.choice([-15, -10, -5, 5, 10, 15])
return {'FINISHED'}
REGISTER_CLASSES = [SetTextColour, SetTextLocation, SetTextDuration,
SetTextSize]
def register():
for classname in REGISTER_CLASSES:
bpy.utils.register_class(classname)
def unregister():
for classname in REGISTER_CLASSES:
bpy.utils.unregister_class(classname)
if __name__ == "__main__":
register()