Esempio n. 1
0
def rigRecon(input, context, timeOffset, rot, transl):
    for name in input.keys():
        entry = input[name]
        bpy.ops.object.select_pattern(pattern=name + "*", extend=False)
        print("pattern: %s" % name + '*')
        obj = context.selected_objects[0]
        print("Keyframing %s" % obj.name)

        prevPose = None
        prevEuler = None
        offset = Vector((0, 0, 0))
        for sFrameId in entry['log'].keys():
            state = entry['log'][sFrameId]
            frameId = int(sFrameId)
            #print( "Adding keyFrame %d" % frameId )
            context.scene.frame_set(frameId)
            obj.location = state['pos']
            obj.rotation_mode = 'QUATERNION'
            q = Quaternion(state['pose'])
            if prevPose:
                if q.dot(prevPose) < 0:
                    q.negate()
                    #print('newq:',q)
                # else:
                #     print('dotok:', prevPose, q, q.dot(prevPose))
            obj.rotation_quaternion = q
            #obj.rotation_euler = q.to_euler('XYZ')
            #obj.rotation_euler.x += offset.x
            #obj.rotation_euler.y += offset.y
            #obj.rotation_euler.z += offset.z
            #if prevEuler:
            #    d = Vector(obj.rotation_euler) - Vector(prevEuler)
            #    if d.length > 1:
            #        print("[",sFrameId,"] d: ", d )
            #        print('prev: ', prevEuler, 'new: ', obj.rotation_euler)
            #     if abs(d.x) > math.pi/2:
            #         offset.x += 2*math.pi
            #         obj.rotation_euler.x += 2*math.pi
            #         print( "offset.x:" , offset.x, 'newx: ', obj.rotation_euler.x )
            #     if abs(d.y) > math.pi/2:
            #       offset.y += 2*math.pi
            #        obj.rotation_euler.y += 2*math.pi
            #        print( "offset.y:" , offset.y, 'newy: ', obj.rotation_euler.y  )
            #    if abs(d.z) > math.pi/2:
            #        offset.z += 2*math.pi
            #        obj.rotation_euler.z += 2*math.pi
            #        print( "offset.z:" , offset.z, 'newz: ', obj.rotation_euler.z  )

            #obj.rotation_euler.x -= math.pi / 2.
            #bpy.ops.anim.keyframe_insert()
            #obj.keyframe_insert( data_path="LocRot", index=-1,frame=frameId)
            bpy.ops.anim.keying_set_active_set(type='BUILTIN_KSI_LocRot')
            bpy.ops.anim.keyframe_insert()
            #obj.keyframe_insert(index=-1)
            #obj.keyframe_insert(data_path="location", index=-1, frame=frameId)
            #obj.keyframe_insert(data_path="rotation", index=-1, frame=frameId)
            prevPose = q.copy()
            prevEuler = obj.rotation_euler.copy()
Esempio n. 2
0
class Transform(object):
    world_scale = 0.1
    world_scale_reciprocal = 10.0  # := 1/world_scale

    def __init__(self):
        self.translation = Vector((0, 0, 0))
        self.rotation = Quaternion()
        self.scale = 1

    def read(self, file):
        v = read_vector4(file)
        q = read_vector4(file)
        scale = read_vector4(file)

        self.translation = Vector(v.xyz) * self.world_scale
        self.rotation = Quaternion(q.wxyz)
        self.scale = scale.z

    def write(self, file):
        v = self.translation * self.world_scale_reciprocal
        v = (v.x, v.y, v.z, 0)
        q = self.rotation
        q = (q.x, q.y, q.z, q.w)
        scale = (self.scale, self.scale, self.scale, 0)

        write_vector4_raw(file, v)
        write_vector4_raw(file, q)
        write_vector4_raw(file, scale)

    def __mul__(self, other):
        t = Transform()
        v = Vector(other.translation)  # dup
        v.rotate(self.rotation)
        t.translation = self.translation + v * self.scale
        t.rotation = self.rotation * other.rotation
        t.scale = self.scale * other.scale
        return t

    def to_matrix(self):
        m_rotation = self.rotation.to_matrix().to_4x4()  # 3x3 to 4x4
        m_scale = Matrix.Scale(self.scale, 4)
        m = m_rotation * m_scale
        m.translation = self.translation
        return m

    def copy(self):
        t = Transform()
        t.translation = self.translation.copy()
        t.rotation = self.rotation.copy()
        t.scale = self.scale
        return t
