1
$\begingroup$

I'm trying to create an addon for Blender. The addon will have an operator part that builds a mesh and also a panel part that lets the user configure it.

One thing I want to do with the panel is have a drop down property that lets the user select the name of a reference object in the scene. I found some examples showing how to add this to your panel using the layout.prop_search() method. However, I wasn't able to find any information on how you are supposed to retrieve the value after the user sets it. Right now I'm just trying to have my operator fetch the value the user set and print it to the console.

enter image description here

When I try, I get the message

(<built-in function StringProperty>, {'attr': 'theChosenObject'})

Also, is this the best way to go about doing this? It looks to me that I'm cluttering up Blender's object space by having my addon insert all it's 'global' variables into bpy.types.Scene.

Here's my source:

bl_info = {
    "name": "Build It", 
    "category": "Object"
}

import bpy

class BuildItPanel(bpy.types.Panel):
    bl_label = "Build It"
    bl_idname = "VIEW_3D_TOOLS_build_it"
    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOLS"
    bl_category = "Tools"

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

        scene = context.scene

        col = layout.column(align=True)
        col.prop_search(scene, "referenceMesh", scene, "objects")
        col.operator("object.build_it", text="Do Build")

class BuildItOp(bpy.types.Operator):
    bl_idname = "object.build_it"
    bl_label = "Build It"
    bl_options = {"UNDO"}

    def invoke(self, context, event):
#        print(BuildItPanel.bl_category)
        print(bpy.types.Scene.referenceMesh)
        return {"FINISHED"}

def register():
    bpy.utils.register_class(BuildItPanel)
    bpy.utils.register_class(BuildItOp)
    bpy.types.Scene.referenceMesh = bpy.props.StringProperty()


def unregister():
    bpy.utils.unregister_class(BuildItPanel)
    bpy.utils.unregister_class(BuildItOp)
    del bpy.types.Object.referenceMesh



# This allows you to run the script directly from blenders text editor
# to test the addon without having to install it.
if __name__ == "__main__":
    register()
$\endgroup$
  • $\begingroup$ regarding the clutter of the object namespace, as you mention, search how to use 'ID properties' , they are great for storing per-object information, (ints, strings, lists of ints/strings) $\endgroup$ – zeffii Jul 25 '15 at 7:03
2
$\begingroup$

You have one small mistake: you are reading the function that creates properties for new classes, while you should be accessing the property itself in the current scene, which has the value you have stored:

print(context.scene.referenceMesh) instead of print(bpy.types.Scene.referenceMesh)

$\endgroup$
  • 1
    $\begingroup$ Although this isn't really OOP, you can think of bpy.types.* as classes. The objects in bpy.data and bpy.context are instances of said classes. The "classes" do not store the specific values, as they are local to every instance, like Chebhou said. $\endgroup$ – CoDEmanX Jul 25 '15 at 14:33

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.