示例#1
0
def _getActiveMeshObject(prop):
    objects = SceneOp(bpy.context).id_objects
    active_obj = objects.active
    if (active_obj and active_obj.type == 'MESH'
            and active_obj.mmd_type == 'NONE'):
        prop['active_mesh_index'] = objects.find(active_obj.name)
    return prop.get('active_mesh_index', -1)
示例#2
0
    def storeShapeKeyOrder(cls, obj, shape_key_names):
        if len(shape_key_names) < 1:
            return
        assert (SceneOp(bpy.context).active_object == obj)
        if obj.data.shape_keys is None:
            bpy.ops.object.shape_key_add()

        if bpy.app.version < (2, 73, 0):

            def __move_to_bottom(key_blocks, name):
                obj.active_shape_key_index = key_blocks.find(name)
                for move in range(
                        len(key_blocks) - 1 - obj.active_shape_key_index):
                    bpy.ops.object.shape_key_move(type='DOWN')
        else:

            def __move_to_bottom(key_blocks, name):
                obj.active_shape_key_index = key_blocks.find(name)
                bpy.ops.object.shape_key_move(type='BOTTOM')

        key_blocks = obj.data.shape_keys.key_blocks
        for name in shape_key_names:
            if name not in key_blocks:
                obj.shape_key_add(name=name)
            elif len(key_blocks) > 1:
                __move_to_bottom(key_blocks, name)
示例#3
0
    def convertToMMDCamera(cameraObj, scale=1.0):
        if MMDCamera.isMMDCamera(cameraObj):
            return MMDCamera(cameraObj)

        empty = bpy.data.objects.new(name='MMD_Camera', object_data=None)
        SceneOp(bpy.context).link_object(empty)

        cameraObj.parent = empty
        cameraObj.data.dof_object = empty
        cameraObj.data.sensor_fit = 'VERTICAL'
        cameraObj.data.lens_unit = 'MILLIMETERS'  # MILLIMETERS, FOV
        cameraObj.data.ortho_scale = 25 * scale
        cameraObj.data.clip_end = 500 * scale
        cameraObj.data.draw_size = 5 * scale
        cameraObj.location = (0, -45 * scale, 0)
        cameraObj.rotation_mode = 'XYZ'
        cameraObj.rotation_euler = (math.radians(90), 0, 0)
        cameraObj.lock_location = (True, False, True)
        cameraObj.lock_rotation = (True, True, True)
        cameraObj.lock_scale = (True, True, True)
        MMDCamera.addDrivers(cameraObj)

        empty.location = (0, 0, 10 * scale)
        empty.rotation_mode = 'YXZ'
        empty.empty_draw_size = 5 * scale
        empty.lock_scale = (True, True, True)
        empty.mmd_type = 'CAMERA'
        empty.mmd_camera.angle = math.radians(30)
        empty.mmd_camera.persp = True
        return MMDCamera(empty)
示例#4
0
 def execute(self, context):
     obj = context.active_object
     root = mmd_model.Model.findRoot(obj)
     rig = mmd_model.Model(root)
     rig.applyAdditionalTransformConstraints()
     SceneOp(context).active_object = obj
     return {'FINISHED'}
示例#5
0
    def convertToMMDLamp(lampObj, scale=1.0):
        if MMDLamp.isMMDLamp(lampObj):
            return MMDLamp(lampObj)

        empty = bpy.data.objects.new(name='MMD_Light', object_data=None)
        SceneOp(bpy.context).link_object(empty)

        empty.rotation_mode = 'XYZ'
        empty.lock_rotation = (True, True, True)
        empty.empty_draw_size = 0.4
        empty.scale = [10 * scale] * 3
        empty.mmd_type = 'LIGHT'
        empty.location = (0, 0, 11 * scale)

        lampObj.parent = empty
        lampObj.data.color = (0.602, 0.602, 0.602)
        lampObj.location = (0.5, -0.5, 1.0)
        lampObj.rotation_mode = 'XYZ'
        lampObj.rotation_euler = (0, 0, 0)
        lampObj.lock_rotation = (True, True, True)

        constraint = lampObj.constraints.new(type='TRACK_TO')
        constraint.name = 'mmd_lamp_track'
        constraint.target = empty
        constraint.track_axis = 'TRACK_NEGATIVE_Z'
        constraint.up_axis = 'UP_Y'

        return MMDLamp(empty)
