Localization plays an important role in many applications of wireless sensor networks. Recently, mobile anchor assisted localization methods become promising, and the moving trajectory planning of anchor is an interesting and basic issue in these methods. In this paper, an obstacle-avoidance trajectory planning method for three-dimensional wireless sensor networks is proposed. After dividing the network into grids, a depth-first-search algorithm with greedy strategy is proposed to get the approximately shortest path, and a trigonal function based localization method is presented to estimate the positions of the sensor nodes. Simulations show that this method can obtain almost the optimal path and localize almost all the sensor nodes.