0
$\begingroup$

I want to write a script that creates a 3D Mesh based on an Image (as plane). My workflow now is:

  • import image as plane
  • subdivide plane
  • bake image as vertex color
  • delete vertices based on their color
  • Decimate Modifier
  • Solidify Modifier
  • Subdiv Modifier

This is the result I get:

enter image description here

All of this must be achieved with the script so tracing the shape by hand is not possible. Do you know any other approaches to this, that might be useful?

$\endgroup$
8
  • $\begingroup$ I don't know of any specific add-on that has exactly that workflow; but with one exception it would be pretty easy to write a script to do what you want. The exception is "delete vertices based on their color". That would depend on how you wanted to go about selecting the colors to delete and could vary from something simple like how close to black the pixel is to something complex enough to require an AI-based tool. How do you decide the color now? $\endgroup$ Mar 14, 2022 at 15:16
  • $\begingroup$ I'll write the script, it's just about the workflow. Selecting the color should not be an issue since I can create black and white masks, which makes it very simple. $\endgroup$
    – Timmethy
    Mar 14, 2022 at 15:18
  • 1
    $\begingroup$ You can look at addons like easy bake to see how the baking is done. Once baked, I would use bmesh for the mesh manipulations. Adding the modifiers is pretty easy. Good luck $\endgroup$ Mar 14, 2022 at 15:24
  • 2
    $\begingroup$ There's an add-on that does this already: github.com/Banbury/io_image_as_mesh you might find some helpful information in their process $\endgroup$ Mar 14, 2022 at 15:43
  • $\begingroup$ @MartyFouts Can you maybe help me with bmesh? How can I delete vertices that don't belong to a surface? Right now I am doing the following: - select mode to face select - select all faces - select mode to vertex - invert selection - delete However this is extremly unstable and I guess thats the reason why bmesh should be used but I can't figure out how to achieve this. $\endgroup$
    – Timmethy
    Mar 14, 2022 at 19:07

1 Answer 1

0
$\begingroup$

I was following my initial workflow and with your help my script is now working. This is the result: enter image description here

Here is also the script in case it is helpful for someone:


import bpy
import bmesh

ob = bpy.context.object
me = ob.data
loops = len(ob.data.loops)
verts = len(ob.data.vertices)
visit = verts * [False]
sel_v = []

#SET THRESHOLD VALUES HERE

r = 0.2
g = 1
b = 1

#CREATE VERTEX COLOR LAYER

bpy.ops.mesh.vertex_color_add()    

#BAKE TEXTURE TO VERTEX COLOR

bpy.context.scene.render.engine = 'CYCLES'
bpy.context.scene.render.bake.target = 'VERTEX_COLORS'
bpy.ops.object.bake(type = 'DIFFUSE') 

#SELECT VERTICES BASED ON VERTEX COLOR

for l in range(loops):
    v = ob.data.loops[l].vertex_index
    c = ob.data.vertex_colors[0].data[l].color
    if not visit[v]:
        if c[0] < r and c[1] < g and c[2] < b:
            sel_v.append(v)
        visit[v] = True
        
#REMOVE VERTICES WITH BMESH

verts = []
bpy.ops.object.mode_set(mode='EDIT')
bm = bmesh.from_edit_mesh(me)
bm.faces.ensure_lookup_table()
bm.verts.ensure_lookup_table()

for i in sel_v:
    verts.append(bm.verts[i])
    
for i in verts:
    bm.verts.remove(i)


for vert in bm.verts:
    vert.select = False

#DELETE VERTICES THAT ARE NOT PART OF ANY FACE

for face in bm.faces:
    face.select_set(True)

for vert in bm.verts:
    vert.select_set(not vert.select) 
    
for vert in bm.verts:
    if vert.select:
        bm.verts.remove(vert)

bmesh.update_edit_mesh(me)

bpy.ops.object.mode_set(mode='OBJECT')

#CREATE MODIFIERS

size = max(ob.dimensions)

dec = ob.modifiers.new(name = "dec", type = 'DECIMATE')
dec.decimate_type = 'DISSOLVE'

sol = ob.modifiers.new(name = "sol", type = 'SOLIDIFY')
sol.thickness = size/3
sol.offset = 0

sub = ob.modifiers.new(name = "sub", type = 'SUBSURF')
sub.levels = 2

$\endgroup$

You must log in to answer this question.

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