示例#6
0
    def draw(self, context):
        active_obj = context.active_object
        root = mmd_model.Model.findRoot(active_obj)
        if root is None:
            self.layout.label(text='Select a MMD Model')
            return

        col = self.layout.column()
        c = col.column(align=True)

        row = c.row()
        row.template_list(
            "UL_joints",
            "",
            SceneOp(context).id_scene,
            'objects',
            root.mmd_root,
            'active_joint_index',
        )
        tb = row.column()
        tb1 = tb.column(align=True)
        tb1.operator('mmd_tools.joint_add', text='', icon=ICON_ADD)
        tb1.operator('mmd_tools.joint_remove', text='', icon=ICON_REMOVE)
        tb1.menu('OBJECT_MT_mmd_tools_joint_menu',
                 text='',
                 icon='DOWNARROW_HLT')
        tb.separator()
        tb1 = tb.column(align=True)
        tb1.enabled = active_obj.mmd_type == 'JOINT'
        tb1.operator('mmd_tools.object_move', text='',
                     icon='TRIA_UP').type = 'UP'
        tb1.operator('mmd_tools.object_move', text='',
                     icon='TRIA_DOWN').type = 'DOWN'
示例#7
0
def _toggleVisibilityOfJoints(self, context):
    root = self.id_data
    rig = mmd_model.Model(root)
    hide = not self.show_joints
    for i in rig.joints():
        i.hide = hide
    if hide and context.active_object is None:
        SceneOp(context).active_object = root
示例#8
0
def _toggleVisibilityOfTemporaryObjects(self, context):
    root = self.id_data
    rig = mmd_model.Model(root)
    hide = not self.show_temporary_objects
    for i in rig.temporaryObjects(rigid_track_only=True):
        i.hide = hide
    if hide and context.active_object is None:
        SceneOp(context).active_object = root
示例#9
0
def _setVisibilityOfMMDRigArmature(prop, v):
    obj = prop.id_data
    rig = mmd_model.Model(obj)
    arm = rig.armature()
    if arm is None:
        return
    if bpy.context.active_object == arm:
        SceneOp(bpy.context).active_object = obj
    arm.hide = not v
示例#10
0
def selectAObject(obj):
    import bpy
    try:
        bpy.ops.object.mode_set(mode='OBJECT')
    except Exception:
        pass
    bpy.ops.object.select_all(action='DESELECT')
    SceneOp(bpy.context).active_object = obj
    obj.select = True
示例#11
0
def setParentToBone(obj, parent, bone_name):
    import bpy
    selectAObject(parent)
    bpy.ops.object.mode_set(mode='POSE')
    selectAObject(obj)
    SceneOp(bpy.context).active_object = parent
    parent.select = True
    bpy.ops.object.mode_set(mode='POSE')
    parent.data.bones.active = parent.data.bones[bone_name]
    bpy.ops.object.parent_set(type='BONE', xmirror=False, keep_transform=False)
    bpy.ops.object.mode_set(mode='OBJECT')
示例#12
0
 def execute(self, context):
     obj = context.active_object
     root = mmd_model.Model.findRoot(context.active_object)
     rig = mmd_model.Model(root)
     if self.type == 'BIND':
         rig.morph_slider.bind()
     elif self.type == 'UNBIND':
         rig.morph_slider.unbind()
     else:
         rig.morph_slider.create()
     SceneOp(context).active_object = obj
     return {'FINISHED'}
