-1

Probably this is a very newbie question, but I am having issues trying to access the value of a FloatProperty.

I have something like:

    float_property = bpy.props.FloatProperty(
        name='Test float property',
        description='This is an example of a FloatProperty used by a UserExtension.',
        default=0.0
        )

    def export(self, blender_object, export_settings) -> Extension:
        print("Example extension")

        print(self.float_property)
        print("Float value", self.float_property[0]())
        return None

Printing the float_property directly shows the following

(<built-in function FloatProperty>, {'name': 'Test float property', 'description': 'This is an example of a FloatProperty used by a UserExtension.', 'default': 0.0, 'attr': 'export_ExampleExtension_float'})

and printing the self.float_property[0]() shows this

Float value (<built-in function FloatProperty>, {})

For a BoolProperty I can simply do

if aBoolProperty:
   print("bool property is true")

so the question is: How am I supposed to read the float value of the property?

jjcasmar
  • 359
  • 1
  • 4
  • 15
  • Why the index operator? Just try self.float_property if your property is part of an operator...? – brockmann Oct 11 '19 at 14:31
  • Have you even read my question? Ive already tried that and I have post the result of doing it – jjcasmar Oct 11 '19 at 17:18
  • Yep, read it and my comment will solve your issue (because that's how you get the value). Thanks for your friendly comment rather than answering my questions... – brockmann Oct 11 '19 at 18:05
  • As said in the question, Ive already tried that with this result: (<built-in function FloatProperty>, {'name': 'Test float property', 'description': 'This is an example of a FloatProperty used by a UserExtension.', 'default': 0.0, 'attr': 'export_ExampleExtension_float'}) This code is not executed inside an operator. This is a class with an export method which is called by an operator. I want to access the property value inside the export method. Sorry for my previous comment, it was very aggresive. – jjcasmar Oct 11 '19 at 20:37

2 Answers2

1

You can access the value of any declared operator property within all provided methods.

The following demo is based on Templates > Python > Operator File Export. Once the execute method is called by the user (when hitting the 'Execute Some Data' button) the values/states of both user properties (in this case my_float and my_enum) are going to be printed into the console:

import bpy

# ExportHelper is a helper class, defines filename and
# invoke() function which calls the file selector.
from bpy_extras.io_utils import ExportHelper
from bpy.props import StringProperty, BoolProperty, EnumProperty
from bpy.types import Operator


class ExportSomeData(Operator, ExportHelper):
    """This appears in the tooltip of the operator and in the generated docs"""
    bl_idname = "export_test.some_data"  # important since its how bpy.ops.import_test.some_data is constructed
    bl_label = "Export Some Data"

    # ExportHelper mixin class uses this
    filename_ext = ".txt"

    filter_glob: StringProperty(
        default="*.txt",
        options={'HIDDEN'},
        maxlen=255,  # Max internal buffer length, longer would be clamped.
    )

    # List of operator properties, the attributes will be assigned
    # to the class instance from the operator settings before calling.
    my_float: bpy.props.FloatProperty(
        name="My float",
        description="",
        default=0.0
    )
    my_enum: EnumProperty(
        name="Example Enum",
        description="Choose between two items",
        items=(
            ('OPT_A', "First Option", "Description one"),
            ('OPT_B', "Second Option", "Description two"),
        ),
        default='OPT_A',
    )

    # Methods
    def write_some_data(self, context, filepath, use_some_setting):
        print("running write_some_data...")
        f = open(filepath, 'w', encoding='utf-8')
        f.write("Hello World %s" % use_some_setting)
        f.close()

    def execute(self, context):
        # Print property values
        print ("User values:", self.my_float, self.my_enum)
        # Write the data
        self.write_some_data(context, self.filepath, self.my_float)
        return {'FINISHED'}


# Only needed if you want to add into a dynamic menu
def menu_func_export(self, context):
    self.layout.operator(ExportSomeData.bl_idname, text="Text Export Operator")


def register():
    bpy.utils.register_class(ExportSomeData)
    bpy.types.TOPBAR_MT_file_export.append(menu_func_export)


def unregister():
    bpy.utils.unregister_class(ExportSomeData)
    bpy.types.TOPBAR_MT_file_export.remove(menu_func_export)


if __name__ == "__main__":
    register()

    # test call
    bpy.ops.export_test.some_data('INVOKE_DEFAULT')

Also read: What do operator methods do? (poll, invoke, execute, draw & modal)

brockmann
  • 12,613
  • 4
  • 50
  • 93
  • So I can only access properties values from Operators? – jjcasmar Oct 13 '19 at 13:48
  • In case the property is part of an operator, it is meant to be a user property for interaction purposes. In case your property is registered e.g. per scene, you can get the value from everywhere in your script @jjcasmar. – brockmann Oct 13 '19 at 15:51
  • Possibly at issue here is the property gets "wired up" akin to Foo.xxx = property(getter, setter) when it is bpy.utils.register_class(Foo) registered. Until then it looks akin to the property method. @jjcasmar if you post minimal working code in your questions it will help avoid such confusion. – batFINGER Jan 10 '20 at 02:31
0

I had a problem to read an operator FloatProperty doing print(self.prop) it was just print(self.prop[:])