fixed the create_object nonetype error

This commit is contained in:
ahujasid 2025-03-16 21:42:14 +05:30
parent 5e0f87cbc1
commit 9c299304ce

120
addon.py
View File

@ -266,63 +266,81 @@ class BlenderMCPServer:
align="WORLD", major_segments=48, minor_segments=12, mode="MAJOR_MINOR", align="WORLD", major_segments=48, minor_segments=12, mode="MAJOR_MINOR",
major_radius=1.0, minor_radius=0.25, abso_major_rad=1.25, abso_minor_rad=0.75, generate_uvs=True): major_radius=1.0, minor_radius=0.25, abso_major_rad=1.25, abso_minor_rad=0.75, generate_uvs=True):
"""Create a new object in the scene""" """Create a new object in the scene"""
# Deselect all objects try:
bpy.ops.object.select_all(action='DESELECT') # Deselect all objects first
bpy.ops.object.select_all(action='DESELECT')
if type == "CUBE": # Create the object based on type
bpy.ops.mesh.primitive_cube_add(location=location, rotation=rotation, scale=scale) if type == "CUBE":
elif type == "SPHERE": bpy.ops.mesh.primitive_cube_add(location=location, rotation=rotation, scale=scale)
bpy.ops.mesh.primitive_uv_sphere_add(location=location, rotation=rotation, scale=scale) elif type == "SPHERE":
elif type == "CYLINDER": bpy.ops.mesh.primitive_uv_sphere_add(location=location, rotation=rotation, scale=scale)
bpy.ops.mesh.primitive_cylinder_add(location=location, rotation=rotation, scale=scale) elif type == "CYLINDER":
elif type == "PLANE": bpy.ops.mesh.primitive_cylinder_add(location=location, rotation=rotation, scale=scale)
bpy.ops.mesh.primitive_plane_add(location=location, rotation=rotation, scale=scale) elif type == "PLANE":
elif type == "CONE": bpy.ops.mesh.primitive_plane_add(location=location, rotation=rotation, scale=scale)
bpy.ops.mesh.primitive_cone_add(location=location, rotation=rotation, scale=scale) elif type == "CONE":
elif type == "TORUS": bpy.ops.mesh.primitive_cone_add(location=location, rotation=rotation, scale=scale)
bpy.ops.mesh.primitive_torus_add( elif type == "TORUS":
align=align, bpy.ops.mesh.primitive_torus_add(
location=location, align=align,
rotation=rotation, location=location,
major_segments=major_segments, rotation=rotation,
minor_segments=minor_segments, major_segments=major_segments,
mode=mode, minor_segments=minor_segments,
major_radius=major_radius, mode=mode,
minor_radius=minor_radius, major_radius=major_radius,
abso_major_rad=abso_major_rad, minor_radius=minor_radius,
abso_minor_rad=abso_minor_rad, abso_major_rad=abso_major_rad,
generate_uvs=generate_uvs abso_minor_rad=abso_minor_rad,
) generate_uvs=generate_uvs
elif type == "EMPTY": )
bpy.ops.object.empty_add(location=location, rotation=rotation, scale=scale) elif type == "EMPTY":
elif type == "CAMERA": bpy.ops.object.empty_add(location=location, rotation=rotation, scale=scale)
bpy.ops.object.camera_add(location=location, rotation=rotation) elif type == "CAMERA":
elif type == "LIGHT": bpy.ops.object.camera_add(location=location, rotation=rotation)
bpy.ops.object.light_add(type='POINT', location=location, rotation=rotation, scale=scale) elif type == "LIGHT":
else: bpy.ops.object.light_add(type='POINT', location=location, rotation=rotation, scale=scale)
raise ValueError(f"Unsupported object type: {type}") else:
raise ValueError(f"Unsupported object type: {type}")
# Get the created object # Force update the view layer
bpy.context.view_layer.update() bpy.context.view_layer.update()
obj = bpy.context.view_layer.objects.active
# Rename the object if a name is provided # Get the active object (which should be our newly created object)
if name: obj = bpy.context.view_layer.objects.active
obj.name = name
result = { # If we don't have an active object, something went wrong
"name": obj.name, if obj is None:
"type": obj.type, raise RuntimeError("Failed to create object - no active object")
"location": [obj.location.x, obj.location.y, obj.location.z],
"rotation": [obj.rotation_euler.x, obj.rotation_euler.y, obj.rotation_euler.z],
"scale": [obj.scale.x, obj.scale.y, obj.scale.z],
}
if obj.type == "MESH": # Make sure it's selected
bounding_box = self._get_aabb(obj) obj.select_set(True)
result["world_bounding_box"] = bounding_box
return result # Rename if name is provided
if name:
obj.name = name
if obj.data:
obj.data.name = name
# Return the object info
result = {
"name": obj.name,
"type": obj.type,
"location": [obj.location.x, obj.location.y, obj.location.z],
"rotation": [obj.rotation_euler.x, obj.rotation_euler.y, obj.rotation_euler.z],
"scale": [obj.scale.x, obj.scale.y, obj.scale.z],
}
if obj.type == "MESH":
bounding_box = self._get_aabb(obj)
result["world_bounding_box"] = bounding_box
return result
except Exception as e:
print(f"Error in create_object: {str(e)}")
traceback.print_exc()
return {"error": str(e)}
def modify_object(self, name, location=None, rotation=None, scale=None, visible=None): def modify_object(self, name, location=None, rotation=None, scale=None, visible=None):
"""Modify an existing object in the scene""" """Modify an existing object in the scene"""