16
$\begingroup$

I'm trying to figure out a good workflow for developing scripts and add-ons in Blender. Here's what I'm currently doing as I try my luck with a PyNode add-on:

  1. Open blender
  2. Create a text editor panel
  3. Open my script (external text file)
  4. Edit my script in a nice external editor
  5. Reload the script
  6. Save the .blend file
  7. Run the script

Now that I can see what my code does, this Blender instance has been modified, so I can't (make changes to my script and) run my script again. So I do this:

  1. Close blender
  2. Open blender with the .blend from step 6 and repeat steps 4-7, over and over and over..

Is there a better workflow for this sort of thing?

$\endgroup$
5
  • $\begingroup$ Do you really need that nice external editor? What features of this editor are you missing in blender? I am asking because I can help to improve the text editor a bit so you don't have to go external.. $\endgroup$ May 20, 2015 at 6:02
  • 2
    $\begingroup$ @Jerryno 1. I don't know why, but writing code in the editor feels slow, 2. there are 1-2 bugs, which produces unexpected indent errors, 3. smarter code completion and suggestions, 4. simultaneously highlighting of the same variables for better Search and Replace functionality would be really nice. $\endgroup$
    – p2or
    May 20, 2015 at 8:13
  • $\begingroup$ Hmm I'll just post an answer with the text editor addon, might solve 1 and 2 but 3 no way and I thought about 4 but didn't implement it, though it wouldn't be that hard to add. $\endgroup$ May 20, 2015 at 8:50
  • $\begingroup$ Did you put those two basic lines in the end? if name == 'main': register() $\endgroup$
    – Mano-Wii
    May 20, 2015 at 13:03
  • $\begingroup$ @Jerryno For me, I'm so used to Vim shortcuts that all other editors seem clunky :) $\endgroup$
    – ajwood
    May 20, 2015 at 15:17

4 Answers 4

11
$\begingroup$
  1. Save addon script in the addons_contrib folder
  2. Use nice external editor to edit the script
  3. Disable/Enable addon in preferences.

I find this the easiest for addons, by editing the script while it is in the addons folder you only have to disable and re-enable it in the preferences to reload the modified script. The preferences window can be left open to keep it easily accessible.

For non-addon scripts I often edit the file externally and copy/paste it into blenders text editor. I limit what I save in a blend file, only data to start testing, I will often run the script then reload the blend from recent files to go back to the start.

I use an addon by CoDEmanX to run scripts, I picked it up from a blender artists thread. This adds the text blocks to a run script menu in blenders python console, it is much easier to get feedback right there and after you run the script the script variables are available in the console for you to explore further.

$\endgroup$
1
17
$\begingroup$

Blender text-editor is not good for coding, but using external one is cumbersome because the code still needs to be debugged regularly in blender and the blender console and outliner dataview is also very usefull. Switching back and forth would be an annoying experience for me even if the codes (external, internal) could be synced (which they can be with scene_update handlers..).


There was couple years ago an attempt to improve the Text-editor (using Scintilla that many external text editors use), but it seems dead.


So some time ago I wrote this addon to improve some Text editor functionality so I don't have to go the external path. Nowadays I would code some of it's parts differently and better (especially the multithreaded minimap updating) so any feedback is very welcomed. Here are the features:

  • Start and End with right-click in any Text-editor window (it also ends with F8 because when scripting an addon you need to reload with F8 and that is not possible because this addon is a modal operator. So you need F8 + F8 + Right-click>Start_again )

    enter image description here

  • When started it adjusts the text-editor to the addon preferences, so you don't have to set line_numbers, syntax_highlight, text_margin etc. every time you open new text-editor window (this is frustating blender behavior, these settings should be global..)

    enter image description here

  • It follows blender theme colors. The default ones are bad, so here I give you a good ones so its usable:

    enter image description here

  • It adds a scrollable and clickable code minimap with syntax highlighting, which fades out when window is too small. It is done in another thread and has lazy updating to not lag the text-editor (python is slow for this). It can be disabled in preferences with just setting huge Hide Panel threshold:

    enter image description here

  • It adds markers to visualize indentation, and it displays the scope of defs and classes (you can see it above in the gif). The scopes could be minimized but this didn't make it into this release because when blender crashes you would loose the hidden code parts (the lines were removed from text and stored in custom property). You can see the toggle icons when you hover over line numbers.

  • Autocompletes brackets and "",'':

    enter image description here

  • Use Alt+C for magic:

    • put things in lists:

      enter image description here

    • evaluate expressions (it knows pi, e, phi(golden ratio) and g(grav accel)):

      enter image description here

    • stringify words when cursor just after the word:

      enter image description here

  • Correct Home and Shift+Home behavior. Will move to first letter of line or to the previous indent. Alt+Home now moves to the line beginning:

    enter image description here

  • Make comments with Alt+D. (Remove with Ctrl+Shift+D - this one is not new):

    enter image description here

  • With multiple text-files it displays tabs to quickly switch between (blender has tabs but does not use them where it should..)

    enter image description here

