3
$\begingroup$

I try to improve my "comment", uncomment", "copy" and "cut" command in the text editor and i'd like to know if it was possible to print the number and the index of selected lines in the text editor. I'd like to comment or uncomment all selected lines. This is the code i'm using to comment:

class CustomComment(bpy.types.Operator):
    ''' Auto comment with a space after # '''
    bl_idname = "text.custom_comment"
    bl_label = "Custom comment"

    def execute(self, context):

        base = context.space_data.text.current_line.body
        bpy.ops.text.select_line()            
        if "    " in base:
            bpy.ops.text.move(type='LINE_BEGIN')
            bpy.ops.text.move(type='NEXT_WORD')        
            bpy.ops.text.insert(text="# ")
        else:
            bpy.ops.text.move(type='LINE_BEGIN')        
            bpy.ops.text.insert(text="# ")                
        return {'FINISHED'}
$\endgroup$
4
  • 1
    $\begingroup$ The index of the selected line is in context.space_data.text.current_line_index, which is the first line if multiple are selected. Print() will output to the terminal or console that blender was started. $\endgroup$
    – sambler
    Commented Jul 15, 2015 at 8:20
  • 1
    $\begingroup$ You can already (un)comment via Text Editor -> Format -> (un)comment. The relevant api sections are bpy.types.Text and bpy.ops.text $\endgroup$ Commented Jul 15, 2015 at 8:40
  • $\begingroup$ @ sambler: as you say it, this command get only the current line index, not all the selected line :( $\endgroup$
    – pistiwique
    Commented Jul 15, 2015 at 11:03
  • $\begingroup$ @ pink vertex: i looked in the API but don't find a command as "selected_objects". Maybe with the python command or by combinig several command of blender's API. I'd like to comment all selected lines. I continuous to look. $\endgroup$
    – pistiwique
    Commented Jul 15, 2015 at 11:11

1 Answer 1

4
$\begingroup$
import bpy

txt = bpy.data.texts['Text']
line_begin = txt.current_line_index

#select_end_line_index missing - iterate over txt.lines
for line_end, line_obj in enumerate(txt.lines):
    if line_obj == txt.select_end_line:
        break

selection = [i for i in range(line_begin, line_end + 1)]
print(selection)

There is already a comment function

comment function

$\endgroup$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .