0
$\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$
  • $\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$ – Robert Gützkow Aug 17 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$ – Lenny White Aug 17 at 22:09
  • $\begingroup$ Can you show the code that declares the custom property? $\endgroup$ – Robert Gützkow Aug 17 at 22:11
  • $\begingroup$ @rjg I didn't define the custom property. I added it via the viewport. $\endgroup$ – Lenny White Aug 17 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$ – Lenny White Aug 17 at 22:20
1
$\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$
  • $\begingroup$ Your code should check though whether an object called "Armature" actually exists and if it has a custom property called "prop". $\endgroup$ – Robert Gützkow Aug 17 at 22:29
  • $\begingroup$ Have you tested this out? Writing layout.prop(bpy.data.objects["Armature"], '["prop"]') doesn't work. The property doesn't get displayed. $\endgroup$ – Lenny White Aug 17 at 22:34
  • $\begingroup$ Yes I've tested it. Have you checked that your armature and property are actually named like that? $\endgroup$ – Robert Gützkow Aug 17 at 22:36
  • $\begingroup$ I needed to reference the armature like this bpy.data.armatures["Armature"]. This fixed it. Thanks for the help! $\endgroup$ – Lenny White Aug 17 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$ – Robert Gützkow Aug 17 at 22:42

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.