Dev:IT/2.5/Py/Scripts/Cookbook/Code snippets/Meshes

提供: wiki
移動先: 案内検索

Meshes

Mesh

Questo programma crea due Mesh. La prima è una piramide solida, con facce sia triangolari che quadrate, la seconda è un triangolo cavo. Il nome di entrambe le mesh è mostrato. Il triangolo è spostato di lato rispetto alla piramide, in modo che sia possibile vederlo, ciò richiede che sia selezionato. Code Snippets Meshes.png

#----------------------------------------------------------
# File meshes.py
#----------------------------------------------------------
import bpy

def createMesh(name, origin, verts, edges, faces):
    # Create mesh and object
    me = bpy.data.meshes.new(name+'Mesh')
    ob = bpy.data.objects.new(name, me)
    ob.location = origin
    ob.show_name = True
    # Link object to scene
    bpy.context.scene.objects.link(ob)

    # Create mesh from given verts, edges, faces. Either edges or
    # faces should be [], or you ask for problems
    me.from_pydata(verts, edges, faces)

    # Update mesh with new data
    me.update(calc_edges=True)
    return ob

def run(origin):
    (x,y,z) = (0.707107, 0.258819, 0.965926)
    verts1 = ((x,x,-1), (x,-x,-1), (-x,-x,-1), (-x,x,-1), (0,0,1))
    faces1 = ((1,0,4), (4,2,1), (4,3,2), (4,0,3), (0,1,2,3))
    ob1 = createMesh('Solid', origin, verts1, [], faces1)
    verts2 = ((x,x,0), (y,-z,0), (-z,y,0))
    edges2 = ((1,0), (1,2), (2,0))
    ob2 = createMesh('Edgy', origin, verts2, edges2, [])

    # Move second object out of the way
    ob1.select = False
    ob2.select = True
    bpy.ops.transform.translate(value=(0,2,0))
    return

if __name__ == "__main__":
    run((0,0,0))

Vertex groups e shapekeys

Questo programma aggiunge una UV sphere con 2 vertex groups (Left e Right) e quattro shapekeys. Code Snippets ShapeKey.png

#----------------------------------------------------------
# File shapekey.py
#----------------------------------------------------------
import bpy, random

def run(origin):
    # Add UV sphere
    bpy.ops.mesh.primitive_uv_sphere_add(
        segments=6, ring_count=5, size=1, location=origin)
    ob = bpy.context.object
    ob.name = 'ShapeKeyObject'
    ob.show_name = True

    # Create Left and Right vertex groups
    left = ob.vertex_groups.new('Left')
    right = ob.vertex_groups.new('Right')
    for v in ob.data.vertices:
        if v.co[0] > 0.001:
            left.add([v.index], 1.0, 'REPLACE')
        elif v.co[0] < -0.001:
            right.add([v.index], 1.0, 'REPLACE')
        else:
            left.add([v.index], 0.5, 'REPLACE')
            right.add([v.index], 0.5, 'REPLACE')

    # Add Basis key
    bpy.ops.object.shape_key_add(None)
    basis = ob.active_shape_key

    # Add FrontForward key: front verts move one unit forward
    # Slider from -1.0 to +2.0
    bpy.ops.object.shape_key_add(None)
    frontFwd = ob.active_shape_key
    frontFwd.name = 'FrontForward'
    frontFwd.slider_min = -1.0
    frontFwd.slider_max = 2.0    
    for v in [19, 20, 23, 24]:
        pt = frontFwd.data[v].co
        pt[1] = pt[1] - 1

    # Add TopUp keys: top verts move one unit up.  TopUp_L and 
    # TopUp_R only affect left and right halves, respectively
    keylist = [(None, ''), ('Left', '_L'), ('Right', '_R')]
    for (vgrp, suffix) in keylist:
        bpy.ops.object.shape_key_add(None)
        topUp = ob.active_shape_key
        topUp.name = 'TopUp' + suffix
        if vgrp:
            topUp.vertex_group = vgrp
        for v in [0, 1, 9, 10, 17, 18, 25]:
            pt = topUp.data[v].co
            pt[2] = pt[2] + 1

    # Pose shape keys
    for shape in ob.data.shape_keys.key_blocks:
        shape.value = random.random()
    return

if __name__ == "__main__":
    # Create five object with random shapekeys
    for j in range(5):
        run((3*j,0,0))

Applicare un modificatore Array

Questo programma crea una catena con dieci anelli. Un anello è un torus scalato lungo l'asse X. Diamo all'anello un modificatore Array il cui off-set è controllato da un Empty. Infine il modificatore è applicato rendendo la catena una mesh singola.

Code Snippets Chain.png

#----------------------------------------------------------
# File chain.py
# Creates an array modifier and applies it
# Update to API rev. 36523
#----------------------------------------------------------
import bpy
import math
from math import pi

def run(origin):
    # Add single chain link to the scene
    bpy.ops.mesh.primitive_torus_add(
        #major_radius=1, 
        #minor_radius=0.25, 
        major_segments=12, 
        minor_segments=8, 
        use_abso=True, 
        abso_major_rad=1, 
        abso_minor_rad=0.6, 
        location=(0,0,0), 
        rotation=(0,0,0))

    # Scale the torus along the x axis
    ob = bpy.context.object
    ob.scale = (0.7, 1, 1)
    bpy.ops.object.transform_apply(scale=True)

    # Create an empty
    bpy.ops.object.add(
        type='EMPTY', 
        location=(0,1.2,0.2), 
        rotation=(pi/2, pi/4, pi/2))
    empty = bpy.context.object

    # Make chain link active again
    scn = bpy.context.scene
    scn.objects.active = ob

    # Add modifier
    mod = ob.modifiers.new('Chain', 'ARRAY')
    mod.fit_type = 'FIXED_COUNT'
    mod.count = 10
    mod.use_relative_offset = 0
    mod.use_object_offset = True
    mod.offset_object = empty

    # Apply the modifier
    bpy.ops.object.visual_transform_apply()
    bpy.ops.object.modifier_apply(apply_as='DATA', modifier='Chain')

    # Move chain into place
    bpy.ops.transform.translate(value=origin)

    # Don't need empty anymore
    scn.objects.unlink(empty)
    del(empty)

    return

if __name__ == "__main__":
    run((0,3,0))