示例#13
0
def selectSingleBone(context, armature, bone_name, reset_pose=False):
    import bpy
    try:
        bpy.ops.object.mode_set(mode='OBJECT')
    except:
        pass
    for i in context.selected_objects:
        i.select = False
    SceneOp(context).select_object(armature)
    SceneOp(context).active_object = armature
    bpy.ops.object.mode_set(mode='POSE')
    if reset_pose:
        for p_bone in armature.pose.bones:
            p_bone.matrix_basis.identity()
    armature_bones = armature.data.bones
    for i in armature_bones:
        i.select = (i.name == bone_name)
        i.select_head = i.select_tail = i.select
        if i.select:
            armature_bones.active = i
            i.hide = False
示例#14
0
 def __dummy_armature(self, obj, create=False):
     arm = next((x for x in obj.children
                 if x.mmd_type == 'PLACEHOLDER' and x.type == 'ARMATURE'),
                None)
     if create and arm is None:
         arm = bpy.data.objects.new(
             name='.dummy_armature',
             object_data=bpy.data.armatures.new(name='.dummy_armature'))
         arm.mmd_type = 'PLACEHOLDER'
         arm.parent = obj
         SceneOp(bpy.context).link_object(arm)
         arm.data.draw_type = 'STICK'
     return arm
示例#15
0
    def execute(self, context):
        #TODO convert some basic MMD properties
        armature = context.active_object
        scale = 1
        model_name = 'New MMD Model'

        root = mmd_model.Model.findRoot(armature)
        if root is None or root != armature.parent:
            rig = mmd_model.Model.create(model_name, model_name, scale, armature=armature)

        self.__attach_meshes_to(armature, SceneOp(context).id_objects)
        self.__configure_rig(mmd_model.Model(armature.parent))
        return {'FINISHED'}
示例#16
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'}
示例#17
0
 def placeholder(self, create=False):
     rig = self.__rig
     root = rig.rootObject()
     obj = next((x for x in root.children
                 if x.mmd_type == 'PLACEHOLDER' and x.type == 'MESH'), None)
     if create and obj is None:
         obj = bpy.data.objects.new(
             name='.placeholder',
             object_data=bpy.data.meshes.new('.placeholder'))
         obj.mmd_type = 'PLACEHOLDER'
         obj.parent = root
         SceneOp(bpy.context).link_object(obj)
     if obj and obj.data.shape_keys is None:
         key = obj.shape_key_add(name='--- morph sliders ---')
         key.mute = True
     return obj
示例#18
0
 def jointGroupObject(self):
     if self.__joint_grp is None:
         for i in filter(lambda x: x.mmd_type == 'JOINT_GRP_OBJ',
                         self.__root.children):
             self.__joint_grp = i
             break
         if self.__joint_grp is None:
             joints = bpy.data.objects.new(name='joints', object_data=None)
             joints.mmd_type = 'JOINT_GRP_OBJ'
             joints.parent = self.__root
             joints.hide = joints.hide_select = True
             joints.lock_rotation = joints.lock_location = joints.lock_scale = [
                 True, True, True
             ]
             SceneOp(bpy.context).link_object(joints)
             self.__joint_grp = joints
     return self.__joint_grp
示例#19
0
 def rigidGroupObject(self):
     if self.__rigid_grp is None:
         for i in filter(lambda x: x.mmd_type == 'RIGID_GRP_OBJ',
                         self.__root.children):
             self.__rigid_grp = i
             break
         if self.__rigid_grp is None:
             rigids = bpy.data.objects.new(name='rigidbodies',
                                           object_data=None)
             rigids.mmd_type = 'RIGID_GRP_OBJ'
             rigids.parent = self.__root
             rigids.hide = rigids.hide_select = True
             rigids.lock_rotation = rigids.lock_location = rigids.lock_scale = [
                 True, True, True
             ]
             SceneOp(bpy.context).link_object(rigids)
             self.__rigid_grp = rigids
     return self.__rigid_grp
