Unmanned aerial vehicle (UAV)-assisted multi-access edge computing (MEC) networks significantly improve Internet of Things (IoT) communication in infrastructure-limited environments. However, in large-scale, complex, and dynamic 3D environments, uncertain sensor information and unexpected obstacles pose serious challenges to UAV flight safety, thereby necessitating robust and fast-responding trajectory optimization methods. This study presents a hierarchical trajectory generation and optimization scheme for UAV-assisted MEC in complex environments. Different from existing works, our scheme incorporates constraints from the UAV’s realistic dynamics model and a communication-computation energy consumption model. A global trajectory is first generated using an improved cost-function-based rapidly-exploring random tree star (RRT*) algorithm, followed by refinement via nonlinear model predictive control for high-precision smoothing. In addition, an attention-enhanced double deep Q-network (DDQN) module is employed as the high-level controller within the hierarchical trajectory optimization framework, providing optimized obstacle avoidance strategies in the presence of uncertain obstacles. Simulation results show that: 1) Compared with traditional approaches, the proposed scheme generates trajectories that demonstrate improved smoothness while rigorously satisfying physical and safety constraints, thereby enhancing the overall reliability of flight operations; and 2) in scenarios involving uncertain obstacles, the proposed framework effectively balances multiple objectives, including flight efficiency, obstacle avoidance safety, and energy consumption, leading to more robust and cost-effective aerial operation performance.