All this was good enough for me not to use external editor. Multiple variables highlighting and multi-editing (like Sublime has) was planned but abandoned. Maybe if enough people will be interested I could further improve this. But I don't see Blender addon python api strong enough to do this properly, this should be done in Blenders code for performance (like that scintilla patch).


There is also this paid addon that does blender api autocomplete and is very nice.


In your workflow I don't see much room for optimization, except for auto-reloading your script with bpy.app.handlers.scene_update_pre handler. You should be able also to return to the start state with ctrl-z so you don't have to restart blender but that depends on what you are doing if that would work.

$\endgroup$
8
  • 2
    $\begingroup$ +5, if I could - a text editor I'll fall in love with ;) Thanks for sharing. $\endgroup$
    – p2or
    May 20, 2015 at 12:30
  • $\begingroup$ thx @poor, though I feel that it would have to have more useful features (like that auto-complete or code hiding) to be really a thing. $\endgroup$ May 20, 2015 at 14:55
  • $\begingroup$ ctrl-z doesn't undo the kind of stuff that my script does.. $\endgroup$
    – ajwood
    May 20, 2015 at 15:21
  • 1
    $\begingroup$ My script registers a custom node tree type. I'm not really comfortable with the api yet, but I suppose what I should be doing is IF node tree is registered, then unregister/reregister ELSE register.. that way I might be able to modify my script and rerun it in the same blender instance? $\endgroup$
    – ajwood
    May 20, 2015 at 15:22
  • $\begingroup$ @Jerryno I've tried this addon in blender 2.77 but it crashes blender as soon as I right-click and select Start Code Editor. Also I wonder if there are any updates regarding this script? $\endgroup$
    – Tak
    Oct 26, 2016 at 1:38
3
$\begingroup$

I use pycharm to edit the python. When pycharm loses focus it saves the file. Then when I get over to the blender window the little red circle icon in the header of the text editor area allows me to reload the script.

How about after you run the script: do not save; instead use File > Open Recent ( or Ctrl+Shift+o ) and re-load the version you saved in step 6. Once you have it open, you can load the latest version of the addon and try it again.

In my personal projects I try to code in such a way that I can run the script multiple times and it still works (either by deleting the old versions of the objects, or by updating them).

# edit: to provide an example of how to create an object, or modify it if it already exists
# since I didn't include one in my original answer
def fab_object_for(scn, u,v):
    name = "ball2 at %d,%d" % (u, v)
    obj = bpy.data.objects.get(name)
    if obj is None:
        obj = bpy.data.objects.new(name, bpy.data.meshes["Icosphere"])
    try:
        scn.objects.link(obj)
    except:
        pass
    obj.location = (u,v,0)
    rig_nlas(obj, u, v)

Sometimes I have a second script whose job it is to purge stale versions of objects to create a clean(er) slate.

scn = bpy.context.scene
for obj in scn.objects:
    if obj.name[:10] == 'active at ':
        scn.objects.unlink(obj)
        # optionally
        # obj.name = "discard"
$\endgroup$
2
  • $\begingroup$ This is a great suggestion too! I guess my problem is that my script (and the person writing is) hasn't matured enough to be able to do that. I still need to get comfortable with the API/environment $\endgroup$
    – ajwood
    May 20, 2015 at 19:10
  • $\begingroup$ I added some examples of techniques for dealing with "stale" objects. $\endgroup$
    – Mutant Bob
    May 21, 2015 at 16:08
2
$\begingroup$

For an external workflow I suggest Script Runner. This Addon allows to execute external scripts (up to 10 external python files) in 3d View or the Text Editor.

Since every script is basically a button, you can assign a shortcut via Right Click:

enter image description here

I personally prefer Q, this results in the following workflow:

  1. Edit the script in an external editor,
  2. Save it and switch back to blender (Alt+Tab)
  3. Hit Q in blender to execute the script
$\endgroup$
0

You must log in to answer this question.

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