示例#20
0
 def temporaryGroupObject(self):
     if self.__temporary_grp is None:
         for i in filter(lambda x: x.mmd_type == 'TEMPORARY_GRP_OBJ',
                         self.__root.children):
             self.__temporary_grp = i
             break
         if self.__temporary_grp is None:
             temporarys = bpy.data.objects.new(name='temporary',
                                               object_data=None)
             temporarys.mmd_type = 'TEMPORARY_GRP_OBJ'
             temporarys.parent = self.__root
             temporarys.hide = temporarys.hide_select = True
             temporarys.lock_rotation = temporarys.lock_location = temporarys.lock_scale = [
                 True, True, True
             ]
             SceneOp(bpy.context).link_object(temporarys)
             self.__temporary_grp = temporarys
     return self.__temporary_grp
示例#21
0
    def __makeSpring(self, target, base_obj, spring_stiffness):
        with bpyutils.select_object(target):
            bpy.ops.object.duplicate()
            spring_target = SceneOp(bpy.context).active_object
        t = spring_target.constraints.get('mmd_tools_rigid_parent')
        if t is not None:
            spring_target.constraints.remove(t)
        spring_target.mmd_type = 'SPRING_GOAL'
        spring_target.rigid_body.kinematic = True
        spring_target.rigid_body.collision_groups = (False, False, False,
                                                     False, False, False,
                                                     False, False, False,
                                                     False, False, False,
                                                     False, False, False,
                                                     False, False, False,
                                                     False, True)
        spring_target.parent = base_obj
        spring_target.matrix_parent_inverse = mathutils.Matrix(
            base_obj.matrix_basis).inverted()
        spring_target.hide = True

        obj = bpy.data.objects.new('S.' + target.name, None)
        SceneOp(bpy.context).link_object(obj)
        obj.location = target.location
        obj.empty_draw_size = 0.1
        obj.empty_draw_type = 'ARROWS'
        obj.hide_render = True
        obj.select = False
        obj.hide = True
        obj.mmd_type = 'SPRING_CONSTRAINT'
        obj.parent = self.temporaryGroupObject()

        with bpyutils.select_object(obj):
            bpy.ops.rigidbody.constraint_add(type='GENERIC_SPRING')
        rbc = obj.rigid_body_constraint
        rbc.object1 = target
        rbc.object2 = spring_target

        rbc.use_spring_x = True
        rbc.use_spring_y = True
        rbc.use_spring_z = True

        rbc.spring_stiffness_x = spring_stiffness[0]
        rbc.spring_stiffness_y = spring_stiffness[1]
        rbc.spring_stiffness_z = spring_stiffness[2]
示例#22
0
    def create(name,
               name_e='',
               scale=1,
               obj_name=None,
               armature=None,
               add_root_bone=False):
        scene = SceneOp(bpy.context)
        if obj_name is None:
            obj_name = name

        root = bpy.data.objects.new(name=obj_name, object_data=None)
        root.mmd_type = 'ROOT'
        root.mmd_root.name = name
        root.mmd_root.name_e = name_e
        root.empty_draw_size = scale / 0.2
        scene.link_object(root)

        armObj = armature
        if armObj:
            m = armObj.matrix_world
            armObj.parent_type = 'OBJECT'
            armObj.parent = root
            #armObj.matrix_world = m
            root.matrix_world = m
            armObj.matrix_local.identity()
        else:
            arm = bpy.data.armatures.new(name=obj_name)
            #arm.draw_type = 'STICK'
            armObj = bpy.data.objects.new(name=obj_name + '_arm',
                                          object_data=arm)
            armObj.parent = root
            scene.link_object(armObj)
        armObj.lock_rotation = armObj.lock_location = armObj.lock_scale = [
            True, True, True
        ]
        armObj.show_x_ray = True
        armObj.draw_type = 'WIRE'

        if add_root_bone:
            bone_name = u'全ての親'
            with bpyutils.edit_object(armObj) as data:
                bone = data.edit_bones.new(name=bone_name)
                bone.head = [0.0, 0.0, 0.0]
                bone.tail = [0.0, 0.0, root.empty_draw_size]
            armObj.pose.bones[bone_name].mmd_bone.name_j = bone_name
            armObj.pose.bones[bone_name].mmd_bone.name_e = 'Root'

        bpyutils.select_object(root)
        return Model(root)
