2
$\begingroup$

I have a kind of theoric question.

I've registered a PropertyGroup Class to the GreasePencil class/type, lets call it Property_A.

At the same time, this Property_A has Collection Property with different elements.

(So, I have an structure like):

a_grease_pencil_object.property_a.collection

My question is:

Having an element/instance of that collection, is it possible to know/get the original Grease Pencil where it is stored?

Some kind of tracing back where it belongs?

I'm new and Im not sure how bl_rna works, but maybe some kind of just getting the name of the Grease Pencil where the property is stored, so I can get it via bpy.data.grease_pencils[name]?

(I need this since for some methods, I accesed the Grease Pencil by the active Grease Pencil. But now I have to loop over all the grease pencils (using bpy.data.grease_pencils to change different things, and since those methods use the active GP, they dont work. So Im looking to replace that code for the thing Im asking).

Edit: Reformulating my question (maybe in a simpler way), I want to kind of get the data path of a property

So if I have element_zero = a_grease_pencil_object.property_a.collection[0]

somekind of method that could make something like:

element_zero.get_data_path()

And I get kind of:

bpy.data.grease_pencils["GPencil.011"].property_a.collection

So I know that I could get the name of the Grease Pencil object where it is from.

Thank you so much for the help, much appreciated!

$\endgroup$

1 Answer 1

3
$\begingroup$

You can very simply use repr(prop), or in your case repr(element_zero). This will return a string with the full data path from bpy.

From the repr documentation :

Return a string containing a printable representation of an object. For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to eval(); otherwise, the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object. A class can control what this function returns for its instances by defining a repr() method. If sys.displayhook() is not accessible, this function will raise RuntimeError.

If you want access to the base ID object (ie the grease pencil object) holding the property, you can use prop.id_data. See the docs for bpy.types.Struct.id_data.

$\endgroup$
2
  • $\begingroup$ Both options does exactly what I wanted! And I can't believe I wasn't aware of id_data until you pointed it. Thank you so much for the help friend, much appreciated! $\endgroup$
    – Ommadawn
    Aug 22, 2023 at 19:36
  • 1
    $\begingroup$ @Ommadawn hehe always a pleasure to answer well documented and thought out questions. I still discover new things and tricks every day ! Cheers $\endgroup$
    – Gorgious
    Aug 22, 2023 at 19:48

You must log in to answer this question.

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