9
$\begingroup$

I was going through the "Getting started" API documentation for 2.68 since I'm guessing 2.70 is similar. I was testing out how to create my first script in 2.70 which I would like to save values which I will need later for future computations. I am using the Blender Panel UI for the user to modify the properties. Here is the basic code I'm stuck at from the HellowWorldPanel class. I was wondering why my property field isn't showing up in the Panel. (Edit: I'm sorry just to clarify this, the panel is showing up with my "Hello world!" label but my property called "bl_mine1" isn't showing up in the newly created panel)

Here is the code I am testing it with:

import bpy


class HelloWorldPanel(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "Hello World Panel"
    bl_idname = "OBJECT_PT_hello"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "object"
    bl_mine1 = bpy.props.FloatProperty(name = "MyProperty")

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

        obj = context.object
        #Create New Row with text Hello World
        row = layout.row()
        row.label(text="Hello world!", icon='WORLD_DATA')
        #Try showing a field box with MYProperty in it
        row = layout.row()
        row.prop(obj, "bl_mine1")

def register():
    bpy.utils.register_class(HelloWorldPanel)


def unregister():
    bpy.utils.unregister_class(HelloWorldPanel)


if __name__ == "__main__":
    register()

Thanks for any input.

$\endgroup$
10
$\begingroup$

Since your example panel is in the object context I assume you want to use the data on objects.

If you want to store and retrieve properties on a per object basis you can append a property to objects like this:

bpy.types.Object.obj_property = bpy.props.FloatProperty(name = "ObjectProperty")

and access the value in the UI like this:

row.prop(context.object, "obj_property")

Or if you need your property for all objects in a scene you could store it inside the scene like this:

bpy.types.Scene.scn_property = bpy.props.FloatProperty(name = "SceneProperty")

and access the value in the UI like this:

row.prop(context.scene, "scn_property")

To access the property values in your script you can also use the bpy.data module.

Note that operators and panels are conceptually different. Panels are not supposed to store properties or change them on their own. They are just for user access to internal data and properties. Operators on the other hand have builtin access to properties and are better suited to manipulate data. Note also that properties are automatically stored in the .blend file while saving. If you want to share data between scenes you could use other ID blocks like Text or use a simple operator to copy data between scenes e.g. like this:

import bpy

class UIPanel(bpy.types.Panel):
  bl_label = "My Panel"
  bl_space_type = "PROPERTIES"
  bl_region_type = "WINDOW"
  bl_context = "object"
  bpy.types.Scene.scn_property = bpy.props.FloatProperty(name = "SceneProperty")

  def draw(self, context):
    self.layout.operator("prop.propcopy", text='Update Scenes')
    self.layout.prop(context.scene,"scn_property")

class OBJECT_OT_PropCopyButton(bpy.types.Operator):
  bl_idname = "prop.propcopy"
  bl_label = "Propcopy"

  def execute(self, context):
    sc0 = context.scene
    for sc in bpy.data.scenes:
        sc.scn_property = sc0.scn_property
    return{'FINISHED'}

bpy.utils.register_module(__name__)
$\endgroup$

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.