示例#23
0
 def fixShapeKeyOrder(cls, obj, shape_key_names):
     if len(shape_key_names) < 1:
         return
     assert (SceneOp(bpy.context).active_object == obj)
     key_blocks = getattr(obj.data.shape_keys, 'key_blocks', None)
     if key_blocks is None:
         return
     if bpy.app.version < (2, 73, 0):
         len_key_blocks = len(key_blocks)
         for ii, name in enumerate(x for x in reversed(shape_key_names)
                                   if x in key_blocks):
             obj.active_shape_key_index = idx = key_blocks.find(name)
             offset = (len_key_blocks - 1 - idx) - ii
             move_type = 'UP' if offset < 0 else 'DOWN'
             for move in range(abs(offset)):
                 bpy.ops.object.shape_key_move(type=move_type)
     else:
         for name in shape_key_names:
             idx = key_blocks.find(name)
             if idx < 0:
                 continue
             obj.active_shape_key_index = idx
             bpy.ops.object.shape_key_move(type='BOTTOM')
示例#24
0
    def draw(self, context):
        layout = self.layout
        active_obj = context.active_object
        root = Model.findRoot(active_obj)
        if root is None:
            layout.label(text='Select a MMD Model')
            return

        col = layout.column(align=True)
        row = col.row()
        row.template_list("UL_ModelMeshes", "",
                          SceneOp(context).id_scene, 'objects', root.mmd_root,
                          "active_mesh_index")
        tb = row.column()
        tb1 = tb.column(align=True)
        tb1.enabled = active_obj.type == 'MESH' and active_obj.mmd_type == 'NONE'
        tb1.operator('mmd_tools.object_move', text='',
                     icon=TRIA_UP_BAR).type = 'TOP'
        tb1.operator('mmd_tools.object_move', text='',
                     icon='TRIA_UP').type = 'UP'
        tb1.operator('mmd_tools.object_move', text='',
                     icon='TRIA_DOWN').type = 'DOWN'
        tb1.operator('mmd_tools.object_move', text='',
                     icon=TRIA_DOWN_BAR).type = 'BOTTOM'
示例#25
0
def _setActiveRigidbodyObject(prop, v):
    obj = SceneOp(bpy.context).id_objects[v]
    if mmd_model.isRigidBodyObject(obj):
        obj.hide = False
        utils.selectAObject(obj)
    prop['active_rigidbody_object_index'] = v
