Python 'RestricContext' problem

Hello,
i am a beginner on blender . i am trying to make a scrypt which cut an object into few object . Its works when i execute it from the text editor but if i try to add it with the custom > add-ons > folder, it load but when i press my button on the pannel i’ve got an error " AttributeError: ’ _RestrictContext’ objet has no attribute ‘object’ Line 56 . You can try it on the cube, paste on text editor and execute, a new tab called "Bricolage"will appears on the left pannel , the button in it will cut your object then you can try to load it like a classic add-ons and you’ll see the error. Sorry for language mistakes, i tried to be understandable.

import bpy, bmesh
from bpy import context as C
from bpy.types import Panel
bl_info = {
    "name": "DecoupeObj",
    "author": "Marc Durrenmatt",
    "version": (1, 0),
    "blender": (2, 75, 0),
    "location": "View3D > Add > Mesh > New Object",
    "description": "coupe un objet",
    "warning": "",
    "wiki_url": "",
    "category": "Add Mesh",
    }

class SimpleToolPanel(Panel):
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'
    bl_label = 'BricoKit'
    bl_context = 'objectmode'
    bl_category = 'Bricolage'
    def draw(self, context):
        layout = self.layout
        layout.operator("object.simple_operator" , text='DECOUPE' , icon="MODIFIER")

def main(context,bm):
    bpy.ops.object.mode_set(mode='EDIT')
    bm = bmesh.from_edit_mesh(C.object.data)

    edges = []
    for i in range(-10, 10, 2):
        ret = bmesh.ops.bisect_plane(bm, geom=bm.verts[:]+bm.edges[:]+bm.faces[:], plane_co=(i,0,0), plane_no=(-1,0,0))
        bmesh.ops.split_edges(bm, edges=[e for e in ret['geom_cut'] if isinstance(e, bmesh.types.BMEdge)])

    for i in range(-10, 10, 2):
        ret = bmesh.ops.bisect_plane(bm, geom=bm.verts[:]+bm.edges[:]+bm.faces[:], plane_co=(0,i,0), plane_no=(0,1,0))
        bmesh.ops.split_edges(bm, edges=[e for e in ret['geom_cut'] if isinstance(e, bmesh.types.BMEdge)])


    bmesh.update_edit_mesh(C.object.data)

    bpy.ops.mesh.separate(type='LOOSE')
    bpy.ops.object.mode_set(mode='OBJECT')

class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

    @classmethod
    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):
        bpy.ops.object.mode_set(mode='EDIT')
        C=context
        bm = bmesh.from_edit_mesh(C.object.data)
        main(context,bm)
        return {'FINISHED'}


def register():
    bpy.utils.register_class(SimpleToolPanel)
    bpy.utils.register_class(SimpleOperator)


def unregister():
    bpy.utils.register_class(SimpleToolPanel)
    bpy.utils.unregister_class(SimpleOperator)


if __name__ == "__main__":
    register()

    # test call
    #bpy.ops.object.simple_operator()

Thanks

First: when pasting python code here in the forum, please enclose it with ‘CODE’ tags. Without it, indentation is lost (a bit crucial for python!)

Second: you are using C.object, but haven’t defined C. The C variable is defined in the console as a pointer to bpy.context, but when running scripts from anywhere else you need to explicitly set ‘C=bpy.context’ before using it like that (or ‘C=context’ when the context is passed to the function).

Thank you very much man, i solved my problem

You’re welcome.
Also, in this situation, a better approach is to directly point to the object or even to the object data…

dat = context.active_object.data
bm = bmesh.from_edit_mesh(dat)

… And you still have an invalid ‘C’ reference in the ‘main’ function. :wink: