this is my first time coding for Blender, I haven't gotten around to organizing the code yet. I'm self taught so my code's not the cleanest.
I'm attempting to create a 'dorito' deformer using this code, by selecting a vertex and running this script, it would create two empties that would be used for a warp modifier that would be added to the selected object. I am using bmesh to access the selected vertex's coordinates so I could add the empties to that location, storing it into the "vertex" variable.
I am storing the return value of the empties to an empty list, but I don't know how to use that to be able to add more empties to a second or third warp modifier making the script actually useful.
Any input is appreciated!
PS: I am scripting this for 2.92. Just in case there are relevant changes in the API.
import bpy
import bmesh
if bpy.ops.object.mode_set.poll():
bpy.ops.object.mode_set(mode='OBJECT')
object_data = bpy.context.object.data
meObject = bpy.context.active_object
bm = bmesh.new()
bm.from_mesh(object_data)
bpy.context.view_layer.objects.active = meObject
if bpy.ops.object.mode_set.poll():
bpy.ops.object.mode_set(mode='EDIT')
#gets vertex position to add empties
vertex = (0,0,0)
for v in bm.verts:
if v.select:
vertex = tuple(v.co)
#changes mode
if bpy.ops.object.mode_set.poll():
bpy.ops.object.mode_set(mode='OBJECT')
#empties list to store empties
empties = []
#adds first empty for warp deformer, and appends to list
empties.append(bpy.ops.object.empty_add(type='PLAIN_AXES', align='WORLD', location=(vertex), scale=(2, 2, 2)))
#names empty added
bpy.context.active_object.name = 'Parent'
#sets parent name
bpy.context.object.parent = meObject
#adds second empty for warp deformer
empties.append(bpy.ops.object.empty_add(type='PLAIN_AXES', align='WORLD', location=(vertex), scale=(1, 1, 1)))
#names empty added
bpy.context.active_object.name = 'Deformer'
#changes empty used
bpy.context.object.empty_display_type = 'CUBE'
bpy.context.object.empty_display_size = 0.75
bpy.context.object.parent = bpy.data.objects["Parent"]
bpy.context.view_layer.objects.active = meObject
#adds warp modifier
bpy.ops.object.modifier_add(type='WARP')
bpy.context.object.modifiers["Warp"].object_from = bpy.data.objects["Parent"]
bpy.context.object.modifiers["Warp"].object_to = bpy.data.objects["Deformer"]
if bpy.ops.object.mode_set.poll():
bpy.ops.object.mode_set(mode='OBJECT')