2
$\begingroup$

O, I have this Collection Property:

class particle_instancer(bpy.types.PropertyGroup):
    particle_object = StringProperty (name = "Object", description = "")
    particle_material = StringProperty (name = "Material", description = "")

bpy.utils.register_class(particle_instancer)
bpy.types.Scene.particle_instancer = CollectionProperty(type = particle_instancer)  

And then I have an operator that is supposed to add a StringProperty() to its first item.

If Instead of a CollectionProperty I used the scene as the "Container" for the properties, I could simply do:

bpy.types.Scene.my_prop = StringProperty()

I thought I could do:

bpy.types.particle_instancer.my_prop = StringProperty()

But this leads to nothing as the property doesn't get register and it doesn't give me any error StackTrace neither. So is it possible to do? If yes how could I do?

$\endgroup$
0
2
$\begingroup$

You can add properties to the PropertyGroup as follows:

bpy.types.Scene.particle_instancer[1]['type'].my_prop = bpy.props.StringProperty()

Internally, your bpy.types.Scene.particle_instancer is a tuple that will look like this:

(<built-in function CollectionProperty>, {'type': <class '__main__.particle_instancer'>, 'attr': 'particle_instancer'})

So, you can index the dict giving you access to the particle_instancer class, where you can add properties.

I got the idea to access the 'type' from this answer: https://blender.stackexchange.com/a/28091/20051

$\endgroup$
1
$\begingroup$

You can only assign values or add new class attributes per item.

import bpy

class CustomGroup(bpy.types.PropertyGroup):
    id = bpy.props.IntProperty()
    foo = bpy.props.StringProperty()
    ...

bpy.utils.register_module(__name__)
bpy.types.Scene.custom_group = bpy.props.CollectionProperty(type=CustomGroup)

>>> item = bpy.context.scene.custom_group.add()
>>> item.id = 0
>>> item.name = "Cube"
>>> item.foo = "Sphere"
>>> item.foo
>>> 'Sphere'
$\endgroup$
1
  • 2
    $\begingroup$ I was able to add new class attributes with bpy.types.Scene.custom_group[1]['type'].my_new_prop = ... $\endgroup$ Dec 7 '18 at 6:07

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.