fixed the create_object nonetype error

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

View File

@ -266,9 +266,11 @@ class BlenderMCPServer:
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):
"""Create a new object in the scene"""
# Deselect all objects
try:
# Deselect all objects first
bpy.ops.object.select_all(action='DESELECT')
# Create the object based on type
if type == "CUBE":
bpy.ops.mesh.primitive_cube_add(location=location, rotation=rotation, scale=scale)
elif type == "SPHERE":
@ -302,14 +304,26 @@ class BlenderMCPServer:
else:
raise ValueError(f"Unsupported object type: {type}")
# Get the created object
# Force update the view layer
bpy.context.view_layer.update()
# Get the active object (which should be our newly created object)
obj = bpy.context.view_layer.objects.active
# Rename the object if a name is provided
# If we don't have an active object, something went wrong
if obj is None:
raise RuntimeError("Failed to create object - no active object")
# Make sure it's selected
obj.select_set(True)
# 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,
@ -323,6 +337,10 @@ class BlenderMCPServer:
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):
"""Modify an existing object in the scene"""