Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # LUCID JOINT ADDON
- import bpy
- import bgl
- from bpy.app.handlers import persistent
- from bpy.types import Operator
- from bpy.props import FloatVectorProperty, BoolProperty
- from bpy_extras.object_utils import AddObjectHelper, object_data_add
- from mathutils import Vector
- def draw_callback_px(self, context):
- pass
- # # 50% alpha, 2 pixel width line
- # bgl.glEnable(bgl.GL_BLEND)
- # bgl.glColor4f(0.0, 0.0, 0.0, 0.5)
- # bgl.glLineWidth(2)
- # bgl.glBegin(bgl.GL_LINE_STRIP)
- # for obj in bpy.data.objects:
- # l = obj.location
- # try:
- # c = obj.constraints
- # if "Child Of" in c:
- # l = c["Child Of"].target.location + l
- # except AttributeError as ex:
- # pass
- # bgl.glVertex3f(l.x, l.y, l.z)
- # bgl.glEnd()
- # # restore opengl defaults
- # bgl.glLineWidth(1)
- # bgl.glDisable(bgl.GL_BLEND)
- # bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
- def add_object(self, context):
- def unselect_all():
- bpy.ops.object.select_all(action="DESELECT")
- selected = bpy.context.selected_objects
- if len(selected) is not 2:
- return True
- 'FINISHED' in bpy.ops.object.empty_add(type='PLAIN_AXES', location=(0, 0, 0))
- joint = bpy.context.active_object
- 'FINISHED' in bpy.ops.object.empty_add(type='PLAIN_AXES', location=(0, 0, 0))
- a = bpy.context.active_object
- 'FINISHED' in bpy.ops.object.empty_add(type='PLAIN_AXES', location=(0, 0, 0))
- b = bpy.context.active_object
- a.protected = True
- b.protected = True
- joint.select = True
- a.select = True
- b.select = True
- bpy.context.scene.objects.active = joint
- bpy.ops.object.parent_set()
- unselect_all()
- bpy.context.scene.objects.active = a
- selected[0].select = True
- bpy.ops.object.constraint_add_with_targets(type="CHILD_OF")
- unselect_all()
- bpy.context.scene.objects.active = b
- selected[1].select = True
- bpy.ops.object.constraint_add_with_targets(type="CHILD_OF")
- # return {'RUNNING_MODAL'}
- return False
- class OBJECT_OT_add_object(Operator, AddObjectHelper):
- """Create a new LUCID Joint"""
- bl_idname = "mesh.add_b2joint"
- bl_label = "Add LUCID Joint"
- bl_options = {'REGISTER', 'UNDO'}
- @classmethod
- def register(cls):
- # the arguments we pass the the callback
- # args = (self, context)
- # Add the region OpenGL drawing callback
- # draw in view space with 'POST_VIEW' and 'PRE_VIEW'
- pass
- def modal(self, context, event):
- context.area.tag_redraw()
- if event.type in {'ESC'}:
- bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
- return {'CANCELLED'}
- return {'PASS_THROUGH'}
- def execute(self, context):
- print("QWEQWEQWEQWEQWEQWEQWE")
- self._handle = bpy.types.SpaceView3D.draw_handler_add(draw_callback_px, (), 'WINDOW', 'POST_VIEW')
- bpy.context.window_manager.modal_handler_add(self)
- return {'FINISHED'}
- def invoke(self, context, event):
- return {'CANCELLED'} if add_object(self, context) else {'FINISHED'}
- # Registration
- def add_object_button(self, context):
- self.layout.operator(
- OBJECT_OT_add_object.bl_idname,
- text="LUCID Joint",
- icon='PLUGIN')
- #Protectedness follows
- bpy.types.Object.protected = BoolProperty(name = 'protected', default = False)
- def main_prot(context):
- for obj in context.selected_objects:
- if not obj.protected :
- bpy.context.scene.objects.unlink(obj)
- bpy.data.objects.remove(obj)
- else :
- print(obj.name +' is protected')
- class delete_override(bpy.types.Operator):
- """Tooltip"""
- bl_idname = "object.delete"
- bl_label = "Simple Object Operator"
- @classmethod
- def poll(cls, context):
- return context.active_object is not None
- def execute(self, context):
- main_prot(context)
- return {'FINISHED'}
- @persistent
- def load_handler(dummy):
- bpy.ops.mesh.add_b2joint()
- def register():
- bpy.utils.register_class(delete_override)
- bpy.utils.register_class(OBJECT_OT_add_object)
- bpy.types.INFO_MT_mesh_add.append(add_object_button)
- bpy.app.handlers.load_post.append(load_handler)
- bpy.ops.mesh.add_b2joint()
- def unregister():
- bpy.utils.unregister_class(delete_override)
- bpy.utils.unregister_class(OBJECT_OT_add_object)
- bpy.types.INFO_MT_mesh_add.remove(add_object_button)
- if __name__ == "__main__":
- register()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement