码迷,mamicode.com
首页 > 其他好文 > 详细

The Bmesh Module

时间:2020-07-19 15:57:11      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:mit   ide   check   cat   and   mode   lib   opera   rip   

  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(‘\nBefore 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(‘\nAfter 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(‘\nAfter 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

标签:mit   ide   check   cat   and   mode   lib   opera   rip   

原文地址:https://www.cnblogs.com/jeremy-zhuhn/p/13339184.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!