Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> For example, many operations (bpy.ops) require an accurate "context" to succeed.

The bpy.ops are really just a python shim to be able to call them from the UI.

Not a good idea to call them from a script (because of the reasons you cited) but that hasn’t stopped anyone, ever. The whole of blender is designed around the MVC model and the operators are the ‘control’ and are dependent on the ‘view’ (aka context) to do their thing.

All the underlying data structures should be exposed to the python API (through bpy.data IIRC) so, in theory, one could do whatever their little heart desires.

Weird design but once you realize the UI runs everything through python it mostly makes sense.



Definitely, prefer using bpy.data objects whenever you can. IIRC there were a few things I wanted to do that only seemed possible through bpy.ops, for example recursively duplicating a collection. In the UI, this calls bpy.ops.outliner.collection_duplicate() and requires the Outliner editor to exist and be active. There are workarounds, but they aren't pretty.


Back when I was poking at the python API a lot of what I did was figure out how the operator did it and either wrap the function it called as a method on the object or write a simple C function wrapper and expose that to python. Most things that people needed just needed someone to spend a little time on.

I think it was the outliner (the part where you edit the keyframes?) where the underlying design made the python API an absolute trainwreck. I spent a bunch of time on that and it was just bad, the best that could be done was to expose it and let people who were sufficiently motivated dig around and figure out how all the pieces interacted because it wasn’t at all obvious. Horrible design from the python side…

Anyhoo, sounds like someone just needs to add a collection.duplicate() or .clone() method — whichever is more pythonic.


Another example is changing modifier order - you can do that only with operators.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: