0
$\begingroup$

From what I can find this appears to be an issue with what Blender allows access to at the time the addon loads. Posts such as this:

Accessing bpy.context gives a '_RestrictContext' warning, how to fix this?

Suggest that access is restricted due to the the problem with cross functional addons. That makes sense, and I understand the restriction. What I can't find is how to work within that limitation.

In my situation the addon is really an overlay application that user will only run this one thing. So I actually do want to modify the working environment for the whole application. In this instance changing from metric to imperial units. I've tried two ways and both give the same error:

bpy.context.scene.unit_settings.system = 'IMPERIAL'
bpy.data.scenes["Scene"].unit_settings.system = 'IMPERIAL'

How can I make this change from an addon perspective? No matter how it's done this is a global change (at least from a scene perspective) and is going to affect other addons anyway.

$\endgroup$
0
$\begingroup$

You shouldn't access or change properties before the add-on is properly loaded. The unit settings can be changed for example in an operator as can be seen below:

bl_info = {
    "name": "Make Imperial",
    "author": "Robert Guetzkow",
    "version": (1, 0),
    "blender": (2, 80, 0),
    "location": "Dummy panel in View3D",
    "description": "Changes the unit settings to imperial",
    "warning": "",
    "wiki_url": "",
    "category": "3D View"}

import bpy


class EXAMPLE_OT_make_imperial(bpy.types.Operator):
    bl_idname = "example.make_imperial"
    bl_label = "Set unit settings to imperial"
    bl_options = {'REGISTER', 'UNDO'}

    def execute(self, context):
        bpy.context.scene.unit_settings.system = 'IMPERIAL'
        return {'FINISHED'}


class EXAMPLE_PT_panel(bpy.types.Panel):
    bl_label = "My own addon"
    bl_category = "Name of your tab"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"

    def draw(self, context):
        layout = self.layout
        layout.operator(EXAMPLE_OT_make_imperial.bl_idname)


classes = (EXAMPLE_PT_panel, EXAMPLE_OT_make_imperial)


def register():
    for cls in classes:
        bpy.utils.register_class(cls)


def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)


if __name__ == "__main__":
    register()
$\endgroup$
  • $\begingroup$ That was the problem... I was setting all of this up in my headers, prior to register being run on all the classes. Once this was moved to the end of the register function, then it allowed these settings to be modified. $\endgroup$ – Sam Vimes Oct 2 at 23:19

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.