201605091027[blender] IK like effect testing

https://www.youtube.com/watch?v=IHJdkdqL3uM&feature=youtu.be

import bpy
from mathutils import *
for o in bpy.data.objects:
    o.select = True
bpy.ops.object.delete()
segs = []
head = None
def init():
    for i in range( 5 ):
        bpy.ops.mesh.primitive_cone_add()
        bpy.ops.object.editmode_toggle()
        bpy.ops.transform.translate(value=(0,0,1))
        bpy.ops.object.editmode_toggle()
        bpy.context.object.name = 'cone_' + str(i)
        segs.append( bpy.context.object )
    segs[len(segs)-1].location.x = 0
    segs[len(segs)-1].location.y = 0
    segs[len(segs)-1].location.z = 0
    
    bpy.ops.object.empty_add(type='CUBE', location=(1, 2, 3))
    global head
    head = bpy.context.object
    
    
def reach( seg, target ):
    
    d = target - seg.location
    
    upvec = Vector((0,0,1))
    spinvec = upvec.cross(d)
    angle = upvec.angle(d)
    
    if spinvec.length == 0:
        spinvec.x = 1
    rm = Matrix.Rotation( angle, 4, spinvec )
    
    tm = Matrix.Translation( seg.location )
    seg.matrix_local = tm * rm 
    
    #need those line for data update
    seg.location = seg.location    
    bpy.context.scene.update()
    
    t = seg.matrix_local * Vector((0,0,2)) - seg.location
    return target + -t
def position( segA, segB ):
    segA.location = segB.matrix_local * Vector((0,0,2))
    
def update(scene):
    target = reach( segs[0], head.location )
    for i in range( len(segs) )[1::]:
        seg = segs[i]
        target = reach( seg, target )
    for i in range( len(segs )-1):
        ni = (len(segs) - 1) - i
        segA = segs[ni]
        segB = segs[ni-1]
        position(segB, segA )
        
    
        
init()
bpy.app.handlers.frame_change_pre.clear()
bpy.app.handlers.frame_change_pre.append( update )

回應

熟悉:

flash, 3ds max

 

玩過的技術有:

網站製作, 遊戲製作, 質子物理學, 粒子特效, 3d建模, 影片後製, 體感控制, 遊戲shader

    沒有新回應!
關鍵字