def mouseup(): # Loop until click mouse_buttons = Window.GetMouseButtons() while not mouse_buttons & LMB: sys.sleep(10) mouse_buttons = Window.GetMouseButtons() while mouse_buttons & LMB: sys.sleep(10) mouse_buttons = Window.GetMouseButtons()
def main(): scn = bpy.data.scenes.active ob = scn.objects.active if not ob or ob.type != 'Mesh': return is_editmode = Window.EditMode() if is_editmode: Window.EditMode(0) mousedown_wait() # so the menu items clicking dosnt trigger the mouseclick Window.DrawProgressBar(0.0, '') Window.DrawProgressBar(0.1, '(1 of 3) Click on a face corner') # wait for a click mouse_buttons = Window.GetMouseButtons() while not mouse_buttons & LMB: sys.sleep(10) mouse_buttons = Window.GetMouseButtons() # Allow for RMB cancel if mouse_buttons & RMB: return while mouse_buttons & LMB: sys.sleep(10) mouse_buttons = Window.GetMouseButtons() Window.DrawProgressBar(0.2, '(2 of 3 ) Click confirms the U coords') mousedown_wait() obmat = ob.matrixWorld screen_x, screen_y = Window.GetMouseCoords() mouseInView, OriginA, DirectionA = mouseViewRay(screen_x, screen_y, obmat) if not mouseInView or not OriginA: return me = ob.getData(mesh=1) # Get the face under the mouse face_click, isect, side = BPyMesh.pickMeshRayFace(me, OriginA, DirectionA) if not face_click: return proj_z_component = face_click.no if not face_click: return # Find the face vertex thats closest to the mouse, # this vert will be used as the corner to map from. best_v = None best_length = 10000000 vi1 = None for i, v in enumerate(face_click.v): l = (v.co - isect).length if l < best_length: best_v = v best_length = l vi1 = i # now get the 2 edges in the face that connect to v # we can work it out fairly easerly if len(face_click) == 4: if vi1 == 0: vi2, vi3 = 3, 1 elif vi1 == 1: vi2, vi3 = 0, 2 elif vi1 == 2: vi2, vi3 = 1, 3 elif vi1 == 3: vi2, vi3 = 2, 0 else: if vi1 == 0: vi2, vi3 = 2, 1 elif vi1 == 1: vi2, vi3 = 0, 2 elif vi1 == 2: vi2, vi3 = 1, 0 face_corner_main = face_click.v[vi1].co face_corner_a = face_click.v[vi2].co face_corner_b = face_click.v[vi3].co line_a_len = (face_corner_a - face_corner_main).length line_b_len = (face_corner_b - face_corner_main).length orig_cursor = Window.GetCursorPos() Window.SetCursorPos(face_corner_main.x, face_corner_main.y, face_corner_main.z) SHIFT = Window.Qual.SHIFT MODE = 0 # firstclick, 1, secondclick mouse_buttons = Window.GetMouseButtons() project_mat = Matrix([0, 0, 0], [0, 0, 0], [0, 0, 0]) def get_face_coords(f): f_uv = f.uv return [(v.co - face_corner_main, f_uv[i]) for i, v in enumerate(f.v)] if me.faceUV == False: me.faceUV = True coords = [(co, uv) for f in me.faces if f.sel for co, uv in get_face_coords(f)] coords_orig = [uv.copy() for co, uv in coords] USE_MODIFIER = using_modifier(ob) while 1: if mouse_buttons & LMB: if MODE == 0: mousedown_wait() Window.DrawProgressBar( 0.8, '(3 of 3 ) Click confirms the V coords') MODE = 1 # second click # Se we cont continually set the length and get float error proj_y_component_orig = proj_y_component.copy() else: break elif mouse_buttons & RMB: # Restore old uvs for i, uv_orig in enumerate(coords_orig): coords[i][1][:] = uv_orig break mouse_buttons = Window.GetMouseButtons() screen_x, screen_y = Window.GetMouseCoords() mouseInView, OriginA, DirectionA = mouseViewRay( screen_x, screen_y, obmat) if not mouseInView: continue # Do a ray tri intersection, not clipped by the tri new_isect = Intersect(face_corner_main, face_corner_a, face_corner_b, DirectionA, OriginA, False) new_isect_alt = new_isect + DirectionA * 0.0001 # The distance from the mouse cursor ray vector to the edge line_isect_a_pair = LineIntersect(new_isect, new_isect_alt, face_corner_main, face_corner_a) line_isect_b_pair = LineIntersect(new_isect, new_isect_alt, face_corner_main, face_corner_b) # SHIFT to flip the axis. is_shift = Window.GetKeyQualifiers() & SHIFT if MODE == 0: line_dist_a = (line_isect_a_pair[0] - line_isect_a_pair[1]).length line_dist_b = (line_isect_b_pair[0] - line_isect_b_pair[1]).length if line_dist_a < line_dist_b: proj_x_component = face_corner_a - face_corner_main y_axis_length = line_b_len x_axis_length = (line_isect_a_pair[1] - face_corner_main).length else: proj_x_component = face_corner_b - face_corner_main y_axis_length = line_a_len x_axis_length = (line_isect_b_pair[1] - face_corner_main).length proj_y_component = proj_x_component.cross(proj_z_component) proj_y_component.length = 1 / y_axis_length proj_x_component.length = 1 / x_axis_length if is_shift: proj_x_component.negate() else: proj_y_component[:] = proj_y_component_orig if line_dist_a < line_dist_b: proj_y_component.length = 1 / (line_isect_a_pair[1] - new_isect).length else: proj_y_component.length = 1 / (line_isect_b_pair[1] - new_isect).length if is_shift: proj_y_component.negate() # Use the existing matrix to make a new 3x3 projecton matrix project_mat[0][:] = -proj_y_component project_mat[1][:] = -proj_x_component project_mat[2][:] = proj_z_component # Apply the projection matrix for proj_co, uv in coords: uv[:] = (project_mat * proj_co)[0:2] if USE_MODIFIER: me.update() Window.Redraw(Window.Types.VIEW3D) Window.SetCursorPos(*orig_cursor) if is_editmode: Window.EditMode(1) Window.RedrawAll()
def mousedown_wait(): # If the menu has just been pressed dont use its mousedown, mouse_buttons = Window.GetMouseButtons() while mouse_buttons & LMB: mouse_buttons = Window.GetMouseButtons() sys.sleep(10)
def eventHandler(event, value): """ General GUI event handler. @param event: Event type. @param value: Value of the event. """ global G if event == Draw.WHEELDOWNMOUSE: setZoom(getWMCoords(), G.zoom * (1.0 - ZOOM_DELTA)) elif event == Draw.WHEELUPMOUSE: setZoom(getWMCoords(), G.zoom * (1.0 + ZOOM_DELTA)) elif event == Draw.MIDDLEMOUSE: if value: G.mousepos = Window.GetMouseCoords() else: G.mousepos = None elif event == Draw.MOUSEX or event == Draw.MOUSEY: mouseButs = Window.GetMouseButtons() if (mouseButs & Draw.MIDDLEMOUSE) and (G.mousepos is not None): nx, ny = Window.GetMouseCoords() dx, dy = nx - G.mousepos[0], ny - G.mousepos[1] G.mousepos = (nx, ny) G.imgpos = [int(G.imgpos[0] + dx), int(G.imgpos[1] + dy)] Draw.Redraw(1) elif G.mode == MODE_ADD: Draw.Redraw(1) elif G.mode == MODE_GRAB: G.havebupclik = True nx, ny = Window.GetMouseCoords() dx, dy = (nx - G.grabpos[0]) / G.zoom, (ny - G.grabpos[1]) / G.zoom G.grabpos = [nx, ny] def translate(x): name = x.getName() if G.coordmap.has_key(name): c = G.coordmap[name] G.coordmap[name] = (c[0] + dx, c[1] + dy) map(translate, G.selection) ## autocalibration.. gets stuck some times.. #if G.last_camera: # calibrate(G.last_camera) Draw.Redraw(1) elif (event == Draw.LEFTMOUSE) or (event == Draw.RETKEY): if (G.mode == MODE_ADD) and (value == 1): x, y = map(int, getWMCoords()) x -= 10 y += 10 G.coordmap[G.curempty.getName()] = wc2ic((x, y)) G.mode = MODE_NORMAL Draw.Redraw(1) elif (G.mode == MODE_GRAB) and (value == 1): G.mode = MODE_NORMAL Draw.Redraw(1) elif (event == Draw.RIGHTMOUSE) and (value == 1): if G.mode == MODE_NORMAL: xi, yi = wc2ic(getWMCoords()) closest = None for (emptyname, coord) in G.coordmap.items(): dist = math.sqrt((coord[0] - xi)**2 + (coord[1] - yi)**2) * G.zoom if (closest == None) or (dist < closest[0]): closest = (dist, emptyname) if closest[0] < MAX_SELECT_DIST: obj = Object.Get(closest[1]) kq = Window.GetKeyQualifiers() if (kq & Window.Qual.LSHIFT) or (kq & Window.Qual.RSHIFT): obj.select(True) else: map(lambda x: x.select(False), G.selection) obj.select(True) Window.RedrawAll() elif (event == Draw.AKEY) and (value == 1): if G.mode == MODE_NORMAL: someSelected = False for (emptyname, coord) in G.coordmap.items(): if Object.Get(emptyname).isSelected(): someSelected = True break newselect = (someSelected == False) map(lambda x: Object.Get(x[0]).select(newselect), G.coordmap.items()) Window.RedrawAll() elif (event == Draw.GKEY) and (value == 1): if G.mode == MODE_NORMAL: G.mode = MODE_GRAB G.grabpos = Window.GetMouseCoords() Draw.Redraw(1) elif event == Draw.UPARROWKEY and value == 1: p = Window.GetMouseCoords() Window.SetMouseCoords(p[0], p[1] + 1) elif event == Draw.DOWNARROWKEY and value == 1: p = Window.GetMouseCoords() Window.SetMouseCoords(p[0], p[1] - 1) elif event == Draw.LEFTARROWKEY and value == 1: p = Window.GetMouseCoords() Window.SetMouseCoords(p[0] - 1, p[1]) elif event == Draw.RIGHTARROWKEY and value == 1: p = Window.GetMouseCoords() Window.SetMouseCoords(p[0] + 1, p[1]) elif event == Draw.XKEY and value == 1: if len(G.selection) > 0: result = Draw.PupMenu('OK?%t|Erase selected') if result == 1: buttonEventHandler(BUTTON_DELETE) elif event == Draw.SPACEKEY and value == 1: if (G.curempty is not None) and not (G.coordmap.has_key( G.curempty.getName())): buttonEventHandler(BUTTON_ADD) elif event == Draw.ZKEY and value == 1: x, y, w, h = getWinRect() setZoom((w / 2, h / 2), 1.0) elif event == Draw.RKEY and value == 1: Draw.Redraw(1)