Essential Blender Text Editor Add-ons

Glad you found it useful. I’ll move things out from module level and start packaging it once I’m happy with how it behaves. Currently it keeps me sane having it separated while debugging or changing the flow of the code.

I’ve uploaded a couple more operators I’ve written in the 2.8 folder, in the same spirit of enhancement.

Toggle Move

  • Makes Home key move caret position to start at indent, then toggle to line begin

Copy 2

  • Copy, cut, paste entire lines without needing to select them first

One thing I’ve noticed is that undoing in the text editor is just generally broken. Sometimes hitting undo deletes entire sentences, clauses or un-links the text instead if undoing a single character or pasted text, sometimes it does absolutely nothing for a dozen steps only to suddenly continue to work.

Because of this, I’m manually pushing undos or adding undo as a macro operator in some cases, because the naive {'REGISTER', 'UNDO'} approach became too unpredictable or in some cases even resulted in loss of text. I’d rather hit Ctrl-Z twice and have it eat up an extra undo step, as long as the undo works. They really should decouple the text undo from the global stack.

1 Like

I’ve written a bit to you on Github. For further testing I properly need a bit more info on how to use your add-ons.

On the unreliable undo did I notice a case here which makes it go wrong: https://developer.blender.org/T65909

But I guess some of the troubles are because it doesn’t seem like the text editor has it’s own undo history, but mixes with ex. 3d view. So when you need to undo in the text editor, but have done a few things in the 3d view, things will go haywire in the text editor.

There has been a bit of UI brush up of the text editor here:
https://developer.blender.org/D5028

Converting the Text Editor to 2.80 style:

Before:

After:

Before:
image

After:

Before:
image

After:

2 Likes

On the topic of including operators in python in the official built of Blender, Campbell, the Text Editor module owner, writes: " Using the clipboard for text tools is a hack which could interfear with the users workflow (they could have some rich text in the clipboard - unrelated to using Blender, which gets overwritten by using this tool).

I think it would be best to extend the Python API so a range (typically the selection) can be operated on, so the text editors undo system can properly track the change.

Once this is done some parts of the text editor could be implemented in Python cleanly."

This is one case where the TE API can be improved, I bet there are more functions in the TE API, which can be improved or added, so TE operators can be coded cleanly(I can’t code C myself)

So maybe it’s a good idea to collect TE bugs and shortcomings here, so it would be easier to estimate how much effort is needed to fix the TE API.

My question to is to you TE coders, what API shortcomings and bugs have you encountered, which could eliminate hacks and workarounds from your TE add-ons?

I agree it’s a hack.

Which is why I decided to rewrite the Copy 2 addon :stuck_out_tongue:

As for API requests:

  • There is currently no elegant way of setting the cursor in the text editor. There exists region_location_from_cursor() and bpy.ops.text.cursor_set(), both which operate on pixel coordinates and aren’t reliable for getting and setting arbitrary positions, especially when the cursor or destination is off-screen.
    A cursor RNA prop definition with both read/write capabilities or a function to set cursor position by line/column is what we need.

  • Getting the selected text should be made easier, either by having a function like text.get_selection() or accessing a property like text.selection. As of now it takes around 20-25 lines of code just to get multi-line selection as a raw string.

  • Campbell not too long ago added a feature to make an input field active when a menu or panel was called, which is what the new Rename Active Item does. In python we can now utilize this by setting activate_init to True before defining the UILayout prop.
    The operator bpy.ops.text.jump(), CTRL J, should utilize this behavior, too, and have its line property compatible with the new activate_init so we don’t have to click the input field. activate_init currently only appears to work for StringProperty types so it needs to be made compatible with other kinds of properties like ints and floats.

Thank you for sharing your insights. I’ve posted them on developer.blender.org. Have you found any patterns in why the undo fails?

Btw. does your run_in_console.py chatch and print all messages from the system console?

As for undo, here’s something you can try for yourself:

  1. Start a new text block
  2. Type “1234567890”
  3. Run script
  4. Undo once (all the text should disappear)
  5. Undo four more times
  6. Now redo once

