I’m attempting to set up a panel that is only drawn when an object selected has the ‘Domain’ fluid type attached. The problem is that when another object is selected without a fluid modifier, it throws an error because it can’t find whether to display or not.
Here is the error:
Python: Traceback (most recent call last):
line 470, in draw
KeyError: ‘bpy_prop_collection[key]: key “Fluid” not found’
There are a couple of ways you can deal with this… A lot of the blender types have a ‘poll’ function that checks if some conditions are met before enabling what ever it is.
You can also use a try: block, which will catch errors and allow you to continue.
In your case though you probably just want to use use getattr() around the first part of your if statement. Just do a search on getattr and see how you use it, but it would be something like:
if getattr(bpy.context.object.modifiers, "Fluid", False) and <your current condition>:
This will not work as OP is hitting a KeyError (not an attribute error), because the modifiers object is a property collection and thus- accessed like a dictionary.
if 'Fluid' in your_object.modifiers would work in this situation.