Question: Can Blender Builtin Modules be accessed from an External Interpreter?

(Pte Jack) #1

I’m just getting into Python and have managed to snake (pun intended) my way through a couple of projects. But, with the number of classes, properties, methods, and what-not else in a Blender Project, it’s just so overwhelming for a beginner.,,,, etc etc, etc. When do I access What, When or How do I use What, and How do I get That to do This instead of That or print What I Want, When I Want it to. (I would Prefer to learn without Banging my head off the computer desk in frustration.)

I’ve just downloaded the MS Visual Studio Code program which has an extension for Python. I’ve added the path to Blender Python to my USER Path statement so VS Code can find “Python”.

Is it possible to load (import) the Blender BPY (or other builtin) modules into VS Code so I can watch and debug code while I write it and it’s active and have access actual syntax help by hovering the mouse over a command?

Or are the builtins only accessible via the Blender Python Console (in other words there has to be a firm connection to the actual objects within the Blender environment for Python to see the elements of the blender scene?)

I’m looking for a way to be able to walk through my code as I write it, learn the in and outs as I go, with a minimal frustration factor as I learn.


(Dimitris Chloupis) #2

Yes blender Python modules are usually located inside a shared folder , it’s appdata roaming in win if I remember correctly and library application support in macOS . It’s basically where addons are added located to. You can add those to the library paths of your project in the ide of your choice. The best way learning coding with blender Python is using pdb.set_trace() which will stop execution there and open the debugger that will let you test and inspect code as it executes. This way you know exactly what goes wrong.

So so look up pdb which basically the default Python debugger and the dir() built method that allows to explore Python objects.

Best python ide right now is PyCharm that specializes on Python and comes with free edition for open source projects with only few features disabled.

Live coding is also a way to speed up your workflow you can do it using importlib.reload() it basically reloads Python modules during execution which in turn allows you to change will it executes and see the changes live.

The list does does not stop here , there is iPython that also integrates well but be warned it’s extremely powerful and complex tool. It basically a lot like blender’s Python console but with a ton more features.

I also like to overide the draw fuction of the viewports view ports which allows me to print to viewport directly debug info Tex and I also use the logging library of Python to creat log text files that contain detail info of what happens with my code.

Overiding viwpewports or other windows draw it also also allow you to create all sort of fancy visualization for your data.

text editor or blender may be weak but also comes with its own API explorer but personally I am fine with dir().

Python also has its reflection libraries , reflectionis the ability of a language to inspect , explore and modify its live code and data during execution. It’s one of the big features that make Python so much more powerful than less powerful languages like C/C++ which is why Python is regularly used to create prototypes even if the final code is to be written in another language.

Python is extremely complex language . It may be minimalistic by design but it comes with a crazy amounts of features , 99% of the are rarely used.

An an example is the AST module which allows you to modify the very Python syntax during execution. HyLang uses this feature to allow you to write Lisp code that generates Python bytecode, meaning you can easily mix it with existing Python code and have direct access to all Python libraries with no compromises. All this happens during code execution.

I can write books of how to use Python efficiently with Blender but then I would repeating the same advice for using Python with anything else. Afterall Blender Python is just regular Python with no major limitations.

Last but Definetly not least is TDD , which Python of course support that allows you to create automated tests for your code to spot near impossible to find manually bugs and is extremely popular for big code that tends to be too complex for testing it the manual way

PS: I forgot to add you can build blender as a Python library too to be used outside blender , Blender Python API docs contains info about this

(Pte Jack) #3

Thanks Kilon, I appreciate the reply.

