2
$\begingroup$

How can i store a array/list of Pointer Properties?!
I want every pointer to point to a object and get a specific pointer with an index or key.

import bpy

class customPropertiesGroup(bpy.types.PropertyGroup):
    objects = bpy.props.PointerProperty(type=bpy.types.Object)    
bpy.utils.register_class(customPropertiesGroup)

bpy.types.Object.customList = bpy.props.CollectionProperty(type=customPropertiesGroup)


ob = bpy.context.active_object.customList.add().name = 'ob_01'
ob = bpy.context.selected_objects[1].copy()

is how can i access ob_01 and does ob_01 actually store a copy of selected object [1]?

$\endgroup$
  • $\begingroup$ Python does not use pointers the way C does. qa.geeksforgeeks.org/1540/qa.geeksforgeeks.org/1540/… $\endgroup$ – Justin Helps Mar 22 at 14:08
  • $\begingroup$ @JustinHelps sure but im talking about docs.blender.org/api/blender2.8/bpy.types.PointerProperty.html they exist and i want a array of these props that is unique to every object when i make a array of the pointerprops and store them in a object they are shared between all objects $\endgroup$ – Syler Mar 22 at 14:16
  • $\begingroup$ Okay, I'm not exactly sure what you're trying to do. Could you add some code that you would think should work but doesn't? $\endgroup$ – Justin Helps Mar 22 at 16:48
  • $\begingroup$ @JustinHelps my current workaround is that i create new objects and just dont add them to the scene/renderlayer/collection so that they remain hidden, they all have a index prop and when i want to access one object i loop trough all objects that have a pointer prop if that equals to the active object i check their index props if that index prop is what i want then i have my reference to the object that i want to access having a single array/list variable would be much cleaner but if thats not possible i will just use my messy system $\endgroup$ – Syler Mar 22 at 17:21
2
+100
$\begingroup$

Can add methods to the propertygroup definition

Something that is very handy IMO is the ability to add methods to the property group.

Here I've added copy to make a copy of itself to the collection, and add to add an object to the collection. In both cases the object name is used as the collection item name.

Remember that 2.8 uses annotations for properties.

import bpy

class ObjectCopy(bpy.types.PropertyGroup):
    object : bpy.props.PointerProperty(type=bpy.types.Object)    

    def copy(self):
        self.object = self.id_data.copy()
        self.name = self.object.name
        return self.object

    def add(self, ob):
        self.object = ob
        self.name = ob.name
        return self.object

def register():
    bpy.utils.register_class(ObjectCopy)
    bpy.types.Object.copies = bpy.props.CollectionProperty(type=ObjectCopy)

def unregister():
    bpy.utils.register_class(ObjectCopy)
    del(bpy.types.Object.copies)

if __name__ == "__main__":
    register()

Some testing in the python console.

>>> C.object.copies.clear()
>>> for o in C.selected_objects:
...     C.object.copies.add().add(o)
...     
bpy.data.objects['Cube.002']
bpy.data.objects['Cube.001']
bpy.data.objects['Cube']

>>> C.object.copies.add().copy()
bpy.data.objects['Cube.005']

>>> C.object.copies['Cube'].object
bpy.data.objects['Cube']
$\endgroup$
  • $\begingroup$ nice thank you is there a way to store only bpy.context.active_object.data or modifiers? $\endgroup$ – Syler Mar 25 at 17:16
  • $\begingroup$ For data could add pointers for each case, eg mesh, armature. Possibly easiest to do both on an object level. Save a dummy object, and hence it's data and any added modifiers. $\endgroup$ – batFINGER Mar 26 at 8:17
2
$\begingroup$

You are actually pretty close.

class CustomProp(bpy.types.PropertyGroup):
    '''name = StringProperty() '''
    id = bpy.props.IntProperty()
    object = bpy.props.PointerProperty(
        name="Object",
        type=bpy.types.Object,
    )

bpy.types.Scene.custom = CollectionProperty(type=CustomProp)

Adding objects:

scn = bpy.context.scene
item = scn.custom.add()
item.id = len(scn.custom)
item.object = bpy.data.objects.new(name="My Object", object_data=C.object.data)
item.name = item.object.name
$\endgroup$
  • $\begingroup$ i get this error: TypeError: bpy_struct.__new__(type): expected a single argument, im using 2.80 0bbff8a71138 $\endgroup$ – Syler Mar 25 at 15:06

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.