Fix the create_object in server and addon, as torus receive different parameters
This commit is contained in:
parent
0ec76f0c31
commit
5745c8bff9
36
addon.py
36
addon.py
@ -231,12 +231,13 @@ class BlenderMCPServer:
|
|||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
return {"error": str(e)}
|
return {"error": str(e)}
|
||||||
|
|
||||||
def create_object(self, type="CUBE", name=None, location=(0, 0, 0), rotation=(0, 0, 0), scale=(1, 1, 1)):
|
def create_object(self, type="CUBE", name=None, location=(0, 0, 0), rotation=(0, 0, 0), scale=(1, 1, 1),
|
||||||
|
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"""
|
"""Create a new object in the scene"""
|
||||||
# Deselect all objects
|
# Deselect all objects
|
||||||
bpy.ops.object.select_all(action='DESELECT')
|
bpy.ops.object.select_all(action='DESELECT')
|
||||||
|
|
||||||
# Create the object based on type
|
|
||||||
if type == "CUBE":
|
if type == "CUBE":
|
||||||
bpy.ops.mesh.primitive_cube_add(location=location, rotation=rotation, scale=scale)
|
bpy.ops.mesh.primitive_cube_add(location=location, rotation=rotation, scale=scale)
|
||||||
elif type == "SPHERE":
|
elif type == "SPHERE":
|
||||||
@ -248,7 +249,19 @@ class BlenderMCPServer:
|
|||||||
elif type == "CONE":
|
elif type == "CONE":
|
||||||
bpy.ops.mesh.primitive_cone_add(location=location, rotation=rotation, scale=scale)
|
bpy.ops.mesh.primitive_cone_add(location=location, rotation=rotation, scale=scale)
|
||||||
elif type == "TORUS":
|
elif type == "TORUS":
|
||||||
bpy.ops.mesh.primitive_torus_add(location=location, rotation=rotation, scale=scale)
|
bpy.ops.mesh.primitive_torus_add(
|
||||||
|
align=align,
|
||||||
|
location=location,
|
||||||
|
rotation=rotation,
|
||||||
|
major_segments=major_segments,
|
||||||
|
minor_segments=minor_segments,
|
||||||
|
mode=mode,
|
||||||
|
major_radius=major_radius,
|
||||||
|
minor_radius=minor_radius,
|
||||||
|
abso_major_rad=abso_major_rad,
|
||||||
|
abso_minor_rad=abso_minor_rad,
|
||||||
|
generate_uvs=generate_uvs
|
||||||
|
)
|
||||||
elif type == "EMPTY":
|
elif type == "EMPTY":
|
||||||
bpy.ops.object.empty_add(location=location, rotation=rotation, scale=scale)
|
bpy.ops.object.empty_add(location=location, rotation=rotation, scale=scale)
|
||||||
elif type == "CAMERA":
|
elif type == "CAMERA":
|
||||||
@ -261,18 +274,13 @@ class BlenderMCPServer:
|
|||||||
# Get the created object
|
# Get the created object
|
||||||
obj = bpy.context.active_object
|
obj = bpy.context.active_object
|
||||||
|
|
||||||
# Rename if name is provided
|
# Rename the object if a name is provided
|
||||||
if name:
|
if name:
|
||||||
obj.name = name
|
obj.name = name
|
||||||
|
|
||||||
return {
|
return {"name": obj.name}
|
||||||
"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],
|
|
||||||
}
|
|
||||||
|
|
||||||
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"""
|
||||||
# Find the object by name
|
# Find the object by name
|
||||||
@ -1256,4 +1264,4 @@ def unregister():
|
|||||||
print("BlenderMCP addon unregistered")
|
print("BlenderMCP addon unregistered")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
register()
|
register()
|
||||||
|
|||||||
@ -271,7 +271,17 @@ def create_object(
|
|||||||
name: str = None,
|
name: str = None,
|
||||||
location: List[float] = None,
|
location: List[float] = None,
|
||||||
rotation: List[float] = None,
|
rotation: List[float] = None,
|
||||||
scale: List[float] = None
|
scale: List[float] = None,
|
||||||
|
# Torus-specific parameters
|
||||||
|
align: str = "WORLD",
|
||||||
|
major_segments: int = 48,
|
||||||
|
minor_segments: int = 12,
|
||||||
|
mode: str = "MAJOR_MINOR",
|
||||||
|
major_radius: float = 1.0,
|
||||||
|
minor_radius: float = 0.25,
|
||||||
|
abso_major_rad: float = 1.25,
|
||||||
|
abso_minor_rad: float = 0.75,
|
||||||
|
generate_uvs: bool = True
|
||||||
) -> str:
|
) -> str:
|
||||||
"""
|
"""
|
||||||
Create a new object in the Blender scene.
|
Create a new object in the Blender scene.
|
||||||
@ -281,7 +291,21 @@ def create_object(
|
|||||||
- name: Optional name for the object
|
- name: Optional name for the object
|
||||||
- location: Optional [x, y, z] location coordinates
|
- location: Optional [x, y, z] location coordinates
|
||||||
- rotation: Optional [x, y, z] rotation in radians
|
- rotation: Optional [x, y, z] rotation in radians
|
||||||
- scale: Optional [x, y, z] scale factors
|
- scale: Optional [x, y, z] scale factors (not used for TORUS)
|
||||||
|
|
||||||
|
Torus-specific parameters (only used when type == "TORUS"):
|
||||||
|
- align: How to align the torus ('WORLD', 'VIEW', or 'CURSOR')
|
||||||
|
- major_segments: Number of segments for the main ring
|
||||||
|
- minor_segments: Number of segments for the cross-section
|
||||||
|
- mode: Dimension mode ('MAJOR_MINOR' or 'EXT_INT')
|
||||||
|
- major_radius: Radius from the origin to the center of the cross sections
|
||||||
|
- minor_radius: Radius of the torus' cross section
|
||||||
|
- abso_major_rad: Total exterior radius of the torus
|
||||||
|
- abso_minor_rad: Total interior radius of the torus
|
||||||
|
- generate_uvs: Whether to generate a default UV map
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
A message indicating the created object name.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
# Get the global connection
|
# Get the global connection
|
||||||
@ -296,18 +320,36 @@ def create_object(
|
|||||||
"type": type,
|
"type": type,
|
||||||
"location": loc,
|
"location": loc,
|
||||||
"rotation": rot,
|
"rotation": rot,
|
||||||
"scale": sc
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if name:
|
if name:
|
||||||
params["name"] = name
|
params["name"] = name
|
||||||
|
|
||||||
result = blender.send_command("create_object", params)
|
if type == "TORUS":
|
||||||
return f"Created {type} object: {result['name']}"
|
# For torus, the scale is not used.
|
||||||
|
params.update({
|
||||||
|
"align": align,
|
||||||
|
"major_segments": major_segments,
|
||||||
|
"minor_segments": minor_segments,
|
||||||
|
"mode": mode,
|
||||||
|
"major_radius": major_radius,
|
||||||
|
"minor_radius": minor_radius,
|
||||||
|
"abso_major_rad": abso_major_rad,
|
||||||
|
"abso_minor_rad": abso_minor_rad,
|
||||||
|
"generate_uvs": generate_uvs
|
||||||
|
})
|
||||||
|
result = blender.send_command("create_object", params)
|
||||||
|
return f"Created {type} object: {result['name']}"
|
||||||
|
else:
|
||||||
|
# For non-torus objects, include scale
|
||||||
|
params["scale"] = sc
|
||||||
|
result = blender.send_command("create_object", params)
|
||||||
|
return f"Created {type} object: {result['name']}"
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error creating object: {str(e)}")
|
logger.error(f"Error creating object: {str(e)}")
|
||||||
return f"Error creating object: {str(e)}"
|
return f"Error creating object: {str(e)}"
|
||||||
|
|
||||||
|
|
||||||
@mcp.tool()
|
@mcp.tool()
|
||||||
def modify_object(
|
def modify_object(
|
||||||
ctx: Context,
|
ctx: Context,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user