12
$\begingroup$

If the user renames an object, the reference via string key is lost.

Is it possible to store a pointer to an object in a property assigned to a class of bpy.types

I checked

>>> type(obj.bl_rna.properties['parent'])
<class 'bpy.types.PointerProperty'>  


However type for PointerProperty is expected to be a subclass of bpy.types.PropertyGroup but bpy.types.Object is inherited from bpy_struct and bpy.types.ID.

$\endgroup$
14
$\begingroup$

No, despite the sound of PointerProperty, its purpose is not to store a pointer/reference to an ID datablock. Update: you can use it to reference ID datablocks in Blender 2.79 and above, see the other answer.

It's used to organize a set of properties in a nice way, especially if your addon registers a lot of properties:

# Bad, clutters scene objects and may even conflict with other addons
bpy.types.Scene.my_prop_1 = bpy.props.IntProperty()
bpy.types.Scene.my_prop_2 = bpy.props.IntProperty()
bpy.types.Scene.my_prop_3 = bpy.props.IntProperty()

# Good, organize properties as a group
class MyAddonProperties(bpy.types.PropertyGroup):
    my_prop_1 = bpy.props.IntProperty()
    my_prop_2 = bpy.props.IntProperty()
    my_prop_3 = bpy.props.IntProperty()

bpy.types.Scene.my_addon = bpy.props.PointerProperty(type=MyAddonProperties)

# Access it e.g. like
#bpy.context.scene.my_addon.my_prop_1

http://www.blender.org/documentation/blender_python_api_2_69_release/bpy.props.html#propertygroup-example

What you are looking for is bpy.props.IDProperty, but we don't have that (shame!)

These kind of properties need to be created in C, like Object.parent.

In Python, you can only store by name (StringProperty). Use it together with a CollectionProperty and layout.prop_search().

Note: The StringProperty isn't tied to the object's name. It's possible to abuse app handlers to check for name changes and to update the property accordingly, but I wouldn't consider it very safe nor efficient.

$\endgroup$
  • $\begingroup$ blender.stackexchange.com/questions/7973/… has an example of doing this with StringProperty. $\endgroup$ – neodelphi Mar 1 '18 at 18:46
  • $\begingroup$ So in a nutshell: use PropertyGroup / PointerProperty for custom properties attached to bpy._types_ and use CollectionProperty to add custom properties to objects. Thanks CoDEmanX, that clarified things for me! $\endgroup$ – squarespiral Jul 27 '18 at 3:21
9
$\begingroup$

As of release 2.79 it is possible to store a pointer to an object

Usage example:

my_pointer = PointerProperty(name="My Pointer", type=bpy.types.Object)
$\endgroup$
3
$\begingroup$

As @max-villafranca pointed out, you can use a PointerProperty to point to an object. I don't have the reputation to comment, but his reply made me re-check, when I had already given up.

my_pointer = PointerProperty(name="My Pointer", type=bpy.types.Object)

Works perfectly, thanks Max!

$\endgroup$
  • 1
    $\begingroup$ Added you answer to Max_Villafranca's answer, for completion. ;) $\endgroup$ – Secrop Feb 2 '18 at 23:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.