3
$\begingroup$

I want to make a capsule by changing the distance between the two halves of the sphere to a certain depth

enter image description here

enter image description here

import bpy
import bmesh

bm = bmesh.new()
bmesh.ops.create_uvsphere(bm, u_segments=8, v_segments=9, radius=2)

name = 'Capsule'
mesh = bpy.data.meshes.new(name)
bm.to_mesh(mesh)
mesh.update()
bm.free()

object = bpy.data.objects.new(name, mesh)
bpy.context.scene.collection.objects.link(object)
$\endgroup$

1 Answer 1

3
$\begingroup$

create_uvsphere now takes a diameter argument rather than radius despite what the manual claims. EDIT: From the comments, what you want to do is move the top half of the sphere up an increment and the bottom half down the same increment. Here's code to do that:

import bmesh

bm = bmesh.new()
bmesh.ops.create_uvsphere(bm, u_segments=8, v_segments=9, diameter=2)
delta_Z = WHATEVER_YOU_WANT
bm.verts.ensure_lookup_table()
for vert in bm.verts:
    if vert.co[2] < 0:
        vert.co[2] -= delta_Z
    elif vert.co[2] > 0:
        vert.co[2] += delta_Z

name = 'Capsule'
mesh = bpy.data.meshes.new(name)
bm.to_mesh(mesh)
mesh.update()
bm.free()

object = bpy.data.objects.new(name, mesh)
bpy.context.scene.collection.objects.link(object)

where scale_Z is how much you want to scale the result by.

$\endgroup$
2
  • $\begingroup$ I don't want to stretch the sphere, I want distance between the two halves of the sphere as shown in the image above $\endgroup$
    – Karan
    Commented Mar 28, 2022 at 6:54
  • $\begingroup$ @Karan I changed the code to what you want. $\endgroup$ Commented Mar 28, 2022 at 17:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .