Пример #1
0
def get_object_helper(context):
    obj = context.active_object
    if not utils.is_helper_object(obj):
        return None
    name = obj.name[len(utils.HELPER_OBJECT_NAME_PREFIX):]
    helper = __HELPERS__[name]
    if not helper.is_active(context):
        return None
    return helper
Пример #2
0
 def is_active(self, context=bpy.context):
     obj = context.active_object
     if utils.is_helper_object(obj):
         if obj.name != self._name:
             return False
         target = self._get_target_object(obj)
         return target is not None
     target = self.get_target()
     return target and self._is_active_target(target[1], context)
Пример #3
0
    def test_edit_center(self):
        op_edit = bpy.ops.io_scene_xray.edit_bone_center
        set_active_object(None)
        self.assertFalse(op_edit.poll(), msg='no armature')

        bpy.ops.xray_import.object(directory=self.relpath(),
                                   files=[{
                                       'name': 'test_fmt_armature.object'
                                   }],
                                   shaped_bones=False)
        arm = bpy.context.active_object
        bone = arm.data.bones['Bone1']
        arm.data.bones.active = bone
        self.assertTrue(op_edit.poll(), msg='an armature')
        self.assertIsNone(get_object_helper(bpy.context),
                          msg='no edit helper yet')

        op_edit()
        self.assertIsNotNone(get_object_helper(bpy.context),
                             msg='an edit helper')
        self.assertFalse(op_edit.poll(), msg='a helper is already active')
        helper = bpy.context.active_object
        self.assertTrue(is_helper_object(helper), msg='active is helper')
        self.assertGreater(helper.location.length, 0.001,
                           'a helper with some location')

        set_active_object(arm)
        self.assertFalse(op_edit.poll(), msg='a helper is still active')
        set_active_object(helper)

        helper.location = (0, 0, 0)
        self.assertFalse(bpy.ops.io_scene_xray.edit_bone_center_align.poll(),
                         msg='no shape')

        bone.xray.shape.type = '1'
        bpy.ops.io_scene_xray.edit_bone_center_align()
        self.assertEqual(round_vec(helper.location, 6), (0.1, 0, 0),
                         msg='move helper to box')

        bone.xray.shape.type = '2'
        bpy.ops.io_scene_xray.edit_bone_center_align()
        self.assertEqual(round_vec(helper.location, 6), (0, 0.2, 0),
                         msg='move helper to sphere')

        bone.xray.shape.type = '3'
        bpy.ops.io_scene_xray.edit_bone_center_align()
        self.assertEqual(round_vec(helper.location, 6), (0, 0, 0.3),
                         msg='move helper to cylinder')

        bpy.ops.io_scene_xray.edit_bone_center_apply()
        self.assertIsNone(get_object_helper(bpy.context),
                          msg='a helper is hidden')
        self.assertTrue(_has_nonzero(bone.xray.mass.center),
                        msg='has center relative')
Пример #4
0
    def test_edit_shape(self):
        op_edit = bpy.ops.io_scene_xray.edit_bone_shape
        if bpy.app.version >= (2, 80, 0):
            bpy.context.view_layer.objects.active = None
        else:
            bpy.context.scene.objects.active = None
        self.assertFalse(op_edit.poll(), msg='no armature')

        bpy.ops.xray_import.object(directory=self.relpath(),
                                   files=[{
                                       'name': 'test_fmt_armature.object'
                                   }])
        arm = bpy.context.active_object
        bone = arm.data.bones['Bone']
        arm.data.bones.active = bone
        self.assertFalse(op_edit.poll(), msg='an armature, no shape')
        bone.xray.shape.type = '1'
        self.assertTrue(op_edit.poll(), msg='an armature, with shape')
        self.assertIsNone(get_object_helper(bpy.context),
                          msg='no edit helper yet')

        op_edit()
        self.assertIsNotNone(get_object_helper(bpy.context),
                             msg='a box edit helper')
        self.assertFalse(op_edit.poll(), msg='a helper is already active')
        helper = bpy.context.active_object
        self.assertTrue(is_helper_object(helper), msg='active is helper')
        self.assertGreater(helper.scale.length, 0.001,
                           'a helper with some size')

        set_active_object(arm)
        self.assertFalse(op_edit.poll(), msg='a helper is still active')
        set_active_object(helper)

        helper.location = helper.scale = (1, 2, 3)
        bpy.ops.io_scene_xray.edit_bone_shape_fit()
        self.assertLess(helper.location.length, 0.1, msg='fit shape location')
        self.assertLess(helper.scale.length, 0.1, msg='fit shape size')
        self.assertIsNotNone(get_object_helper(bpy.context),
                             msg='a helper still shown')

        bpy.ops.io_scene_xray.edit_bone_shape_apply()
        self.assertIsNone(get_object_helper(bpy.context),
                          msg='a helper is hidden')
        self.assertTrue(_has_nonzero(bone.xray.shape.box_rot),
                        msg='has box_rot')
        self.assertFalse(_has_nonzero(bone.xray.shape.box_trn),
                         msg='has zero box_trn')
        self.assertTrue(_has_nonzero(bone.xray.shape.box_hsz),
                        msg='has box_hsz')
        scale = bone.xray.shape.get_matrix_basis().to_scale().to_tuple()
        self.assertGreater(scale, (0.999, 0.999, 0.999),
                           msg='close to 1:1 scale')
        self.assertLess(reapply_max_difference(bone.xray.shape),
                        0.1,
                        msg='box reapplies almost the same')

        bone.xray.shape.type = '2'
        op_edit()
        self.assertIsNotNone(get_object_helper(bpy.context),
                             msg='a sphere edit helper')
        helper = bpy.context.active_object
        helper.location = helper.scale = (1, 2, 3)
        bpy.ops.io_scene_xray.edit_bone_shape_fit()
        self.assertLess(helper.location.length,
                        0.1,
                        msg='fit shape sphere location')
        bpy.ops.io_scene_xray.edit_bone_shape_apply()
        self.assertGreater(bone.xray.shape.sph_rad, 0.01, msg='has sph_rad')
        self.assertLess(reapply_max_difference(bone.xray.shape),
                        0.1,
                        msg='sphere reapplies almost the same')

        bone.xray.shape.type = '3'
        op_edit()
        self.assertIsNotNone(get_object_helper(bpy.context),
                             msg='a cylinder edit helper')
        helper = bpy.context.active_object
        helper.location = helper.scale = (1, 2, 3)
        bpy.ops.io_scene_xray.edit_bone_shape_fit()
        self.assertLess(helper.location.length,
                        0.1,
                        msg='fit shape cylinder location')
        bpy.ops.io_scene_xray.edit_bone_shape_apply()
        self.assertGreater(bone.xray.shape.cyl_hgh, 0.01, msg='has cyl_hgh')
        self.assertLess(reapply_max_difference(bone.xray.shape),
                        0.1,
                        msg='cylinder reapplies almost the same')

        op_edit()
        self.assertIsNotNone(get_object_helper(bpy.context),
                             msg='anedit helper again')
        bpy.ops.io_scene_xray.edit_cancel()
        self.assertIsNone(get_object_helper(bpy.context),
                          msg='no edit helper again')

        op_edit()
        self.assertIsNotNone(get_object_helper(bpy.context),
                             msg='an edit helper once again')
        bone.xray.shape.type = '0'
        self.assertIsNone(get_object_helper(bpy.context),
                          msg='no edit helper once again')