Blender Python style guide
Code conventions for Blender Python
class POWER_SEQUENCER_OT_gap_remove(bpy.types.Operator):
"""
Remove gaps, starting from the time cursor, ignoring locked strips
"""
bl_idname = 'power_sequencer.gap_remove'
bl_label = 'Remove Gaps'
bl_description = 'Removes gaps, starting from the time cursor, ignoring locked strips'
bl_options = {'REGISTER', 'UNDO'}
ignore_locked: bpy.props.BoolProperty(
name="Ignore Locked Strips",
description="Remove gaps without moving locked strips",
default=True)
all: bpy.props.BoolProperty(
name="Remove All",
description="Remove all gaps starting from the time cursor",
default=False)
frame: bpy.props.IntProperty(
name="Frame",
description="Frame to remove gaps from, defaults at the time cursor",
default=-1)
@classmethod
def poll(cls, context):
return (context.sequences and len(context.sequences) > 0)
def execute(self, context):
frame = self.frame if self.frame >= 0 else context.scene.frame_current
sequences = context.sequences
if self.ignore_locked:
sequences = [s for s in context.sequences if not s.lock]
sequences = [s for s in sequences
if s.frame_final_start >= frame
or s.frame_final_end > frame]
sequence_blocks = slice_selection(context, sequences)
if not sequence_blocks:
return {'FINISHED'}
gap_frame = self.find_gap_frame(context, frame, sequence_blocks[0])
if gap_frame == -1:
return {'FINISHED'}
first_block_start = min(sequence_blocks[0], key=attrgetter('frame_final_start')).frame_final_start
blocks_after_gap = (sequence_blocks[1:]
if first_block_start <= gap_frame else
sequence_blocks)
self.gaps_remove(context, blocks_after_gap, gap_frame)
return {'FINISHED'}
def find_gap_frame(self, context, frame, sorted_sequences):
"""
Takes a list sequences sorted by frame_final_start
"""
...
return gap_frame
def gaps_remove(self, context, sequence_blocks, gap_frame_start):
"""
Recursively removes gaps between blocks of sequences
"""
...
def move_markers(self, context, gap_frame, gap_size):
...Class names

5 techniques to write clean code
Avoid calling other operators
Tips to make your own code easier to read
Last updated