python - 通过 Python API 在 Blender 中选择一个面并挤出一个立方体

标签 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)})

我以类似的方式构建了我的立方体,但我的问题是我不知道如何通过 Python API 选择要拉伸(stretch)的面

请找到我的魔方代码 http://pastebin.com/PQtMcRAh

感谢所有帮助:)

最佳答案

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

那么这是你需要的代码:

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. 更新视口(viewport)以便您可以看到它 ( bmesh.update_edit_mesh(mesh, True) )
  5. 将顶面拉伸(stretch) 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}) )

为了改变挤出的单位数,你可以修改VALUE多变的。

关于python - 通过 Python API 在 Blender 中选择一个面并挤出一个立方体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37808840/

相关文章:

python - 将视频流从 NodeJS 实时发送到 python

python - 存储图像的标题文本 Selenium Webdriver Python

import - Blender fbx 从 ascii 格式导入

three.js - 皮肤指数和皮肤权重是什么意思?

android - 纹理/顶点的不同索引数组

c# - 是否有与 python 的 os.path.normpath 等效的 C#?

python - PhantomJS 网络驱动程序错误代码 -6

unity-game-engine - 在 2D 游戏中使用 3D 模型的可行性/效率

python - Apache 进程空闲并占用内存

python - 如何通过bpy.types获取具体的修饰符属性信息?