Python Editing for Blender (Part One)

A few things I've done since I started working with Python scipting in Blender:

Command-Line Blender

Once I read this, the light bulb turned on: Don't use Blender!

blender myscene.blend --background --python myscript.py

Instead of opening Blender and running the script, I've been running Blender from the command line. This gives me the flexibility to write code that isn't tied to a particular .blend file, and I can use my favorite Python editor, which brings me to...

PyCharm

Most people I know use Eclipse to edit Python, but I've gotten hooked on PyCharm. Witold Jaworski has autocompletion files for setting Eclipse up to acknowledge Blender's bpy module, which works with PyCharm after changing the extensions to '.py' from '.pypredef' (I tried adding .pypredef to the list of recognized python extensions but it didn't pick them up).

pycharm_settings

Next Up

I'll make a "Hello World" python file run via the command line through Blender's python interpreter, then make a trivial GUI with WXPython.

On to Python Editing for Blender (Part Two)

17 Responses to “Python Editing for Blender (Part One)”

  1. nirenyang Says:

    Thanks for your guide. Waiting Part two …

  2. James Kersey Says:

    Thanks! Part 2 is almost ready.

  3. dim Says:

    hi this is really interesting and i have been searching for something like this for a long time..i have a problem though, both in eclipse and pyCharm (by the way i am in debian testing)

    i changed the .pypredef to .py and i get this error

    Traceback (most recent call last):
    File “/home/dimitris/PycharmProjects/firstProject/Tester.py”, line 4, in
    import bpy
    File “/home/dimitris/blender-2.68-linux-glibc211-x86_64/doc/python_api/pypredef/bpy.py”, line 53145
    def template_list(listtype_name, list_id=””, dataptr, propname, active_dataptr, active_propname, rows=5, maxrows=5, type=’DEFAULT’):
    ^
    SyntaxError: non-default argument follows default argument

    any ideas what that might be and how i can solve it?

  4. James Kersey Says:

    Cool, I’m pretty excited about the whole topic 🙂

    I haven’t tested it in Linux yet (and I have no excuse, there is a laptop right next to me). It looks like it’s importing that predef file instead of using Blender’s bpy module, you might just need to remove that directory from your python path when running it.

    How are you executing the script? I can try to replicate that here.

  5. dim Says:

    hi and thanks for replying
    actually i followed the instructions from the pdf file from the link you suggested..

    well, yes i am importing these files, isn`t this what we are supposed to do?
    i have attached a screenshot from the python path, i think it is the same

    https://dl.dropboxusercontent.com/u/4253811/pycharm.png

  6. James Kersey Says:

    Looks like you’re all set for PyCharm. The pdf instructions beyond getting Eclipse set up diverges from how I’m setting things up here.

    Take a look at Part Two; it covers running the script from the command line. Let me know if that doesn’t help.

  7. Jon Says:

    Dear James and Dim,

    Ihave the same problem as Dim, and I have not managed to solve it. I get the same error…

    How did you solve it?

    Thanks,

    Jon

  8. James Kersey Says:

    Hi Jon,

    I think I found it. If you try to run it through PyCharm you’ll get that error because it’s using the template files instead of the real Blender python code. On the command line, you just need to make sure the template files aren’t in your path.

    Let me know if this works, or doesn’t, and we’ll go from there 🙂

  9. Gustaf Blomqvist Says:

    I’ve been trying to import bpy to pycharm with this method with no complete success. The bpy files from the link that you posted seems to be old or something, because the “bpy.py” file only contains 2 classes, but there exists 8 of these classes in blender…

    I have tried running the pypredef_gen.py for getting newer files but that doesn’t solve the problem. How exactly am I supposed to import the complete bpy to pycharm?

    Thanks

    /Gustaf

  10. James Kersey Says:

    I haven’t kept up with the latest PyCharm, and it looks like some things got moved around. I added the directory of the auto-generated .pypredef files (renamed to *.py) as a new Content Root in the Project Structure category and it looks like it’s finding at least the top-level classes.

    If you want to see some code of this stuff in action, the BlenderSpriter page has links to my github project. Specifically: https://github.com/jkersey/BlenderSpriter/blob/master/RenderScript.py

    Hope that helps!

  11. Gustaf Blomqvist Says:

    Thanks for your reply, I tried to do what you said but I still have the same problem. I get 2 complete classes, not even the other top classes like for example it can’t find bpy.props… Anyways I think it works pretty well without importing the blender api if I just run the code once in a while in blender. 🙂

  12. Michael Says:

    Any news on newer pycharm versions and the bpy-import? I can’t find bpy.props like Gustaf.

  13. James Kersey Says:

    I haven’t tried it in a while, I’ll see what I can figure out this weekend. Probably about time I cleaned this page up 🙂

  14. Gustaf Says:

    That would be great,
    I recently found something I haven’t seen before on the bottom of this page: .

    It links to this page: .

    I haven’t really looked into it yet, but I think that might be the answer to importing blender as a python module?

  15. James Kersey Says:

    I remember trying that early on, but couldn’t get it to compile. That would be a far better way to do this, you’d be able to run the scripts straight from PyCharm (and regular Python) instead of having to run it through the Blender command line.

  16. James Kersey Says:

    I couldn’t find the project options I used for the previous PyCharm. I did get it to work by adding a bpy directory to my project, right-clicking on it and setting “Mark Directory As->Sources Root”.

    After that, copy all of the .pypredef files to that directory. You can skip the rename step above if you right-click on one of the files and “Associate With File Type -> Python”

    Probably a terrible way to do this, any better suggestions would be welcome 🙂

  17. James Kersey Says:

    I still don’t see the bpy.props, though, there’s a note at the top of that .pypredef file that makes it sound like it’s supposed to be internal:

    This module defines properties to extend blenders internal data, the result of these functions is used to assign properties to classes registered with blender and can’t be used directly.

Leave a Reply