The Bmesh Module

Posted jeremy-zhuhn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了The Bmesh Module相关的知识,希望对你有一定的参考价值。

  1. Basic Transformations in Edit Mode
import bpy
import bmesh

# Must start in object mode
# Script will fail if scene is empty
bpy.ops.object.mode_set(mode=‘OBJECT‘)
bpy.ops.object.select_all(action=‘SELECT‘)
bpy.ops.object.delete()

# Create a cube and enter Edit mode
bpy.ops.mesh.primitive_cube_add(size=0.5, location=(-3, 0, 0))
bpy.ops.object.mode_set(mode=‘EDIT‘)
bpy.ops.mesh.select_all(action="DESELECT")

# Set to "Face Mode" for easier visualization
bpy.ops.mesh.select_mode(type = "FACE")

bm = bmesh.from_edit_mesh(bpy.context.object.data)
bm.faces.ensure_lookup_table()
bm.faces[1].select = True
bpy.ops.transform.rotate(value = 0.3, orient_axis = ‘Y‘)

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

# Create a cube and pull an edge along the y-axis
bpy.ops.mesh.primitive_cube_add(size=0.5, location=(0, 0, 0))
bpy.ops.object.mode_set(mode=‘EDIT‘)
bpy.ops.mesh.select_all(action="DESELECT")

bm = bmesh.from_edit_mesh(bpy.context.object.data)
bm.edges.ensure_lookup_table()
bm.edges[4].select = True
bpy.ops.transform.translate(value = (0, 0.5, 0))

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

# Create a cube and pull a vertex 1 unit
# along the y and z axes
# Create a cube and pull an edge along the y-axis
bpy.ops.mesh.primitive_cube_add(size=0.5, location=(3, 0, 0))
bpy.ops.object.mode_set(mode=‘EDIT‘)
bpy.ops.mesh.select_all(action="DESELECT")

bm = bmesh.from_edit_mesh(bpy.context.object.data)
bm.verts.ensure_lookup_table()
bm.verts[3].select = True
bpy.ops.transform.translate(value = (0, 1, 0))

bpy.ops.object.mode_set(mode=‘OBJECT‘)
  1. Extrude, Subdivide, and Randomize Operators
import bpy
import bmesh

# Must start in object mode
# Script will fail if scene is empty
bpy.ops.object.mode_set(mode=‘OBJECT‘)
bpy.ops.object.select_all(action=‘SELECT‘)
bpy.ops.object.delete()

# Create a cube and extrude the top face away from it
bpy.ops.mesh.primitive_cube_add(size=0.5, location=(-3, 0, 0))
bpy.ops.object.mode_set(mode=‘EDIT‘)
bpy.ops.mesh.select_all(action="DESELECT")

# Set to "Face Mode" for visualization
bpy.ops.mesh.select_mode(type = "FACE")

bm = bmesh.from_edit_mesh(bpy.context.object.data)
bm.faces.ensure_lookup_table()
bm.faces[5].select = True
bpy.ops.mesh.extrude_region_move(TRANSFORM_OT_translate =
		{"value": (0.3, 0.3, 0.3),
		 "constraint_axis": (True, True, True)})

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

# Create a cube and subdivide the top face
bpy.ops.mesh.primitive_cube_add(size=0.5, location=(0, 0, 0))
bpy.ops.object.mode_set(mode=‘EDIT‘)
bpy.ops.mesh.select_all(action="DESELECT")

bm = bmesh.from_edit_mesh(bpy.context.object.data)
bm.faces.ensure_lookup_table()
bm.faces[5].select = True
bpy.ops.mesh.subdivide(number_cuts = 1)

bpy.ops.mesh.select_all(action="DESELECT")
bm.faces.ensure_lookup_table()
bm.faces[5].select = True
bm.faces[7].select = True
bpy.ops.transform.translate(value = (0, 0, 0.5))

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

# Create a cube and add a random offset to each vertex
bpy.ops.mesh.primitive_cube_add(size=0.5, location=(3, 0, 0))
bpy.ops.object.mode_set(mode=‘EDIT‘)
bpy.ops.mesh.select_all(action="SELECT")
bpy.ops.transform.vertex_random(offset = 0.5)

bpy.ops.object.mode_set(mode=‘OBJECT‘)
  1. Behavior of Global and Local Coordinates and Transform Apply
import sys
sys.path.append("/home/Documents/blender-scripts")
import ut
import importlib
importlib.reload(ut)

import bpy

# Will fail if scene is empty
bpy.ops.object.mode_set(mode=‘OBJECT‘)
bpy.ops.object.select_all(action=‘SELECT‘)
bpy.ops.object.delete()

bpy.ops.mesh.primitive_cube_add(size=0.5, location=(0, 0, 0))
bpy.context.object.name = ‘Cube-1‘

# Check global and local coordinates
print(‘
Before transform:‘)
print(‘Global:‘, ut.coords(‘Cube-1‘, ‘GLOBAL‘)[0:2])
print(‘Local:‘, ut.coords(‘Cube-1‘, ‘LOCAL‘)[0:2])

# Translate it along x = y = z
# See the cube move in the 3D viewport
bpy.ops.transform.translate(value = (3, 3, 3))

