0
$\begingroup$

I would like to have a StringProperty of subtype DIR_PATH initialized to a default of bpy.path.abspath('//'). The property is on an operator with a poll() method that checks to ensure the Blender file has been saved first. This stops working if my add-on is installed, Blender throws a AttributeError: '_RestrictData' object has no attribute 'filepath' error. I am aware that _RestrictData is an artifact of the class registration process however I have no idea how to work around it in this case.

bl_info = {
    "name": "Example to show abspath error",
    "version": (0, 0, 1),
    "blender": (3, 0, 0),
    "category": "Import-Export",
    "location": "View3D > UI > MyCompany",
    "description": "Sample code for Stack Exchange to illustrate problem with abspath and a StringProperty default value"
}

import bpy

class MYCOMPANY_OT_DummyOp(bpy.types.Operator):
    bl_label="Dummy class to show error"
    bl_idname = "mycompany.dummyop"
    ExportFolder: bpy.props.StringProperty(name="Export dir", description="Directory to write out exported file", default=bpy.path.abspath('//'), subtype='DIR_PATH')

    @classmethod
    def poll(cls, C):
        if not bpy.data.filepath:
            cls.poll_message_set("Current .BLEND file hasn't been saved, this is necessary for operation")
            return False
        return True

    def execute(self, C):
        return {'FINISHED'}


class MYCOMPANY_PT_TestPanel(bpy.types.Panel):
    bl_label="Test Panel"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"

    def draw(self, context):
        L=self.layout
        L.operator("mycompany.dummyop")


classesToRegister = (
    MYCOMPANY_OT_DummyOp,
    MYCOMPANY_PT_TestPanel
    )

def register():
    for c in classesToRegister:
        bpy.utils.register_class(c)

def unregister():
    for c in reversed(classesToRegister):
        bpy.utils.unregister_class(c)

if __name__ == "__main__":
    register()
$\endgroup$
2
  • $\begingroup$ Do you mean installed as an add-on? Your test code can't be installed as an addon because it lacks a bl_info structure. However when I add one I can reproduce your error. You can't do what you want because abspath can't create a path at registration time. The best you can do is use the installation directory path as a default. $\endgroup$ Commented May 5, 2022 at 23:02
  • $\begingroup$ I originally left bl_info out for the sake of brevity, I've now added it back in. $\endgroup$ Commented May 5, 2022 at 23:30

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.