0
$\begingroup$

I'm struggling to pass a variable to an EnumProperty items callback, resulting in making this variable a global, which I prefer to avoid. I want to be able to access the "plant" from the "model_callback", but I can't reference it as self.plant. I am unclear on the scope I'm running in within the callback.

Simplied example follows. Note that in the real example, the plant variable changes whenever the filepath is updated.

class ImportPlant(bpy.types.Operator, ImportHelper):
    bl_idname = "import_object.plant"
    bl_label = "Import Plant"
    # ...

    plant = ["spring", "summer"]

    def model_id_callback(self, context):
        items = []
        # Results in: AttributeError: 'IMPORT_OBJECT_OT_plant' object has no attribute 'plant'
        for model in self.plant:
            items.append((model, model, ""))
        return items

    model_id: EnumProperty(items=model_id_callback, name="Model")

    # ...

How can I access the plant variable in the model_id_callback without just making it a global?

$\endgroup$
4
  • 1
    $\begingroup$ The plant is a class attribute not an instance attribute, meaning that each object of type ImportPlant references the same plant. Therefore replacing self.plant with ImportPlant.plant should be the solution. (Currently on mobile, hence posted as comment and not as polished answer). $\endgroup$ Commented Jan 24, 2020 at 23:01
  • $\begingroup$ @RobertGützkow ah, I was confused because accessing the properties did work with self.filepath for example. Tested using the class name instead of self and it indeed works. This is slightly better than global :-) and since the ImportHelper is modal, I suspect it is sufficient. Thanks! $\endgroup$
    – dvhart
    Commented Jan 24, 2020 at 23:37
  • $\begingroup$ To test this further, I made plant a property: plant = property(get_plant, set_plant) and initialized it in __init__(). I can still refer to self.plant in the other methods of the class (draw, invoke, execute), but EnumProperty callback still reports: AttributeError: 'IMPORT_OBJECT_OT_lbw' object has no attribute 'plant'. Somehow, it feels like the callback is running in a different context. $\endgroup$
    – dvhart
    Commented Jan 25, 2020 at 3:40
  • 1
    $\begingroup$ Using a constructor in operators is a bit tricky, because the lifetime is limited to it's execution. I would have to look at your whole test script to see what's wrong. $\endgroup$ Commented Jan 25, 2020 at 15:45

1 Answer 1

1
$\begingroup$

The plant is a class attribute and not an instance attribute, meaning that each instance of the class ImportPlant references the same plant. Therefore your script should use ImportPlant.plant instead of self.plant.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .