Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

March 20, 2012

Quick Text Editor addon, A walk through Part1

For a while now i've wanted an arithmetic evaluator in various IDEs. Something that would turn a simple mathematical sequence of characters into their resulting value. My main interest being whole number arithmetic, something a checked eval() might be able to do. That means that when I select something like 234+23+15-5 and run the operator, it will replace the chain with one value. This is handy for UI tweaking/openGL stuff.

The only way I know to get the content of the selected portion of text in Blender Text Editor is by having my code execute from inside the Text Editor Window. A quick look at the operations needed resulted in taking down some notes. But if you try that from console, you will get a 'context is incorrect' error because the ops is taking place inside console, and and not text editor. I need text editor as context for my shortcut, so we land in boilerplate territory. It's not too bad, there are templates!

In a previous bout of blender python frenzy I made a small add-on to allow me to switch on all eye candy for the Text Editor ( text appeal onGithub ). That add-on is different enough to not really provide me with much reusable code, so I take a look at the code for the blender UI (located in /bin/2.62/scripts/startip/bl_ui called space_text.py). I hope to find some reference to the right click menu here. When you right click inside the blender text editor, this menu is called. So how do we add to that menu? Easy! We write an add-on to attach to that menu, instead of modifying blender startup/ui code in place. The above code is represents a step closer to what I want, it adds a right click item (currently blank) to the menu and if you launch blender with terminal then you will see {finished} being printed, because we are not printing the content of clipboard (that's where the coped text goes to). Let's add the menu item name, so it makes sense. At this point if you run the script with that change, you will still see the old menu item with the blank space, and a new one called something like 'Eval selected', i'll continue without restarting blender, there are only a few more changes to make. So, to kind of jump ahead a little: This copies the selected text into clipboard, evaluates it, writes it to 'answer' , copies answer to clipboard and pastes from clipboard. done. but really? eval is kind of evil, this is a dirty implementation and it could potentially open us up to a lot of pain, we might want to perform some checks on the content of clipboard before running eval(). Part2 will perform checks on the clipboard and implement a safer eval function.

No comments:

Post a Comment

Please use Blender.StackExchange.com for python scripting questions unrelated to this post.