diff --git a/space_view3d_paint_bprojection.py b/space_view3d_paint_bprojection.py index 69aa7f009ac677c880fb049c19ca1de7da5b1e36..9189999fdbead022cc678a291c923ce2d8d471ff 100644 --- a/space_view3d_paint_bprojection.py +++ b/space_view3d_paint_bprojection.py @@ -9,6 +9,7 @@ bl_info = { "category": "Paint"} import bpy +from bpy.app.handlers import persistent from bpy.types import Panel, Operator from bpy.props import IntProperty, FloatProperty, BoolProperty, IntVectorProperty, StringProperty, FloatVectorProperty, CollectionProperty from bpy_extras import view3d_utils @@ -1223,19 +1224,15 @@ class RotateView3D(Operator): return{'FINISHED'} def invoke(self, context, event): - try: - bpy.data.objects['Empty for BProjection'] - context.window_manager.modal_handler_add(self) - self.first_mouse = Vector((event.mouse_region_x,event.mouse_region_y)) - self.first_time = True - for sub in context.object.modifiers: - if sub.type in ['SUBSURF', 'MULTIRES']: - self.tmp_level = sub.levels - sub.levels = 0 - return {'RUNNING_MODAL'} - except: - reinitkey() - return {'RUNNING_MODAL'} + bpy.data.objects['Empty for BProjection'] + context.window_manager.modal_handler_add(self) + self.first_mouse = Vector((event.mouse_region_x,event.mouse_region_y)) + self.first_time = True + for sub in context.object.modifiers: + if sub.type in ['SUBSURF', 'MULTIRES']: + self.tmp_level = sub.levels + sub.levels = 0 + return {'RUNNING_MODAL'} # Oprerator Class to pan the view3D @@ -1284,20 +1281,17 @@ class PanView3D(bpy.types.Operator): return {'RUNNING_MODAL'} def invoke(self, context, event): - try: - bpy.data.objects['Empty for BProjection'] - context.window_manager.modal_handler_add(self) - self.first_mouse.x = event.mouse_region_x - self.first_mouse.y = event.mouse_region_y - for sub in context.object.modifiers: - if sub.type in ['SUBSURF', 'MULTIRES']: - self.tmp_level = sub.levels - sub.levels = 0 - - return {'RUNNING_MODAL'} - except: - reinitkey() - return {'RUNNING_MODAL'} + bpy.data.objects['Empty for BProjection'] + context.window_manager.modal_handler_add(self) + self.first_mouse.x = event.mouse_region_x + self.first_mouse.y = event.mouse_region_y + for sub in context.object.modifiers: + if sub.type in ['SUBSURF', 'MULTIRES']: + self.tmp_level = sub.levels + sub.levels = 0 + + return {'RUNNING_MODAL'} + def execute(self, context): align_to_view(context) @@ -1315,46 +1309,42 @@ class ZoomView3D(Operator): default=1.0) def invoke(self, context, event): - try: - ob = context.object - em = bpy.data.objects[BProjection_Empty] - sd = context.space_data - - width = context.area.regions[4].width - height = context.area.regions[4].height - - r3d = sd.region_3d - v_init = Vector((0.0,0.0,1.0)) - - pos = [width,height] - vtr_b = view3d_utils.region_2d_to_location_3d(context.region, r3d, pos, v_init) - pos = [0,0] - vbl_b = view3d_utils.region_2d_to_location_3d(context.region, r3d, pos, v_init) - len_b = vtr_b - vbl_b - - bpy.ops.view3d.zoom(delta = self.delta) - r3d.update() - - pos = [width,height] - vtr_a = view3d_utils.region_2d_to_location_3d(context.region, r3d, pos, v_init) - pos = [0,0] - vbl_a = view3d_utils.region_2d_to_location_3d(context.region, r3d, pos, v_init) - len_a = vtr_a - vbl_a - - fac = len_a.length/len_b.length - r3d.view_location -= ob.location - r3d.view_location *= fac - r3d.view_location += ob.location - vres = Vector((em.custom_location.x*fac,em.custom_location.y*fac,em.custom_location.z)) - em.custom_location = vres - - - align_to_view(context) - - return {'FINISHED'} - except: - reinitkey() - return {'RUNNING_MODAL'} + ob = context.object + em = bpy.data.objects[BProjection_Empty] + sd = context.space_data + + width = context.area.regions[4].width + height = context.area.regions[4].height + + r3d = sd.region_3d + v_init = Vector((0.0,0.0,1.0)) + + pos = [width,height] + vtr_b = view3d_utils.region_2d_to_location_3d(context.region, r3d, pos, v_init) + pos = [0,0] + vbl_b = view3d_utils.region_2d_to_location_3d(context.region, r3d, pos, v_init) + len_b = vtr_b - vbl_b + + bpy.ops.view3d.zoom(delta = self.delta) + r3d.update() + + pos = [width,height] + vtr_a = view3d_utils.region_2d_to_location_3d(context.region, r3d, pos, v_init) + pos = [0,0] + vbl_a = view3d_utils.region_2d_to_location_3d(context.region, r3d, pos, v_init) + len_a = vtr_a - vbl_a + + fac = len_a.length/len_b.length + r3d.view_location -= ob.location + r3d.view_location *= fac + r3d.view_location += ob.location + vres = Vector((em.custom_location.x*fac,em.custom_location.y*fac,em.custom_location.z)) + em.custom_location = vres + + + align_to_view(context) + + return {'FINISHED'} def execute(self, context): align_to_view(context) @@ -1368,35 +1358,36 @@ class PresetView3D(Operator): view = StringProperty(name="View", description="Select the view", default='TOP') def invoke(self, context, event): - try: - ob = context.object - em = bpy.data.objects[BProjection_Empty] - origine = ob.location - sd = context.space_data - - vr_b = sd.region_3d.view_rotation.copy() - vr_b.invert() - pos_init = sd.region_3d.view_location - origine - sd.region_3d.view_location = origine - - tmp = context.user_preferences.view.smooth_view - context.user_preferences.view.smooth_view = 0 - bpy.ops.view3d.viewnumpad(type=self.view) - align_to_view(context) - context.user_preferences.view.smooth_view = tmp - - vr_a = sd.region_3d.view_rotation.copy() - pos_init.rotate(vr_a*vr_b) - sd.region_3d.view_location = pos_init + origine - - return {'FINISHED'} - except: - reinitkey() - return {'RUNNING_MODAL'} + ob = context.object + em = bpy.data.objects[BProjection_Empty] + origine = ob.location + sd = context.space_data + + vr_b = sd.region_3d.view_rotation.copy() + vr_b.invert() + pos_init = sd.region_3d.view_location - origine + sd.region_3d.view_location = origine + + tmp = context.user_preferences.view.smooth_view + context.user_preferences.view.smooth_view = 0 + bpy.ops.view3d.viewnumpad(type=self.view) + align_to_view(context) + context.user_preferences.view.smooth_view = tmp + + vr_a = sd.region_3d.view_rotation.copy() + pos_init.rotate(vr_a*vr_b) + sd.region_3d.view_location = pos_init + origine + + return {'FINISHED'} + +@persistent +def load_handler(dummy): + reinitkey() def register(): bpy.utils.register_module(__name__) createcustomprops(bpy.context) + bpy.app.handlers.load_post.append(load_handler) def unregister(): bpy.utils.unregister_module(__name__)