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