# Check global and local coordinates
print(‘
After transform:‘)
print(‘Global:‘, ut.coords(‘Cube-1‘, ‘GLOBAL‘)[0:2])
print(‘Local:‘, ut.coords(‘Cube-1‘, ‘LOCAL‘)[0:2])

# Apply transformation
# Nothing changes in 3D viewport
ut.sel.transform_apply()

# Check global and local coordinates
print(‘
After transform, applied:‘)
print(‘Global:‘, ut.coords(‘Cube-1‘, ‘GLOBAL‘)[0:2])
print(‘Local:‘, ut.coords(‘Cube-1‘, ‘LOCAL‘)[0:2])


############################ Output ###########################
#Before transform:
#Global: [(-0.25, -0.25, -0.25), (-0.25, -0.25, 0.25)]
#Local: [(-0.25, -0.25, -0.25), (-0.25, -0.25, 0.25)]
#
#After transform:
#Global: [(2.75, 2.75, 2.75), (2.75, 2.75, 3.25)]
#Local: [(-0.25, -0.25, -0.25), (-0.25, -0.25, 0.25)]
#
#After transform, applied:
#Global: [(2.75, 2.75, 2.75), (2.75, 2.75, 3.25)]
#Local: [(2.75, 2.75, 2.75), (2.75, 2.75, 3.25)]
###############################################################
  1. Selecting and Transforming Pieces of a Sphere
import sys
sys.path.append("/home/Documents/blender-scripts")
import ut
import importlib
importlib.reload(ut)

import bpy

# Will fail if scene is empty
bpy.ops.object.mode_set(mode=‘OBJECT‘)
bpy.ops.object.select_all(action=‘SELECT‘)
bpy.ops.object.delete()

bpy.ops.mesh.primitive_uv_sphere_add(radius=0.5, location=(0, 0, 0))
bpy.ops.transform.resize(value = (5, 5, 5))
bpy.ops.object.mode_set(mode=‘EDIT‘)
bpy.ops.mesh.select_all(action=‘DESELECT‘)

# Selects upper right quadrant of sphere
ut.act.select_by_loc((0, 0, 0), (1, 1, 1), ‘VERT‘,‘LOCAL‘)

# Selects nothing
ut.act.select_by_loc((0, 0, 0), (1, 1, 1), ‘VERT‘,‘GLOBAL‘)

# Selects upper right quadrant of sphere
ut.act.select_by_loc((0, 0, 0), (5, 5, 5), ‘VERT‘,‘LOCAL‘)

# Mess with it
bpy.ops.transform.translate(value = (1, 1, 1))
bpy.ops.transform.resize(value = (2, 2, 2))

# Selects lower half of sphere
ut.act.select_by_loc((-5, -5, -5), (5, 5, -0.5), ‘EDGE‘, ‘GLOBAL‘)

# Mess with it
bpy.ops.transform.translate(value = (0, 0, 3))
bpy.ops.transform.resize(value = (0.1, 0.1, 0.1))

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

技术图片技术图片

  1. Random Shape Growth
import sys
sys.path.append("/home/Documents/blender-scripts")
import ut
import importlib
importlib.reload(ut)

import bpy

from random import randint
from math import floor

# Must start in object mode
bpy.ops.object.select_all(action=‘SELECT‘)
bpy.ops.object.delete()

# Create a cube
bpy.ops.mesh.primitive_cube_add(size=0.5, location=(0, 0, 0))
bpy.context.object.name = ‘Cube-1‘

bpy.ops.object.mode_set(mode=‘EDIT‘)
bpy.ops.mesh.select_all(action="DESELECT")

for i in range(0, 100):

    # Grab the local coordinates
    coords = ut.coords(‘Cube-1‘, ‘LOCAL‘)

    # Find the bounding box for the object
    lower_bbox = [floor(min([v[i] for v in coords])) for i in [0, 1, 2]]
    upper_bbox = [floor(max([v[i] for v in coords])) for i in [0, 1, 2]]

    # Select a random face 2x2x1 units wide, snapped to integer coordinates
    lower_sel = [randint(l, u) for l, u in zip(lower_bbox, upper_bbox)]
    upper_sel = [l + 2 for l in lower_sel]
    upper_sel[randint(0, 2)] -= 1

    ut.act.select_by_loc(lower_sel, upper_sel, ‘FACE‘, ‘LOCAL‘)

    # Extrude the surface along it aggregate vertical normal
    bpy.ops.mesh.extrude_region_move(TRANSFORM_OT_translate =
            {"value": (0, 0, 1),
             "constraint_axis": (True, True, True)})

以上是关于The Bmesh Module的主要内容,如果未能解决你的问题,请参考以下文章

Blender BMesh数据结构解密

环境初始化 Build and Install the Apache Thrift IDL Compiler Install the Platform Development Tools(代码片段

maven web项目的web.xml报错The markup in the document following the root element must be well-formed.(代码片段

What's the difference between @Component, @Repository & @Service annotations in Spring?(代码片段

RuntimeError: An attempt has been made to start a new process before the current process has...(代码片段

[react] Module not found: Can't resolve 'schedule' in 'C:Usersadcaldvmtn7myapp (代码片段