17
$\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$
1

3 Answers 3

17
$\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$
2
  • $\begingroup$ blender.stackexchange.com/questions/7973/… has an example of doing this with StringProperty. $\endgroup$
    – neodelphi
    Mar 1, 2018 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$ Jul 27, 2018 at 3:21
19
$\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$
1
$\begingroup$

We can add pointer property to store a object, and scenes, textures and all other data types in 2.79 version

bpy.types.Scene.MyPointer = bpy.props.PointerProperty(name = "my pointer", description = "my descr",
type=bpy.types.Texture) #add a texture pointer

or

bpy.types.Scene.MyPointer = bpy.props.PointerProperty(name = "my pointer", description = "my descr",
type=bpy.types.Mesh) #add a mesh pointer
$\endgroup$
1
  • 1
    $\begingroup$ "We can add pointer property to store ... all other data types in 2.79 version" <-- This is very incorrect. Only types which subclass bpy.types.PropertyGroup or bpy.types.ID can be referenced by a PointerProperty. You cannot use a PointerProperty to point to any other type! Notably, this means bpy_struct and all of its descendents cannot be referenced by a PointerProperty. Not everything inherits from ID; in fact, many notable "data block" types actually inherit from bpy_struct. For example, PointerProperty(type=bpy.types.ShapeKey) is invalid and will always fail. $\endgroup$ Jul 21, 2023 at 0:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .