← All ideas
  • Published 26 Mar 2016

New property type: IDPointerProperty

I think there should be a new type of custom property that can reference an object (similar to the object parent property). I know that you could use a StringProperty to store the object name, but if the user changes the name of the object, it would break. It could even be generalized to be able to reference any datablock.

The IDPointerProperty (bpy.props.IDPointerProperty)

The IDPointerProperty would work like the PointerProperty except for the fallowing two attributes: type (str): The type of datablock it's referencing (e.g. OBJECT, MESH, SCENE, NODETREE, etc.)

subtype (str): Different types would have different sub-categories (e.g. OBJECT would have a CAMERA sub-category). Not every type would need a subtype (TEXTURES for example).

It would also support the general attributes that PointerProperty supports (name, description etc.)

###Example:

bpy.props.IDPointerProperty(
    type='OBJECT', # Could be any other id type e.g. SCENE or MATERIAL
    subtype='CAMERA', # Reference a specific category of object.
    name='Custom Prop',
    description='Custom object property',
    ...
)


Custom Node Trees

Python Node Trees would be a little bit of a special case, but could definitely still be referenced by an IDPointerProperty . The type would be 'NODETREE' but the subtype would be the node tree class (instead of a string).

Example:

bpy.props.IDPointerProperty(
    type='NODETREE',
    subtype=MyCustomTree, # The node tree class.
    name='Node Tree',
    ...
)

Being able to reference node trees has come up on the bf-commiters mailing list: lists.blender.org/pipermail/bf-committers/2015-October/045967.html

More examples

####Scenes: bpy.props.IDPointerProperty( type='SCENE', subtype=None # No sub-categories for scene datablocks! ) ####Meshes: bpy.props.IDPointerProperty( type='MESH', subtype=None # No sub-categories for mesh datablocks! )

####Curve objects: bpy.props.IDPointerProperty( type='OBJECT', subtype='CURVE' # Not curve data, but an object that has curve data. )

This type of property already shows up all over the place in blender (drivers, object parents, texture coords node in cycles, ridged body constraints... just to name a few) it seems that a LOT of addons could benefit from this.

This has popped up a few times before: blender.stackexchange.com/questions/7973/object-selection-box-in-addon

blender.stackexchange.com/questions/6975/is-it-possible-to-use-bpy-props-pointerproperty-to-store-a-pointer-to-an-object

blender.stackexchange.com/questions/2075/assign-datablock-to-custom-property

100%

Development Status: Open

Upvote 3 Downvote 0
wisaac407
26 Mar 2016

8 Comments

brian.savery

A thousand times this. Addon developers would rejoice.

1
samueloates

This has been added to blender somewhere around the 2-3 versions

mano-wii

You can reference the object by its ID easily with ctypes.

ob2 = ctypes.cast(id(obj), ctypes.py_object).value

Just then reference the ID with a property of type Int (IntProperty)

brian.savery

Mano. I'm not sure that works. If I do:

ob2.prop_group.some_Int_Property = ctypes.cast(id(obj), ctypes.py_object).value

I got "expected an int type", or are you suggesting doing this in the get/set of the IntProperty

mano-wii

No, the id(obj) is the IntProperty. some_Int_Property = id(obj)

But there are some things you need to be careful:

  • If the id refer to an object that does not exist, CRASH.
  • I don't know how precise is the IntProperty, but if it is less than 64 bits, CRASH.

So it's a dangerous hacky solution.

brian.savery

I see. Yeah it seems like that id value is everchanging so don't know if that works:

id(D.materials[0]) 4891511368

>>> id(D.materials[0]) 4891509896

brian.savery

Yeah. Either way yours or mano's aren't great solutions. The main problem I run into is like you said above, if the name changes...

Also if you do it that way. Say the Object the stringproperty is on gets copied (like in appending blend files), then the other piece of data it points to does not necessarily get copied.

mano-wii

It seems that material is a property that is rewritten when read. So the address changes :\