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 thoughts on “Python Editing for Blender (Part One)

  1. 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?

    • 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.

        • 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.

          • 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

          • 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 🙂

  2. 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

  3. 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!

    • 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. 🙂

    • 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 🙂

    • 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 🙂

    • 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

Your email address will not be published. Required fields are marked *