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'}
def poll(cls, context): obj = context.active_object if obj is None: return False if obj.mmd_type == 'ROOT': return True if obj.mmd_type == 'NONE' and (obj.type == 'ARMATURE' or getattr( obj.data, 'shape_keys', None)): return True if MMDCamera.isMMDCamera(obj) or MMDLamp.isMMDLamp(obj): return True return False
def draw(self, context): obj = context.active_object layout = self.layout if MMDCamera.isMMDCamera(obj): mmd_cam = MMDCamera(obj) empty = mmd_cam.object() camera = mmd_cam.camera() row = layout.row() c = row.column() c.prop(empty, 'location') c.prop(camera, 'location', index=1, text='Distance') c = row.column() c.prop(empty, 'rotation_euler') layout.prop(empty.mmd_camera, 'angle') layout.prop(empty.mmd_camera, 'is_perspective') else: layout.operator('mmd_tools.convert_to_mmd_camera', text='Convert')
def assign(self, obj, action_name=None): if obj is None: return if action_name is None: action_name = os.path.splitext( os.path.basename(self.__vmdFile.filepath))[0] if MMDCamera.isMMDCamera(obj): self.__assignToCamera(obj, action_name + '_camera') elif MMDLamp.isMMDLamp(obj): self.__assignToLamp(obj, action_name + '_lamp') elif getattr(obj.data, 'shape_keys', None): self.__assignToMesh(obj, action_name + '_facial') elif obj.type == 'ARMATURE': self.__assignToArmature(obj, action_name + '_bone') elif obj.type == 'CAMERA' and self.__convert_mmd_camera: self.__assignToCamera(obj, action_name + '_camera') elif obj.type == 'LAMP' and self.__convert_mmd_lamp: self.__assignToLamp(obj, action_name + '_lamp') else: pass
def execute(self, context): params = { 'filepath': self.filepath, 'scale': self.scale, 'use_pose_mode': self.use_pose_mode, 'use_frame_range': self.use_frame_range, } obj = context.active_object if obj.mmd_type == 'ROOT': rig = mmd_model.Model(obj) params['mesh'] = rig.firstMesh() params['armature'] = rig.armature() params['model_name'] = obj.mmd_root.name or obj.name elif getattr(obj.data, 'shape_keys', None): params['mesh'] = obj params['model_name'] = obj.name elif obj.type == 'ARMATURE': params['armature'] = obj params['model_name'] = obj.name else: for i in context.selected_objects: if MMDCamera.isMMDCamera(i): params['camera'] = i elif MMDLamp.isMMDLamp(i): params['lamp'] = i try: start_time = time.time() vmd_exporter.VMDExporter().export(**params) logging.info(' Finished exporting motion in %f seconds.', time.time() - start_time) except Exception as e: err_msg = traceback.format_exc() logging.error(err_msg) self.report({'ERROR'}, err_msg) return {'FINISHED'}
def __exportCameraAnimation(self, cameraObj): if cameraObj is None: return None if not MMDCamera.isMMDCamera(cameraObj): logging.warning('[WARNING] camera "%s" is not MMDCamera', cameraObj.name) return None cam_rig = MMDCamera(cameraObj) mmd_cam = cam_rig.object() camera = cam_rig.camera() vmd_cam_anim = vmd.CameraAnimation() data = list(mmd_cam.location) + list(mmd_cam.rotation_euler) data.append(mmd_cam.mmd_camera.angle) data.append(mmd_cam.mmd_camera.is_perspective) data.append(camera.location.y) cam_curves = [_FCurve(i) for i in data ] # x, y, z, rx, ry, rz, fov, persp, distance animation_data = mmd_cam.animation_data if animation_data and animation_data.action: for fcurve in animation_data.action.fcurves: if fcurve.data_path == 'location': # x, y, z cam_curves[fcurve.array_index].setFCurve(fcurve) elif fcurve.data_path == 'rotation_euler': # rx, ry, rz cam_curves[3 + fcurve.array_index].setFCurve(fcurve) elif fcurve.data_path == 'mmd_camera.angle': # fov cam_curves[6].setFCurve(fcurve) elif fcurve.data_path == 'mmd_camera.is_perspective': # persp cam_curves[7].setFCurve(fcurve) animation_data = camera.animation_data if animation_data and animation_data.action: for fcurve in animation_data.action.fcurves: if fcurve.data_path == 'location' and fcurve.array_index == 1: # distance cam_curves[8].setFCurve(fcurve) for frame_number, x, y, z, rx, ry, rz, fov, persp, distance in self.__allFrameKeys( cam_curves): key = vmd.CameraKeyFrameKey() key.frame_number = frame_number - self.__frame_start key.location = [ x[0] * self.__scale, z[0] * self.__scale, y[0] * self.__scale ] key.rotation = [rx[0], rz[0], ry[0]] # euler key.angle = int(0.5 + math.degrees(fov[0])) key.distance = distance[0] * self.__scale key.persp = True if persp[0] else False #FIXME we can only choose one interpolation from (rx, ry, rz) for camera's rotation ir = self.__pickRotationInterpolation([rx[1], ry[1], rz[1]]) ix, iy, iz, iD, iF = x[1], z[1], y[1], distance[1], fov[1] key.interp = [ ix[0][0], ix[1][0], ix[0][1], ix[1][1], iy[0][0], iy[1][0], iy[0][1], iy[1][1], iz[0][0], iz[1][0], iz[0][1], iz[1][1], ir[0][0], ir[1][0], ir[0][1], ir[1][1], iD[0][0], iD[1][0], iD[0][1], iD[1][1], iF[0][0], iF[1][0], iF[0][1], iF[1][1], ] vmd_cam_anim.append(key) logging.info('(camera) frames:%5d name: %s', len(vmd_cam_anim), mmd_cam.name) return vmd_cam_anim
def poll(cls, context): obj = context.active_object return obj and (obj.type == 'CAMERA' or MMDCamera.isMMDCamera(obj))
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)