Ejemplo n.º 1
0
 def execute(self, context):
     if self.bake_animation:
         from core.mmd_tools.bpyutils import SceneOp
         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
         SceneOp(context).active_object = camera
     else:
         MMDCamera.convertToMMDCamera(context.active_object, self.scale)
     return {'FINISHED'}
Ejemplo n.º 2
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

        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 k.location)
            rx.co, rz.co, ry.co = ((frame, val) for val in 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)