Esempio n. 3
0
 def applied_influence(matrix, group_matrix=None):
     q = matrix.to_3x3().to_quaternion()
     loc = matrix.to_translation()
     if self.mode == 'A_TO_B':
         q0 = Quaternion([1, 0, 0, 0])
         loc0 = Vector()
     else:
         q0 = group_matrix.to_quaternion()
         if self.transform_mode == 'TRANSLATE':
             q = q0.copy()
         if self.transform_mode in {'ALL', 'TRANSLATE'}:
             loc0 = group_matrix.to_translation()
         else:
             loc0 = Vector()
     q = q * self.influence + q0 * (1.0 - self.influence)
     loc = loc * self.influence + loc0 * (1.0 - self.influence)
     mat = q.to_matrix().to_4x4()
     mat.col[3][:3] = loc
     return mat
Esempio n. 4
0
 def applied_influence(matrix, group_matrix=None):
     q = matrix.to_3x3().to_quaternion()
     loc = matrix.to_translation()
     if self.mode == 'A_TO_B':
         q0 = Quaternion([1, 0, 0, 0])
         loc0 = Vector()
     else:
         q0 = group_matrix.to_quaternion()
         if self.transform_mode == 'TRANSLATE':
             q = q0.copy()
         if self.transform_mode in {'ALL', 'TRANSLATE'}:
             loc0 = group_matrix.to_translation()
         else:
             loc0 = Vector()
     q = q * self.influence + q0 * (1.0 - self.influence)
     loc = loc * self.influence + loc0 * (1.0 - self.influence)
     mat = q.to_matrix().to_4x4()
     mat.col[3][:3] = loc
     return mat
Esempio n. 5
0
def deviate(branch_angle, branching, twist, initial_phyllotaxic_angle,
            plagiotropism_buds, gravitropism_buds, parent_direction, i, j):

    direction = parent_direction.normalized()

    axis = direction.to_track_quat('X', 'Z') * Vector((0.0, 0.0, 1.0))

    deviate_dir = Quaternion(axis, branch_angle) * direction

    offset = 0.0
    if branching == 1:
        offset = 2.4
    elif branching == 2:
        offset = 1.5708 + twist
    elif branching == 3:
        offset = 2.4
    elif branching == 4:
        offset = 0.7854 + twist
    elif branching == 5:
        offset = 0.6283
    elif branching == 6:
        offset = 0.5236

    phyllotaxis_angle = initial_phyllotaxic_angle + offset * i
    phyllotaxis_angle += 6.2832 * j / branching
    phyllotaxis_angle %= 6.2832

    flat_dir = parent_direction.copy()
    flat_dir.z = 0.0
    horizontal = 1.0 - (abs(parent_direction.angle(flat_dir, 0.0)) / 1.5708)
    if parent_direction.x == 0.0 and parent_direction.y == 0.0:
        horizontal = 0.0

    plagio = plagiotropism_buds * horizontal
    if plagio > 1.0:
        plagio = 1.0

    if phyllotaxis_angle < 1.5708:
        phyllotaxis_angle = (1.0 - plagio) * phyllotaxis_angle
    elif phyllotaxis_angle < 4.7124:
        phyllotaxis_angle = plagio * 3.1416 + (1.0 -
                                               plagio) * phyllotaxis_angle
    else:
        phyllotaxis_angle = plagio * 6.2832 + (1.0 -
                                               plagio) * phyllotaxis_angle

    deviate_dir = Quaternion(direction, phyllotaxis_angle) * deviate_dir

    flat_deviate_dir = deviate_dir.copy()
    flat_deviate_dir.z = 0.0
    strength = (1.0 - horizontal) * plagiotropism_buds
    deviate_dir = deviate_dir.lerp(flat_deviate_dir, strength)

    if gravitropism_buds < 0.0:
        vector_up = Vector((0.0, 0.0, 1.0))
        deviate_dir = deviate_dir.lerp(vector_up, -gravitropism_buds)
    else:
        vector_down = Vector((0.0, 0.0, -1.0))
        deviate_dir = deviate_dir.lerp(vector_down, gravitropism_buds)

    return deviate_dir
Esempio n. 6
0
# plant['q'] = Vector((0.0, 0.0, 1.0))
plant['q'] = Quaternion((0.0, 0.0, 0.0, 1.0))
plant['w'] = Vector((1.0, 0.0, 0.0))
plant['accel_e'] = Vector((0.0, 0.0, 0.0))