Essentially blender fails to correctly restore the undo steps, but somehow is able to redo. This is the only behavior I’m able to reproduce 100%. There are times where undo/redo gives me jumbled texts most likely due to a series of script executions, copying and/or pasting.

It only prints from scripts run from the text editor. If you register an operator in the text editor, it will catch any print statements from the operator when it’s run from anywhere in blender.

Catching the entire console isn’t possible since often blender will show prints that originate from C, something which python doesn’t know of.

In order to catch python related prints globally one would have to monkey patch the builtins module in sys.modules to redirect any print statements. I’ve experimented with this some in the past, but I found it only useful getting prints. Run in console is able to catch the tracebacks because I’m specifically handling them in the event that an exception occurs. Errors that occur elsewhere need to have their exceptions handled as well if we want to catch tracebacks.

Then there’s the issue with the console scrollback being tied to blender’s ui. You’re probably aware that blender doesn’t like that things try to execute when it’s trying to redraw (it’s why blender has restricted states), which causes new runtime issues that need to be handled. It’s a big mess.

Interesting the undo fails but redo doesn’t. One more example of that:

  • Type 1234567890
  • In 3D View grab and move ex. a cube.
  • Move mouse cursor into text editor.
  • Hit undo.
  • The cube will be moved back.
  • Hit undo.
  • Only the last typed character will be removed.
  • Can’t undo anymore.
  • Hit redo
  • Some more characters are removed.
  • Hit redo until 1234567890 are restored.

Thank you for the info in run_in_console.

Undo bugs reported here:
https://developer.blender.org/T66172

@iceythe Have you tried this add on? CodeEditor

Hi @iceythe, I would like to know your workaround to get a multiline selection. Totally agree we need something like get_selection() (and to all your suggestions actually ^^).

I made a post about this here on blender dev : https://developer.blender.org/T66317

But maybe you can answer on the BA thread I created about this too. (to avoid flooding this topic who’s more about suggestion than code) : Get / set character and line coordinate of the selection in the text editor

More than 10 years ago the was a google summer of code student how did a huge reworking og the internal functions of the text editor, including this:

Why this never made it into Blender, I don’t know?
Here’s the commit: https://developer.blender.org/rBSaa4e4da8c31a09c8afe0e60d26a1e68dd949e6b8

3 Likes

A Pie Menu for the Text Editor by Lapineige:

2 Likes

How about using Emacs as an external editor? Now works with Blender 2.8!

1 Like

@ldo Thanks for letting us know about this. My intent with this thread is to improve the Blender Text Editor so it will be good enough to submit code to developer.blender.org at least, but I’ll try out you solution too.

I would say the Blender Text Editor is “good enough”. Why bother bloating the Blender code by reinventing much more powerful editors that already exist elsewhere?

I think there are many reasons for trying to keep all of the Blender included editors as modern as possible. One of them is that most of the Blender UI is coded in Python and it is so much faster to fix UI from within Blender.

4 Likes

Finally had some time to sit and noodle in blender. Experimenting with occurrence highlighting. This is one convenience feature I use a lot in vscode.

Due to lack of ways of getting line height reliably I’m doing some voodoo which is turning out to become a mess, so I’m probably far from a release.

6 Likes

Wow, that’s great!

1 Like

Here’s an open document for potential developments of the Blender Text Editor for everyone to contribute ideas to: https://docs.google.com/document/d/10Sk7_XQGitSPxFcPDMsKHIF2-LQ652XUYwMxd-aaQng/edit?usp=sharing

The thought is not to include every feature of all IDEs out there, but trying to identify the functions which will make the Text Editor a modern Text Editor capable of producing quality code in. So what core functions, workflow enhancers etc. are currently missing in your opinion?

2 Likes

a guy asked about quite the same topic on Blender stack exchange in 2015 pointing the problems he found and asking for an extension. the guys closed his topic telling him it was barely no linked to Blender. It makes me very angry to see this. I think they were really jerks in this community and that’s why an idea was so long to evolve.