4

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 :)

otluk
  • 193
  • 1
  • 1
  • 10

1 Answers1

11

EDIT: Updated for 2.8x, for prior see revision

import bpy
import bmesh

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.

bpy.context.collection.objects.link(basic_sphere)

Select the newly created object

bpy.context.view_layer.objects.active = basic_sphere basic_sphere.select_set(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, radius=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. radius is the sphere radius 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.

J. Bakker
  • 3,051
  • 12
  • 30
Dimali
  • 1,675
  • 1
  • 8
  • 15