1
$\begingroup$

Referencing to this code: Create a Cube in Blender from Python

import bpy
import bmesh

bpyscene = bpy.context.scene

# Create an empty mesh and the object.
mesh = bpy.data.meshes.new('Basic_Sphere')
basic_sphere = bpy.data.objects.new("Basic_Sphere", mesh)

# Add the object into the scene.
bpyscene.objects.link(basic_sphere)
bpyscene.objects.active = basic_sphere
basic_sphere.select = True

# Construct the bmesh cube and assign it to the blender mesh.
bm = bmesh.new()
bmesh.ops.create_uvsphere(bm) # How to set the arguments right?
bm.to_mesh(mesh)
bm.free()

Can someone explain how to set the arguments write for this function?

bmesh.ops.create_uvsphere(bm, u_segments, v_segments, diameter, matrix, calc_uvs)

I am new to bpy, bmesh and I am looking for some more explaination on how to set these arguments right. My goal is to generate a UV Sphere with Subdivision Surface modiefier and smooth ;). Someone can give me some further explainations on how to create a UV Sphere with the script? Thanks :)

$\endgroup$
3
$\begingroup$
import bpy
import bmesh

bpyscene = bpy.context.scene

# Create an empty mesh and the object.
mesh = bpy.data.meshes.new('Basic_Sphere')
basic_sphere = bpy.data.objects.new("Basic_Sphere", mesh)

# Add the object into the scene.
bpyscene.objects.link(basic_sphere)
bpyscene.objects.active = basic_sphere
basic_sphere.select = True

# Construct the bmesh sphere and assign it to the blender mesh.
bm = bmesh.new()
bmesh.ops.create_uvsphere(bm, u_segments=32, v_segments=16, diameter=1)
bm.to_mesh(mesh)
bm.free()

bpy.ops.object.modifier_add(type='SUBSURF')
bpy.ops.object.shade_smooth()

In this case you need keyword arguments for all but the first, meaning you need to specify the argument name like u_segments=. The first argument is a positional argument, you only need to give the value bm. How do I know that? Because the Blender API Documentation is not very precise here, Blender told me in the Error message "bmesh operators expect a single BMesh positional argument, all other args must be keyword".

u_segments and v_segments control the initial subdivision of the mesh. diameter is the sphere size in local coordinate system (object transforation comes on top). matrix can be used if you want to shear, scale, rotate or move the sphere in one step. calc_uvs is about UV maps, but don't ask me what exactly.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.