I wrote a blog post, back in December, on building meaningful abstractions for the Blender API using arithmetic operators and context managers. I continued exploring some more ideas in that route. And this time, I tried something with the Blender’s node system.
TL;DR – Can you connect two node sockets using a syntax like this:
Very interesting, it breaks lots of programming conventions but perhaps you get very powerful code like this. Using string representations and overload operators as such.
If for example you are interested to see if there is a camera in the scene you can serialize all objects into a string and search it instead.
>>> objects = repr(list(bpy.context.scene.objects))
>>> 'Camera' in objects and 'Cube' in objects
True
Or to search object types as such:
>>> types = repr(list([x.type for x in bpy.context.scene.objects]))
>>> types
"['MESH', 'LIGHT', 'CAMERA']"
It depends though if it makes the logic of the code easier and simpler according to each case. However since Python is very powerful to modifying objects, the entire code can be shaped in a very powerful ways.
From what I see bpylib makes programming a little bit more “declarative” (or functional) which is a nice option, for more general purpose programming there is a case to use the pipe operator in order to simplify iteration loops and such.
Looks like it is a convention followed in the Blender API and is available for certain types. It returns where a particular data block is from, it seems.
Yup, very handy ! Other sometimes useful methods are path_from_id which returns the path from the id to the property (shortened version of repr) and path_resolve which lets you get objects from strings without having to use eval or exec which are notoriously bad practice.