Connect Script to Object

In the Game engine, you are able to connect a script to and object through the python controller, is there a way to connect a script to a particular object, without specfically referencing the object by name. Like the ‘self’ keyword in python. Outside the game engine!

Something like this maybe?


def foo(self):
    print(self.name)

bpy.types.Object.foo = foo

bpy.data.objects['Cube'].foo()


I’ll tryit out!

Well tried it! I don’t think this is what I mean.

I think i’m tring to do this, link a script to an object so you can do this:

a = self.foo()

And you would’nt have to name the object because the script context is only a linked object.

I tried you code but I could call your code from any object.

bpy.data.objects[‘Camera’].foo()

This script is not execlusive to the chosen object, making the script part of the object.

Maybe I just not getting it. Yet I can see how this would be a very powerful usage, yet if you called the script from an object that dose’nt support the function you could quickly get in trouble.

How do you pass in the

bpy.types.Object(ID).foo = foo
would that link the function to only this object?

ID looking for object ID now.

ob = bpy.data.objects[‘Cube’]
bpy.types.Object(ob).foo = foo
thinks im calling foo

why would you want a function bound to an object in the first place? what are you trying to achieve?

I do like the fact that you can attach a new function to an Object, but you might want to do a type check upon execution to prevent errors.

You could write a script that responds to external activity, every frame_change. You could infact create your own python modifier that is only connected to that object.
I’m actually working on a mesh modifier, that adds noise to the objects vertices based on the proximatety of anouther object in the scene, affecting only those verticies with in range of the object.

You can’t duck punch a function into a class instance at runtime, only into the class itself so the function will of course be available to all sub-types of that class.

What you can do is attach it to ob.data (eg. bpy.types.Mesh) and if you need to loop over objects and call it do a “if hasattr(ob.data, ‘foo’): ob.data.foo()” which would pass over Lamp and Camera and whatnot.

You probably don’t really need to do it like this (as it’s overkill for what you want) to get your script to work but you were asking how you could use self…