I want to model through Python Flask service,I created a cyclinder and obtained the object through bpy.context.active_object,Then the following error was reported:AttributeError: 'Context' object has no attribute 'active_object'.How to deal with it?

If I don’t use the Python Flask service, I won’t get an error.How should I handle this issue or model it through other means?
Here is the code:

import json
import math

import bpy
from flask import Flask, request, jsonify
from mathutils import Vector

app = Flask(name)

@app.route(‘/receive_json’, methods=[‘POST’])
def receive_json_data():
bpy.ops.mesh.primitive_cylinder_add(vertices=16, radius=1, depth=1, location=(0, 0, 3))
outer_cylinder = bpy.context.active_object
outer_cylinder.name = “Outer_Cylinder”

# 创建内层圆柱(镂空部分)
bpy.ops.mesh.primitive_cylinder_add(vertices=16, radius=0.8, depth=1, location=(0, 0, 3))
inner_cylinder = bpy.context.active_object
inner_cylinder.name = "Inner_Cylinder"

# 选择外层圆柱作为活动对象
bpy.context.view_layer.objects.active = outer_cylinder

# 添加布尔修改器
bpy.ops.object.modifier_add(type='BOOLEAN')
outer_cylinder.modifiers["Boolean"].operation = 'DIFFERENCE'
outer_cylinder.modifiers["Boolean"].object = inner_cylinder

# 应用修改器
bpy.ops.object.modifier_apply(modifier="Boolean")

# 删除内层圆柱,因为已经应用了布尔运算
bpy.data.objects.remove(inner_cylinder)

# 确保最后回到物体模式
bpy.ops.object.mode_set(mode='OBJECT')

if name == ‘main’:
app.run(host=‘0.0.0.0’, port=5013)

This is a forum for Blender development. For questions about using Blender, feature requests and bugs, please use the links above. Forum rules.