Exemplo n.º 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)
Exemplo n.º 2
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
    SceneOp(bpy.context).select_object(obj)
Exemplo n.º 3
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(
            "MMD_TOOLS_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'
Exemplo n.º 4
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)
Exemplo n.º 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)
Exemplo n.º 6
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)
Exemplo n.º 7
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'}
Exemplo n.º 8
0
def _toggleVisibilityOfMeshes(self, context):
    root = self.id_data
    rig = mmd_model.Model(root)
    hide = not self.show_meshes
    for i in rig.meshes():
        i.hide = i.hide_render = hide
    if hide and not getattr(context, 'active_object', True):
        SceneOp(context).active_object = root
Exemplo n.º 9
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
Exemplo n.º 10
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
Exemplo n.º 11
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
Exemplo n.º 12
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
Exemplo n.º 13
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')
Exemplo n.º 14
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
Exemplo n.º 15
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'}
Exemplo n.º 16
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'}
Exemplo n.º 17
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
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 20
0
 def placeholder(self, create=False, binded=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
     if binded and obj and obj.data.shape_keys.key_blocks[0].mute:
         return None
     return obj
Exemplo n.º 21
0
 def execute(self, context):
     if self.bake_animation:
         from mmd_tools_local.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'}
Exemplo n.º 22
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]
Exemplo n.º 23
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)
Exemplo n.º 24
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')
Exemplo n.º 25
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'
Exemplo n.º 26
0
    def updateRigid(self, rigid_obj):
        assert (rigid_obj.mmd_type == 'RIGID_BODY')
        rb = rigid_obj.rigid_body
        if rb is None:
            return

        rigid = rigid_obj.mmd_rigid
        rigid_type = int(rigid.type)
        relation = rigid_obj.constraints['mmd_tools_rigid_parent']
        arm = relation.target
        bone_name = relation.subtarget

        if rigid_type == rigid_body.MODE_STATIC:
            rb.kinematic = True
        else:
            rb.kinematic = False

        if arm is not None and bone_name != '':
            target_bone = arm.pose.bones[bone_name]

            if rigid_type == rigid_body.MODE_STATIC:
                m = matmul(target_bone.matrix,
                           target_bone.bone.matrix_local.inverted())
                self.__rigid_body_matrix_map[rigid_obj] = m
                orig_scale = rigid_obj.scale.copy()
                to_matrix_world = matmul(rigid_obj.matrix_world,
                                         rigid_obj.matrix_local.inverted())
                matrix_world = matmul(to_matrix_world,
                                      matmul(m, rigid_obj.matrix_local))
                rigid_obj.parent = arm
                rigid_obj.parent_type = 'BONE'
                rigid_obj.parent_bone = bone_name
                rigid_obj.matrix_world = matrix_world
                rigid_obj.scale = orig_scale
                #relation.mute = False
                #relation.inverse_matrix = matmul(arm.matrix_world, target_bone.bone.matrix_local).inverted()
                fake_children = self.__fake_parent_map.get(rigid_obj, None)
                if fake_children:
                    for fake_child in fake_children:
                        logging.debug('          - fake_child: %s',
                                      fake_child.name)
                        t, r, s = matmul(m,
                                         fake_child.matrix_local).decompose()
                        fake_child.location = t
                        fake_child.rotation_euler = r.to_euler(
                            fake_child.rotation_mode)

            elif rigid_type in [
                    rigid_body.MODE_DYNAMIC, rigid_body.MODE_DYNAMIC_BONE
            ]:
                m = matmul(target_bone.matrix,
                           target_bone.bone.matrix_local.inverted())
                self.__rigid_body_matrix_map[rigid_obj] = m
                t, r, s = matmul(m, rigid_obj.matrix_local).decompose()
                rigid_obj.location = t
                rigid_obj.rotation_euler = r.to_euler(rigid_obj.rotation_mode)
                fake_children = self.__fake_parent_map.get(rigid_obj, None)
                if fake_children:
                    for fake_child in fake_children:
                        logging.debug('          - fake_child: %s',
                                      fake_child.name)
                        t, r, s = matmul(m,
                                         fake_child.matrix_local).decompose()
                        fake_child.location = t
                        fake_child.rotation_euler = r.to_euler(
                            fake_child.rotation_mode)

                if 'mmd_tools_rigid_track' not in target_bone.constraints:
                    empty = bpy.data.objects.new('mmd_bonetrack', None)
                    SceneOp(bpy.context).link_object(empty)
                    empty.matrix_world = target_bone.matrix
                    empty.empty_draw_size = 0.1 * self.__root.empty_draw_size
                    empty.empty_draw_type = 'ARROWS'
                    empty.mmd_type = 'TRACK_TARGET'
                    empty.hide = True
                    empty.parent = self.temporaryGroupObject()

                    rigid_obj.mmd_rigid.bone = bone_name
                    rigid_obj.constraints.remove(relation)

                    self.__empty_parent_map[empty] = rigid_obj

                    const_type = ('COPY_TRANSFORMS',
                                  'COPY_ROTATION')[rigid_type - 1]
                    const = target_bone.constraints.new(const_type)
                    const.mute = True
                    const.name = 'mmd_tools_rigid_track'
                    const.target = empty
                else:
                    empty = target_bone.constraints[
                        'mmd_tools_rigid_track'].target
                    ori_rigid_obj = self.__empty_parent_map[empty]
                    ori_rb = ori_rigid_obj.rigid_body
                    if ori_rb and rb.mass > ori_rb.mass:
                        logging.debug(
                            '        * Bone (%s): change target from [%s] to [%s]',
                            target_bone.name, ori_rigid_obj.name,
                            rigid_obj.name)
                        # re-parenting
                        rigid_obj.mmd_rigid.bone = bone_name
                        rigid_obj.constraints.remove(relation)
                        self.__empty_parent_map[empty] = rigid_obj
                        # revert change
                        ori_rigid_obj.mmd_rigid.bone = bone_name
                    else:
                        logging.debug('        * Bone (%s): track target [%s]',
                                      target_bone.name, ori_rigid_obj.name)

        rb.collision_shape = rigid.shape
Exemplo n.º 27
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
Exemplo n.º 28
0
 def execute(self, context):
     root = mmd_model.Model.findRoot(context.active_object)
     rig = mmd_model.Model(root)
     rig.build()
     SceneOp(context).active_object = root
     return {'FINISHED'}
Exemplo n.º 29
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)
Exemplo n.º 30
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