def execute(self, context):
     if self.bake_animation:
         obj = context.active_object
         targets = [x for x in context.selected_objects if x != obj]
         target = targets[0] if len(targets) == 1 else None
         if self.camera_source == 'SCENE':
             obj = None
         camera = MMDCamera.newMMDCameraAnimation(
             obj, target, self.scale, self.min_distance).camera()
         camera.select = True
         context.scene.objects.active = camera
     else:
         MMDCamera.convertToMMDCamera(context.active_object, self.scale)
     return {'FINISHED'}
 def execute(self, context):
     if self.bake_animation:
         obj = context.active_object
         target = None
         if len(context.selected_objects
                ) == 2 and obj in context.selected_objects:
             target = context.selected_objects[0]
             if target == obj:
                 target = context.selected_objects[1]
         elif len(context.selected_objects
                  ) == 1 and obj not in context.selected_objects:
             target = context.selected_objects[0]
         MMDCamera.newMMDCameraAnimation(obj, target, self.scale)
     else:
         MMDCamera.convertToMMDCamera(context.active_object, self.scale)
     return {'FINISHED'}
示例#3
0
    def __assignToCamera(self, cameraObj, action_name=None):
        mmdCameraInstance = MMDCamera.convertToMMDCamera(cameraObj, self.__scale)
        mmdCamera = mmdCameraInstance.object()
        cameraObj = mmdCameraInstance.camera()

        cameraAnim = self.__vmdFile.cameraAnimation
        logging.info('(camera) frames:%5d  name: %s', len(cameraAnim), mmdCamera.name)
        if len(cameraAnim) < 1:
            return

        action_name = action_name or mmdCamera.name
        parent_action = bpy.data.actions.new(name=action_name)
        distance_action = bpy.data.actions.new(name=action_name+'_dis')
        mmdCamera.animation_data_create().action = parent_action
        cameraObj.animation_data_create().action = distance_action

        _loc = _rot = lambda i: i
        if self.__mirror:
            _loc, _rot = _MirrorMapper.get_location, _MirrorMapper.get_rotation3

        fcurves = []
        for i in range(3):
            fcurves.append(parent_action.fcurves.new(data_path='location', index=i)) # x, y, z
        for i in range(3):
            fcurves.append(parent_action.fcurves.new(data_path='rotation_euler', index=i)) # rx, ry, rz
        fcurves.append(parent_action.fcurves.new(data_path='mmd_camera.angle')) # fov
        fcurves.append(parent_action.fcurves.new(data_path='mmd_camera.is_perspective')) # persp
        fcurves.append(distance_action.fcurves.new(data_path='location', index=1)) # dis
        for c in fcurves:
            c.keyframe_points.add(len(cameraAnim))

        prev_kps, indices = None, (0, 8, 4, 12, 12, 12, 16, 20) # x, z, y, rx, ry, rz, dis, fov
        cameraAnim.sort(key=lambda x:x.frame_number)
        for k, x, y, z, rx, ry, rz, fov, persp, dis in zip(cameraAnim, *(c.keyframe_points for c in fcurves)):
            frame = k.frame_number + self.__frame_margin
            x.co, z.co, y.co = ((frame, val*self.__scale) for val in _loc(k.location))
            rx.co, rz.co, ry.co = ((frame, val) for val in _rot(k.rotation))
            fov.co = (frame, math.radians(k.angle))
            dis.co = (frame, k.distance*self.__scale)
            persp.co = (frame, k.persp)

            persp.interpolation = 'CONSTANT'
            curr_kps = (x, y, z, rx, ry, rz, dis, fov)
            if prev_kps is not None:
                interp = k.interp
                for idx, prev_kp, kp in zip(indices, prev_kps, curr_kps):
                    self.__setInterpolation(interp[idx:idx+4:2]+interp[idx+1:idx+4:2], prev_kp, kp)
            prev_kps = curr_kps

        for fcurve in fcurves:
            self.__fixFcurveHandles(fcurve)
            if fcurve.data_path == 'rotation_euler':
                self.detectCameraChange(fcurve)