Obesity is a prominent risk contributor to health. However, few studies have explored the nonlinear relationships between commuting and built environments, particularly around workplaces, with obesity. Based on a sample of 1080 respondents in Shanghai, China, we used the gradient-boosting decision trees model to investigate the relative contributions and nonlinear relationships of commuting and built environments surrounding residences and workplaces with body mass index (BMI). Results showed that the built environment made greater contributions to BMI than sociodemographics and behavioral attributes. The built environment around workplaces was more important than that around residences, and commuting behavior rather than exercise or food intake contributed more. Gender and age were the most important predictors among all predictors. Moreover, most predictors were nonlinearly related to BMI. Car and bus commuters had higher BMI. Commuting distance (<20 km) was positively related to BMI and the duration of active modes (<20 min) was negatively associated with BMI. Around workplaces, green space (<0.28 km2) and population density (10,000–28,000 people/km2) had negative associations with BMI. Land use diversity around workplaces (>0.5) had a U-shaped relationship with BMI and the threshold was 0.78, but it around residences (0.82–0.85) had a positive effect. A longer distance to the subway station from residences (<1500 m) was associated with lower BMI. Men had higher BMI and age (24–47 years) was positively correlated with BMI. Beyond the thresholds, these predictors had limited contributions to BMI. We concluded that prioritizing interventions in the built environment around workplaces and commuting behavior to reach effective thresholds is important to reduce obesity.