frame_step = 1
loop_num = 1
sc.frame_current = loop_num * frame_step
while sc.frame_current <= sc.frame_end:
    # plant["x"] = plant["x"] +
    delta_x = (plant["v"] * delta_t) + (
        (accel_g + plant['accel_e']) * delta_t * delta_t / 2)
    plant['x'] += delta_x
    plant['v'] = delta_x / delta_t
    wq = Quaternion(plant['w'][:], plant['w'].length)
    wqI = wq.copy()
    wqI.invert()
    q_w = wq * plant['q'] * wqI

    ops.object.select_pattern(pattern=plant['name'])
    # ops.transform.translate(value=(0,0,growth_rate))
    # ops.transform.translate(delta_x)
    ob = ctx.active_object
    ob.location = ob.location + delta_x
    ops.transform.rotate(value=plant['w'].length * delta_t, axis=plant['w'][:])
    # ops.transform.resize(value=(1,1,((loop_num+1)/loop_num)))
    # ob.location = (0,0,2)
    # ob.scale = (1,1,2)
    ops.anim.keyframe_insert_menu(type='Location')
    ops.anim.keyframe_insert_menu(type='Rotation')
    # ops.anim.keyframe_insert_menu(type='Scaling')
Esempio n. 7
0
def addKeyFrames( input, context ):
    for name in input.keys():
        entry = input[name];
        bpy.ops.object.select_pattern(pattern=name+"*", extend=False )
        #print( "pattern: %s" % name+'*')
        obj = context.selected_objects[0]
        print("Keyframing %s" % obj.name )
        
        prevPose = None
        prevEuler = None
        offset = Vector((0,0,0))
        for sFrameId in entry['log'].keys():
            state = entry['log'][sFrameId]
            frameId = int(sFrameId)
            #print( "Adding keyFrame %d" % frameId )
            context.scene.frame_set( frameId )
            hasPos = False
            hasPose = False
            if 'pos' in state:
                obj.location = state['pos']
                hasPos = True
            if 'pose' in state:
                obj.rotation_mode = 'QUATERNION'
                q = Quaternion(state['pose'])
                #if prevPose:
                    # if q.dot(prevPose) < 0:
                        # q.negate()
                    #     print('newq:',q)
                    # else:
                    #     print('dotok:', prevPose, q, q.dot(prevPose))
                obj.rotation_quaternion = q
                hasPose = True
            #obj.rotation_euler = q.to_euler('XYZ')
            #obj.rotation_euler.x += offset.x
            #obj.rotation_euler.y += offset.y
            #obj.rotation_euler.z += offset.z
            #if prevEuler:
            #    d = Vector(obj.rotation_euler) - Vector(prevEuler)
            #    if d.length > 1:
            #        print("[",sFrameId,"] d: ", d )
            #        print('prev: ', prevEuler, 'new: ', obj.rotation_euler)
           #     if abs(d.x) > math.pi/2:
           #         offset.x += 2*math.pi
           #         obj.rotation_euler.x += 2*math.pi
           #         print( "offset.x:" , offset.x, 'newx: ', obj.rotation_euler.x )
           #     if abs(d.y) > math.pi/2:
            #       offset.y += 2*math.pi
            #        obj.rotation_euler.y += 2*math.pi
            #        print( "offset.y:" , offset.y, 'newy: ', obj.rotation_euler.y  )
            #    if abs(d.z) > math.pi/2:
            #        offset.z += 2*math.pi
            #        obj.rotation_euler.z += 2*math.pi
            #        print( "offset.z:" , offset.z, 'newz: ', obj.rotation_euler.z  )

            #obj.rotation_euler.x -= math.pi / 2.
            #bpy.ops.anim.keyframe_insert()
            #obj.keyframe_insert( data_path="LocRot", index=-1,frame=frameId)
            # try:
            #     if hasPose and hasPos:
            #         bpy.ops.anim.keying_set_active_set( type='BUILTIN_KSI_LocRot' )
            #     elif hasPose:
            #         bpy.ops.anim.keying_set_active_set( type='BUILTIN_KSI_Rot' )
            #     elif hasPos:
            #         bpy.ops.anim.keying_set_active_set( type='BUILTIN_KSI_Loc')
            #     bpy.ops.anim.keyframe_insert()
            # except:
            if hasPos:
                obj.keyframe_insert( data_path="location", index=-1,frame=frameId)
            if hasPose:
                #print('adding pose keyframe')
                obj.keyframe_insert( data_path="rotation_quaternion", index=-1,frame=frameId)
            
            #obj.keyframe_insert(index=-1)
            #obj.keyframe_insert(data_path="location", index=-1, frame=frameId)
            #obj.keyframe_insert(data_path="rotation", index=-1, frame=frameId)
            if hasPose:
                prevPose = q.copy()
                prevEuler = obj.rotation_euler.copy()
            
        #context.scene.frame_set(current_frame)
        #bpy.context.active_object.hide = doHide
        #bpy.context.active_object.hide_render = doHide
        #bpy.context.active_object.keyframe_insert( data_path="hide",
        #                                           index=-1, 
        #                                         frame=current_frame)