I have gone through the various folders that blender adds itself to (I use both the installed Steam Version and the Portable (zip) download from but when I search for the primary BPY module py files. I am finding bpy_types, bpy_restrict_State and I think I saw bpy_Mesh and bpy_Utils in there somewhere (which are usually extracts used by various plugins), but not the big hitters.

I’ve seen the website that referred to the building Blender as a Python Library, but the build method is a bit beyond me right now.

Using Help(‘object you want help with’) Most of the documentation scrolls out of window and is not accessible (I’ve found a way to pass help() to a text file) and of course Dir(‘object’) produces the immediate attributes associated with the object. But again without knowing how to properly access the methods, they produce the “cryptic” error messages which the novice just scratches his head at and starts head bumping the desktop.

I’ll look for some tutorials for using pdb, but if you (or anyone out there) knows of some really good Blender Python tutorials, posting a link to them would be appreciated.

Remember, you’re responding to a complete novice using Python for Blender, but again, thank you for the reply.

(Dimitris Chloupis) #4

Nothing is beyond you.

Some people find coding hard, some people find it easy. The people who find it hard are those on a deadline or in a hurry those that find it easy invest the time needed to guarantee a good understanding and quality produced code. Both are extremes, coders as somewhere in between.

The code cannot be rushed, it wont respect how smart you are , it wont respect how practical you , it wont respect how beginner you are or whether “you are too old for this shit” or “don’t have time for this shit”. The code demands respect it never gives it.

I know that may sound quite poetic because for most people coding may sound so incredible smart or complex or hard. Its neither of those things, it just takes time, thats all. A lot of time. A ton of time. It is a science afterall and , some will argue , an art.

So my advise, forget what you dont know or how much you dont know, pick what you want to learn and go learn it, the only thing you need to do is invest the time.

So let’s get to the practical stuff. Python started as a scripting language for C/C++ coders. Its what made Python so popular, it offered a very easy way to port C/C++ libraries to it. Around 50% of Python itself is written in C, you wont find py modules for those things too either.

When you import a module you dont necessarily import a module. In case of Blender because Python is basically inside the executable, so are many of the libraries it uses. BGL for example to a great extend , though appears as a Blender Python module it is actually C code using PyObject. PyObject is part of the Python C API a collection of C functions that allow for python to talk to c and vice versa. PyObject maps C functions to python methods , functions and other objects. Yes a python function is basically a python object but thats another story.

Obviously after you learn the basics of Python you will need to take a deep dive inside the Blender Python API DOCS

First section with the 7 links are absolutely mandatory to read. The rest is reference documentation you may use as you see fit depending on what you try to learn.

In coding not understanding is perfectly fine, you learn things depending on your needs, so each coder follows his own path on this.

Don’t generally learn stuff, its a waste of time, you will forget it afterwards. Pick a project , learn what you need for your next 10 lines of code , write them, then go to the next 10 lines etc.

Help() wont be much help , pun very much intended, because it basically prints docstrings (basically regular strings using triple double quotes) that are the basis that form the API documentation so you basically getting the same things minus the extra stuff that come with the API docs.

dir() is different because it allow you to examine the structure of your objects which helps a lot because coding is a lot more about being an investigator than being an engineer.

Good ,bad , mediocre tutorials. Does not matters, as I said, time is the key. Invest the time , the knowledge will come. There are no red or blue pills.

Learning C is also inevitable because its really helpful at times to look at C code when API DOC dont contain the info you want, internet does not offer any clues and asking in this forums get you no answers. Learning C is about learning very few things about it, just enough to figure out some technical stuff about the python objects used by blender.

“Head bumping desktops” is another way to name coding. Thats exactly what it is, it has nothing to do with you being a beginner. There is a comic I once read , two coders discuss one saying. “There is one unit that can accurately measure the quality of your code, the lesser it is the higher quality your code is”. The other asks of course what that is and the first coder replies “WTFs/s” , so the second asks again “whats that ?” , the first one replies “WTFs per second”. Get used to cryptic messages , lack of documentation and tons of werid and stupid code. All part of coding.

I am coding for fun since 1988 , almost 30 years, took me one entire day yesterday to figure out how to load textures to the blender viewport directly via BGL (OpenGL). It came down to one word.

Not command.

Not method.

Not object.

Just one word.


Still no clue why it works like that and not the way I assumed it will, but I cannot say I care much other than it works and now I can move to the next as you so elegantly put it “head bumping on desktop”.

But it always great fun to figure things out yourself and a huge confidence boost. So take the dive to the deep black waters of coding and just swim.

Oh and keep asking questions. Its always crucial to ask questions and many times you will find this curious things that happens with the brain when you ask a question it helps you find the answer by yourself. There also plenty of people willing to help out, helping each other is essential to survive the madness of coding.

(Pte Jack) #5

Great pep talk, kilon and much appreciated. And yup, I’ve been working with computers since my first TI99/4a and I am completely self taught. I celebrated my 60th birthday yesterday and can totally relate to everything you said, especially your last statement. I’ve always said that, “The only STUPID question a person can have is the one that isn’t asked.”

As far as becoming a programmer, I dabble, and usually learn just enough to be dangerous (the old, get’er done, hacker type), but as I reach into things I find that sometimes I like to push the envelope of my knowledge from time to time. And this is one of those times. So expect to see a few questions as the model sets I’m currently working on require a lot of manual intervention and I think most of the fixes can be done using python. It is a very powerful tool and I like to add elements like it to my tool box.

Also I’m working a tutorial series with the models I’m working on and I think that if I show some of the power within Blender Python to my watch group, I might be able to spark some interest and get some of the younger generation interested in learning as well.