Flatten Mesh to UV


(actimelvanille) #1

Hey guys,

does anyone have a script that does flatten the mesh to the existing uv map as seen here

Cheers Christian


(liero) #2

if you manually split edges to match islands you can then try this snippet -not too smart-
can also work on a duplicated mesh and then merge as shape keys


#### simple uv > co
import bpy
me = bpy.context.object.data
uv_layer = me.uv_layers.active.data

for poly in me.polygons:
    for loop_index in poly.loop_indices:
        i = me.loops[loop_index].vertex_index
        co = uv_layer[loop_index].uv
        me.vertices[i].co[0] = co[0] - 0.5
        me.vertices[i].co[1] = co[1] - 0.5
        me.vertices[i].co[2] = 0


(actimelvanille) #3

wow, it really works :slight_smile:
such a small script and it does exactly what i need ^^
many thanks liero!!


(Spirit412) #4

Please write how to use it. It is very necessary.


(liero) #5

you need to unwrap your mesh but also manually split edges (V key) to match uv islands
then duplicate your object -because it will be overwritten- and in object mode run the snippet from a text editor
a quick example: https://db.tt/kWAzckzW
a proper script would need to be smarter, would take care of splitting edges, scaling result and adding it as a shape key


(Spirit412) #6

thanks a lot


(Jinxatron) #7

Well i was looking for this! thanks!

saw a tutorial today using flatten to UV, allowing one to add details and then restoring it using blend shapes in 3ds max.

I tinkered a bit with the script, allowing it to copy the mesh, adding a base shape key, flattening it and adding another key to blend with.

also commented and added some debug code to make it a bit more readable in the console.

#### simple uv > co
import bpy

print("######### Script Starting #########")

bpy.ops.object.duplicate(linked=False, mode='TRANSLATION')
print("Duplicated the object")

me = bpy.context.object.data
uv_layer = me.uv_layers.active.data

bpy.ops.object.shape_key_add(from_mix=True)
print("Added Base shapekey")

for poly in me.polygons:
    for loop_index in poly.loop_indices:
        i = me.loops[loop_index].vertex_index
        co = uv_layer[loop_index].uv
        me.vertices[i].co[0] = co[0] * 2    ## To resize result of UV mesh,
        me.vertices[i].co[1] = co[1] * 2    ## change the multiplied ammount
        me.vertices[i].co[2] = 0
print("Flattened Based on UV")

bpy.ops.object.shape_key_add(from_mix=False)
print("Added Morphed shapekey")

print("######### Script Complete #########")

(ArnoldStarkey) #8

Thanks, Sharing for this link.


(tulson) #9

thanksalot mr. Jinxatron
i was searching for something like this for a long time.


(Jinxatron) #10

No problem :slight_smile:


(actimelvanille) #11

damn, i can’t recall how i attached my pattern to the flattened uv’s (so that it follows once i transform the uv’s back).
i tried it with the new surface deform modifier now but i always get a “concave polygons” error message.
does anyone know whats causing this problem?

Attachments

meeeeh.blend (548 KB)