fixed the create_object nonetype error

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

132
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":
bpy.ops.mesh.primitive_cube_add(location=location, rotation=rotation, scale=scale) # Create the object based on type
elif type == "SPHERE": if type == "CUBE":
bpy.ops.mesh.primitive_uv_sphere_add(location=location, rotation=rotation, scale=scale) bpy.ops.mesh.primitive_cube_add(location=location, rotation=rotation, scale=scale)
elif type == "CYLINDER": elif type == "SPHERE":
bpy.ops.mesh.primitive_cylinder_add(location=location, rotation=rotation, scale=scale) bpy.ops.mesh.primitive_uv_sphere_add(location=location, rotation=rotation, scale=scale)
elif type == "PLANE": elif type == "CYLINDER":
bpy.ops.mesh.primitive_plane_add(location=location, rotation=rotation, scale=scale) bpy.ops.mesh.primitive_cylinder_add(location=location, rotation=rotation, scale=scale)
elif type == "CONE": elif type == "PLANE":
bpy.ops.mesh.primitive_cone_add(location=location, rotation=rotation, scale=scale) bpy.ops.mesh.primitive_plane_add(location=location, rotation=rotation, scale=scale)
elif type == "TORUS": elif type == "CONE":
bpy.ops.mesh.primitive_torus_add( bpy.ops.mesh.primitive_cone_add(location=location, rotation=rotation, scale=scale)
align=align, elif type == "TORUS":
location=location, bpy.ops.mesh.primitive_torus_add(
rotation=rotation, align=align,
major_segments=major_segments, location=location,
minor_segments=minor_segments, rotation=rotation,
mode=mode, major_segments=major_segments,
major_radius=major_radius, minor_segments=minor_segments,
minor_radius=minor_radius, mode=mode,
abso_major_rad=abso_major_rad, major_radius=major_radius,
abso_minor_rad=abso_minor_rad, minor_radius=minor_radius,
generate_uvs=generate_uvs abso_major_rad=abso_major_rad,
) abso_minor_rad=abso_minor_rad,
elif type == "EMPTY": generate_uvs=generate_uvs
bpy.ops.object.empty_add(location=location, rotation=rotation, scale=scale) )
elif type == "CAMERA": elif type == "EMPTY":
bpy.ops.object.camera_add(location=location, rotation=rotation) bpy.ops.object.empty_add(location=location, rotation=rotation, scale=scale)
elif type == "LIGHT": elif type == "CAMERA":
bpy.ops.object.light_add(type='POINT', location=location, rotation=rotation, scale=scale) bpy.ops.object.camera_add(location=location, rotation=rotation)
else: elif type == "LIGHT":
raise ValueError(f"Unsupported object type: {type}") bpy.ops.object.light_add(type='POINT', location=location, rotation=rotation, scale=scale)
else:
# Get the created object raise ValueError(f"Unsupported object type: {type}")
bpy.context.view_layer.update()
obj = bpy.context.view_layer.objects.active # Force update the view layer
bpy.context.view_layer.update()
# Rename the object if a name is provided
if name: # Get the active object (which should be our newly created object)
obj.name = name obj = bpy.context.view_layer.objects.active
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], # Make sure it's selected
"scale": [obj.scale.x, obj.scale.y, obj.scale.z], obj.select_set(True)
}
# Rename if name is provided
if obj.type == "MESH": if name:
bounding_box = self._get_aabb(obj) obj.name = name
result["world_bounding_box"] = bounding_box if obj.data:
obj.data.name = name
return result
# 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"""