1
$\begingroup$

I'm trying to create a uv sphere and uv unwrap it, both in python. That is, I can create the sphere, and I can uv unwrap objects, but I cannot unwrap the sphere. I can unwrap a manually created sphere, though.

The following script creates one sphere, and unwraps all objects in the scene. Create your own sphere manually, run the script, and you'll end up with two spheres, one of them uv unwrapped correctly, while the other is not. What goes wrong here?

import bpy
import bmesh

from math import radians

context = bpy.context
scene = context.scene
vl = context.view_layer

# Create a sphere

# 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, diameter=1)
bm.to_mesh(mesh)
bm.free()


# Unwrap all objects 

for obj in scene.objects:
    if (obj.type == 'MESH'):

        vl.objects.active = obj
        obj.select_set(True)
        print(obj.name)
        lm =  obj.data.uv_layers.get("LightMap")
        if not lm:
            lm = obj.data.uv_layers.new(name="LightMap")
        lm.active = True
        bpy.ops.object.editmode_toggle()
        bpy.ops.uv.smart_project()
        bpy.ops.object.editmode_toggle()
        obj.select_set(False)

Any help is appreciated!

$\endgroup$
0
$\begingroup$

Select the Faces.

The Smart UV unwrap (or most UV ops) rely on faces being selected. For objects added via an add mesh primitive operator all verts, and hence faces are selected by default. For meshes created by bmesh the opposite is the case.

Simply select all faces when you create the bmesh and the UV unwrap will work as expected.

bmesh.ops.create_uvsphere(bm, u_segments=32, v_segments=16, diameter=1)
for f in bm.faces:
    f.select_set(True)

Related:

Smart unwrap using script (Looks somewhat familiar)

How to UV Unwrap and Scale UVs with Python (while in object mode)?

This way can lose the edit mode toggle and use no operators.

import bpy
from bl_operators.uvcalc_smart_project import main as smart_proj


context = bpy.context
scene = context.scene

meshobs = [o for o in scene.objects if o.type == 'MESH']
for o in meshobs:
    o.select_set(False) 
for o in meshobs:
    context.view_layer.objects.active = o
    o.select_set(True)
    smart_proj(context, 0, 66, 0, True, True)
    o.select_set(False)

I am trying to create a Script that does a Smart UV unwrap of seperate connected parts of a mesh

| improve this answer | |
$\endgroup$
  • $\begingroup$ Problem solved. However, I would appreciate if the programmatic way of adding a sphere matches the manual way. $\endgroup$ – eezacque Apr 4 at 13:24

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.