通过Python API选择一个面并在Blender中挤出一个多维数据集

时间:2016-06-14 09:59:30

标签: python blender

我正在开发一个项目,我需要能够通过python API挤出多维数据集的面。

我设法通过API挤出飞机:

import bpy

bpy.data.objects['Cube'].select = True # Select the default Blender Cube
bpy.ops.object.delete() # Delete the selected objects (default blender Cube) 

#Define vertices and faces
verts = [(0,0,0),(0,5,0),(5,5,0),(5,0,0)]
faces = [(0,1,2,3)]

# Define mesh and object variables
mymesh = bpy.data.meshes.new("Plane")
myobject = bpy.data.objects.new("Plane", mymesh)  

#Set scene of object
bpy.context.scene.objects.link(myobject)

#Create mesh
mymesh.from_pydata(verts,[],faces)
mymesh.update(calc_edges=True)

bpy.context.scene.objects.active = bpy.context.scene.objects['Plane']

bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
bpy.data.objects['Plane'].select = True # Select the default Blender Cube
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.extrude_region_move(TRANSFORM_OT_translate={"value":(0, 0, 2)})

我以类似的方式构建了我的Cube但我的问题是我无法解决如何通过Python API选择要挤出的面

请查找我的多维数据集代码http://pastebin.com/PQtMcRAh

赞赏所有帮助:)

1 个答案:

答案 0 :(得分:2)

我不太确定你需要什么,但是如果你需要这个:

然后这是您需要的代码:

import bpy
import bmesh

bpy.data.objects['Cube'].select = True # Select the default Blender Cube
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.delete() # Delete the selected objects (default blender Cube)

#Define vertices, faces, edges
verts = [(0,0,0),(0,5,0),(5,5,0),(5,0,0),(0,0,5),(0,5,5),(5,5,5),(5,0,5)]
faces = [(0,1,2,3), (4,5,6,7), (0,4,5,1), (1,5,6,2), (2,6,7,3), (3,7,4,0)]

#Define mesh and object
mesh = bpy.data.meshes.new("Cube")
object = bpy.data.objects.new("Cube", mesh)

#Set location and scene of object
object.location = bpy.context.scene.cursor_location
bpy.context.scene.objects.link(object)

#Create mesh
mesh.from_pydata(verts,[],faces)
mesh.update(calc_edges=True)

bpy.data.objects['Cube'].select = True
bpy.context.scene.objects.active = bpy.context.scene.objects['Cube'] # Select the default Blender Cube

#Enter edit mode to extrude
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.normals_make_consistent(inside=False)

bm = bmesh.from_edit_mesh(mesh)
for face in bm.faces:
    face.select = False
bm.faces[1].select = True

# Show the updates in the viewport
bmesh.update_edit_mesh(mesh, True)

bpy.ops.mesh.extrude_faces_move(MESH_OT_extrude_faces_indiv={"mirror":False}, TRANSFORM_OT_shrink_fatten={"value":-5, "use_even_offset":True, "mirror":False, "proportional":'DISABLED', "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "release_confirm":False})

它扩展了您的代码。解释:

编码后,它:

  1. 使用bmesh修改网格(bm = bmesh.from_edit_mesh(mesh)
  2. 取消选择所有面(for face in bm.faces: face.select = False
  3. 选择顶面(bm.faces[1].select = True
  4. 更新视口,以便您可以看到它(bmesh.update_edit_mesh(mesh, True)
  5. 将顶面挤出5个单位(bpy.ops.mesh.extrude_faces_move(MESH_OT_extrude_faces_indiv={"mirror":False}, TRANSFORM_OT_shrink_fatten={"value": -VALUE, "use_even_offset":True, "mirror":False, "proportional":'DISABLED', "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "release_confirm":False})
  6. 为了更改挤出的单位数,您可以修改VALUE变量。