示例#26
0
    def newMMDCameraAnimation(cameraObj,
                              cameraTarget=None,
                              scale=1.0,
                              min_distance=0.1):
        scene = bpy.context.scene
        mmd_cam = bpy.data.objects.new(
            name='Camera', object_data=bpy.data.cameras.new('Camera'))
        SceneOp(bpy.context).link_object(mmd_cam)
        MMDCamera.convertToMMDCamera(mmd_cam, scale=scale)
        mmd_cam_root = mmd_cam.parent

        _camera_override_func = None
        if cameraObj is None:
            if scene.camera is None:
                scene.camera = mmd_cam
                return MMDCamera(mmd_cam_root)
            _camera_override_func = lambda: scene.camera

        _target_override_func = None
        if cameraTarget is None:
            _target_override_func = lambda camObj: camObj.data.dof_object or camObj

        action_name = mmd_cam_root.name
        parent_action = bpy.data.actions.new(name=action_name)
        distance_action = bpy.data.actions.new(name=action_name + '_dis')
        MMDCamera.removeDrivers(mmd_cam)

        from math import atan
        from mathutils import Matrix, Vector
        from core.mmd_tools.bpyutils import matmul

        render = scene.render
        factor = (render.resolution_y * render.pixel_aspect_y) / (
            render.resolution_x * render.pixel_aspect_x)
        matrix_rotation = Matrix(
            ([1, 0, 0, 0], [0, 0, 1, 0], [0, -1, 0, 0], [0, 0, 0, 1]))
        neg_z_vector = Vector((0, 0, -1))
        frame_start, frame_end, frame_current = scene.frame_start, scene.frame_end + 1, scene.frame_current
        frame_count = frame_end - frame_start
        frames = range(frame_start, frame_end)

        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(frame_count)

        for f, x, y, z, rx, ry, rz, fov, persp, dis in zip(
                frames, *(c.keyframe_points for c in fcurves)):
            scene.frame_set(f)
            if _camera_override_func:
                cameraObj = _camera_override_func()
            if _target_override_func:
                cameraTarget = _target_override_func(cameraObj)
            cam_matrix_world = cameraObj.matrix_world
            cam_target_loc = cameraTarget.matrix_world.translation
            cam_rotation = matmul(cam_matrix_world, matrix_rotation).to_euler(
                mmd_cam_root.rotation_mode)
            cam_vec = matmul(cam_matrix_world.to_3x3(), neg_z_vector)
            if cameraObj.data.type == 'ORTHO':
                cam_dis = -(9 / 5) * cameraObj.data.ortho_scale
                if cameraObj.data.sensor_fit != 'VERTICAL':
                    if cameraObj.data.sensor_fit == 'HORIZONTAL':
                        cam_dis *= factor
                    else:
                        cam_dis *= min(1, factor)
            else:
                target_vec = cam_target_loc - cam_matrix_world.translation
                cam_dis = -max(
                    target_vec.length * cam_vec.dot(target_vec.normalized()),
                    min_distance)
            cam_target_loc = cam_matrix_world.translation - cam_vec * cam_dis

            tan_val = cameraObj.data.sensor_height / cameraObj.data.lens / 2
            if cameraObj.data.sensor_fit != 'VERTICAL':
                ratio = cameraObj.data.sensor_width / cameraObj.data.sensor_height
                if cameraObj.data.sensor_fit == 'HORIZONTAL':
                    tan_val *= factor * ratio
                else:  # cameraObj.data.sensor_fit == 'AUTO'
                    tan_val *= min(ratio, factor * ratio)

            x.co, y.co, z.co = ((f, i) for i in cam_target_loc)
            rx.co, ry.co, rz.co = ((f, i) for i in cam_rotation)
            dis.co = (f, cam_dis)
            fov.co = (f, 2 * atan(tan_val))
            persp.co = (f, cameraObj.data.type != 'ORTHO')
            persp.interpolation = 'CONSTANT'
            for kp in (x, y, z, rx, ry, rz, fov, dis):
                kp.interpolation = 'LINEAR'

        MMDCamera.addDrivers(mmd_cam)
        mmd_cam_root.animation_data_create().action = parent_action
        mmd_cam.animation_data_create().action = distance_action
        scene.frame_set(frame_current)
        return MMDCamera(mmd_cam_root)
示例#27
0
def _setActiveMeshObject(prop, v):
    obj = SceneOp(bpy.context).id_objects[v]
    if obj.type == 'MESH' and obj.mmd_type == 'NONE':
        obj.hide = False
        utils.selectAObject(obj)
    prop['active_mesh_index'] = v
示例#28
0
def _getActiveJointObject(prop):
    objects = SceneOp(bpy.context).id_objects
    active_obj = objects.active
    if mmd_model.isJointObject(active_obj):
        prop['active_joint_object_index'] = objects.find(active_obj.name)
    return prop.get('active_joint_object_index', 0)
示例#29
0
def _setActiveJointObject(prop, v):
    obj = SceneOp(bpy.context).id_objects[v]
    if mmd_model.isJointObject(obj):
        obj.hide = False
        utils.selectAObject(obj)
    prop['active_joint_object_index'] = v
示例#30
0
def _getActiveRigidbodyObject(prop):
    objects = SceneOp(bpy.context).id_objects
    active_obj = objects.active
    if mmd_model.isRigidBodyObject(active_obj):
        prop['active_rigidbody_object_index'] = objects.find(active_obj.name)
    return prop.get('active_rigidbody_object_index', 0)