SHARE
TWEET

mesh_test.py

a guest Mar 6th, 2013 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. bl_info = {
  2.     "name": "Test",
  3.     "author": "Tester",
  4.     "version": (0, 1),
  5.     "blender": (2, 64, 0),
  6.     "location": "View3D > Toolbar and View3D > Specials (W-key) > Test",
  7.     "warning": "",
  8.     "description": "A mesh tool to test some vertex translation stuff",
  9.     "wiki_url": "http://wiki.blender.org/",
  10.     "tracker_url": "",
  11.     "category": "Mesh"}
  12.  
  13. import bpy
  14. import blf
  15. import bgl
  16. import mathutils
  17.  
  18. g_sel_vert_idx = -100
  19. g_sel_vert_pos = mathutils.Vector((0.0, 0.0, 0.0))
  20. g_sel_vert_init_pos_saved = False
  21.  
  22.  
  23. def draw_callback(s_e_l_f, context):
  24.     # draw instructions
  25.     font_id = 0
  26.     txt = "[Enter] to finish, [ESC] to cancel"
  27.     blf.size(font_id, 15, 72)
  28.     dim = blf.dimensions(font_id, txt)
  29.     w = context.region.width
  30.     blf.position(font_id, w - dim[0] - 30, 20, 0)
  31.     blf.draw(font_id, txt)
  32.  
  33.     obj = context.active_object
  34.     bpy.ops.object.mode_set(mode='OBJECT')
  35.  
  36.     global g_sel_vert_idx
  37.     global g_sel_vert_pos
  38.     global g_sel_vert_init_pos_saved
  39.     if not g_sel_vert_init_pos_saved:
  40.         g_sel_vert_pos = obj.data.vertices[g_sel_vert_idx].co
  41.         print("In draw_callback, saving co of the selected vertex " + str(g_sel_vert_idx) + ", co = " + str(g_sel_vert_pos))
  42.         print("-"*20)
  43.         g_sel_vert_init_pos_saved = True
  44.  
  45.     if g_sel_vert_idx >= 0 and obj.data.vertices[g_sel_vert_idx].co != g_sel_vert_pos:
  46.         print("Selected vertex " + str(g_sel_vert_idx))
  47.         print("Old coordinates: " + str(g_sel_vert_pos))
  48.         print("New coordinates: " + str(obj.data.vertices[g_sel_vert_idx].co))
  49.  
  50.         # for v_idx in range(len(obj.data.vertices)):
  51.         #     if v_idx != g_sel_vert_idx:
  52.         #         obj.data.vertices[v_idx].co += (obj.data.vertices[g_sel_vert_idx].co - g_sel_vert_pos)
  53.  
  54.         g_sel_vert_pos = obj.data.vertices[g_sel_vert_idx].co
  55.         print("Saved coordinates: " + str(g_sel_vert_pos))
  56.         print("-"*20)
  57.  
  58.     bpy.ops.object.mode_set(mode='EDIT')
  59.  
  60.  
  61. # --------------------------------------------------------------
  62. class TestOp(bpy.types.Operator):
  63.     bl_idname = "mesh.test_op"
  64.     bl_label = "Test"
  65.     bl_description = "Test"
  66.     bl_options = {'REGISTER', 'UNDO'}
  67.    
  68.     def __init__(self):
  69.         print("__init__")
  70.  
  71.     def __del__(self):
  72.         print("__del__")
  73.  
  74.     def invoke(self, context, event):
  75.         if context.area.type == 'VIEW_3D':
  76.             print("-"*20)
  77.             print("-"*20)
  78.             print("invoke")
  79.             context.window_manager.modal_handler_add(self)
  80.  
  81.             # Add the r3d OpenGL drawing callback
  82.             # draw in view space with 'POST_VIEW' and 'PRE_VIEW'
  83.             self._handle = context.region.callback_add(draw_callback, (self, context), 'POST_PIXEL')
  84.  
  85.             obj = context.active_object
  86.             # bpy.ops.object.editmode_toggle()
  87.             bpy.ops.object.mode_set(mode='OBJECT')
  88.  
  89.             for vert_idx in range(len(obj.data.vertices)):
  90.                 v = obj.data.vertices[vert_idx]
  91.                 if v.select:
  92.                     global g_sel_vert_idx
  93.                     global g_sel_vert_pos
  94.                     print("Vertex " + str(vert_idx) + " is selected")
  95.                     g_sel_vert_idx = vert_idx
  96.                     g_sel_vert_pos = v.co
  97.                     print("Saved position = " + str(g_sel_vert_pos))
  98.                     print("-"*20)
  99.                     break
  100.  
  101.             global g_sel_vert_init_pos_saved
  102.             g_sel_vert_init_pos_saved = False
  103.  
  104.             # bpy.ops.object.editmode_toggle()
  105.             bpy.ops.object.mode_set(mode='EDIT')
  106.  
  107.             return {'RUNNING_MODAL'}
  108.         else:
  109.             self.report({'WARNING'}, "view3d not found, cannot run operator (%s)" % context.area.type)
  110.             return {'CANCELLED'}
  111.  
  112.     def modal(self, context, event):
  113.         context.area.tag_redraw()
  114.  
  115.         if event.type in ('ESC'):
  116.             context.region.callback_remove(self._handle)
  117.             return {'CANCELLED'}
  118.         elif event.type in ('NUMPAD_ENTER', 'RET'):
  119.             context.region.callback_remove(self._handle)
  120.             return {'FINISHED'}
  121.  
  122.         return {'PASS_THROUGH'}
  123. #        return {'RUNNING_MODAL'}
  124.  
  125.     @classmethod
  126.     def poll(cls, context):
  127.         ob = context.active_object
  128.         return(ob and ob.type == 'MESH' and context.mode == 'EDIT_MESH')
  129. # --------------------------------------------------------------
  130.  
  131.  
  132. # registering and menu integration
  133. def register():
  134.     bpy.utils.register_class(TestOp)
  135.  
  136.  
  137. # unregistering and removing menus
  138. def unregister():
  139.     bpy.utils.unregister_class(TestOp)
  140.  
  141.  
  142. if __name__ == "__main__":
  143.     register()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top