questions about ways to attach your functions to the bpy and 'self' use.

this is a post about frustration, and quest for secret knowledges and answers.:confused:

can I attach a function to a collection of blender object to obtain for example :

    bpy.data.image['an_image'].rotate()

(that is, rotate will apply to the an_image object…)

or :

how to retrieve the blender object from self and context when I attach a class as a pointer in these collections ? like :

    bpy.data.image['an_image'].tools.rotate()

rotate won’t apply to an-image. rotate(self) in the Tools class does not returns an_image but the tools pointer.

so you’re stuck, as any of the two works.

can I attach to context ?

    img = bpy.context.active_image

this actually is bpy.context.window.screen.areas[x].spaces[y].image or bpy.data.screens but you need loops and tests for x and y. I got this function in a script but I can’t share in an elegant way.

since operator cannot return anything but a status I can’t even directly :

    img = bpy.ops.image.active

could be useful but not compatible with a row.operator() call from a draw()… which syntax to pass arguments to operators is not good sorry about that.
in many circumstance you’ll need a mini string parser then an object lookup to retrieve your objects in the operator, or a clipboard-like class as a step to pass arguments.

so as a pythonist 2nd rank programmer, I attach my functions anywhere, as a tribute to chaos. then blender first ring and silent nerds shout at you :eyebrowlift:. because in most of the script, you’ll get absurd and wild things like :

    img = bpy.scene.my_supaclass.imgs.enabled()

I know class inheritance works so maybe there’s some ways to combine.

hey, I’m very happy with the new API. this is a really great job. this is really a great mainframe. and there’s lots I don’t known so maybe it can be made.

but register(), pointer props and bpy classes would need some more features or more documentation. I can contribute to that if I know practices, at least in french.

please, please… share info… thanks :slight_smile:

Excellent question. I hope this thread lasts a while. The answer is yes, you can do that. As to whether it’s proper, I don’t know. It can get a little crazy…

here’s my sort-of simple example:

def foowho(mesh):
return mesh.name

bpy.types.Mesh.foo = foowho

bpy.context.object.data.foo() will return the mesh name, in this example.

You can make things even more complicated with @properties and classes, of course.

(sry I didn’t see your reply)

:slight_smile: thanks for this ! in france there’s an expression for the kind of brain I have :
“se faire des noeuds au cerveau”, litteraly “to tie knots in your brain”, going the tricky way whereas python is most of the time clear, logical and handy. :o

so forget these bpy props.pointer blah things for contextual functions and go wild :


registr() :
    [...]
    bpy.types.Image.rotate = ImageTools.rotate # ImageTools is a class of my module

img = bpy.data.images['blah']
img.rotate(90)

many many thanks :slight_smile: