6
$\begingroup$

I created a custom property for my armature and I want to add it to my custom panel. If I want to add an object property to a panel I'll do it like this layout.prop(object, "property"). However this doesn't work for custom properties that I created. What am I doing wrong?

I'm using Blender version 2.79.

Here is the panel code:

class MyPanel(bpy.types.Panel):
    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOLS"
    bl_label = "MyPanel"
    bl_category = "MyPanel"

    def draw(self, context):
       layout = self.layout
       object = context.object

       layout.prop(object, "prop") #Custom property is called "prop"
$\endgroup$
5
  • $\begingroup$ What exactly doesn't work, is there an error displayed on the console? What version of Blender are you using? Blender 2.8 you would implement your own tab differently. $\endgroup$ Commented Aug 17, 2019 at 21:58
  • $\begingroup$ @rjg The Blender version is 2.79. What doesn't work is the custom property isn't drawn on the panel. No error messages are displayed. $\endgroup$
    – user56873
    Commented Aug 17, 2019 at 22:09
  • $\begingroup$ Can you show the code that declares the custom property? $\endgroup$ Commented Aug 17, 2019 at 22:11
  • $\begingroup$ @rjg I didn't define the custom property. I added it via the viewport. $\endgroup$
    – user56873
    Commented Aug 17, 2019 at 22:12
  • $\begingroup$ @rjg Yes. How do I then display the property in the panel? I can reference the object like this object = bpy.data.objects["Armature"] and then write layout.prop(object, "prop"). But this doesn't work. $\endgroup$
    – user56873
    Commented Aug 17, 2019 at 22:20

2 Answers 2

10
$\begingroup$

The custom property is not accessed as an attribute with the ., but through the name of the property.

bpy.data.objects["Armature"]["prop"]

Therefore the correct way of adding the property is:

layout.prop(bpy.data.objects["Armature"], '["prop"]')

You can add your custom property like this:

import bpy

class MyPanel(bpy.types.Panel):
    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOLS"
    bl_label = "MyPanel"
    bl_category = "MyPanel"

    def draw(self, context):
        layout = self.layout
        layout.prop(bpy.data.objects["Armature"], '["prop"]')


classes = (MyPanel,)


def register():
    for cls in classes:
        bpy.utils.register_class(cls)


def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)

if __name__ == "__main__":
    register()

Screenshot UI

$\endgroup$
5
  • 1
    $\begingroup$ Your code should check though whether an object called "Armature" actually exists and if it has a custom property called "prop". $\endgroup$ Commented Aug 17, 2019 at 22:29
  • 1
    $\begingroup$ Have you tested this out? Writing layout.prop(bpy.data.objects["Armature"], '["prop"]') doesn't work. The property doesn't get displayed. $\endgroup$
    – user56873
    Commented Aug 17, 2019 at 22:34
  • $\begingroup$ Yes I've tested it. Have you checked that your armature and property are actually named like that? $\endgroup$ Commented Aug 17, 2019 at 22:36
  • $\begingroup$ I needed to reference the armature like this bpy.data.armatures["Armature"]. This fixed it. Thanks for the help! $\endgroup$
    – user56873
    Commented Aug 17, 2019 at 22:39
  • 1
    $\begingroup$ @LennyWhite in that case you've added the custom property in the armature tab instead of the object tab. Then you need to access it like that. $\endgroup$ Commented Aug 17, 2019 at 22:42
0
$\begingroup$

First, assign "foobar" to an object:

obj["foobar"] = 3

then in a panel draw() function:

layout.prop(obj, f'["{bpy.utils.escape_identifier("foobar")}"]')

Also note: It appears the syntax is sensitive to the use of inner quoations - there have to be single double quotes " inside the bracket, single quotes ' or triple double/single quotes """/''' wont work


Previous answer by Robert appears to be non-functioning in Blender 3.6.2 Hash: e53e55951e7a. To get it to run without errors during registration, you have to remove the line:

bl_category = "MyPanel"

and even then you get:

rna_uiItemR: property not found: Object.["prop"]
$\endgroup$

You must log in to answer this question.