Форум программистов, компьютерный форум, киберфорум
Наши страницы
8Observer8
Войти
Регистрация
Восстановить пароль
Рейтинг: 5.00. Голосов: 2.

Mock for "cube_add" Blender API

Запись от 8Observer8 размещена 05.03.2019 в 21:37
Обновил(-а) 8Observer8 06.03.2019 в 18:26

Mock-objects are useful if you use external editor like VSCode or IDE like PyCharm, Eclipse and so on, because you can use DI (Dependency Injection) and you can debug your code logic with breakpoints.

For example, we want to add a cube on a scene:

Python
1
bpy.ops.mesh.primitive_cube_add()
You can read about this API function in the documentation: primitive_cube_add
  • Create a work folder with the name: mock-object-for-primitive_cube_add-api
  • Open Blender and safe project in the work folder
  • Open "Scripting" tab in Blender from the top menu
  • Open your favourite Python editor. I use VSCode. You can read about how to use VSCode with Python here Python in Visual Studio Code
  • Create a file with the name "main.py" in you favourite Python editor. This file must be placed in the "mock-object-for-primitive_cube_add-api" folder
  • Write in the "main.py":
    Python
    1
    
    print("hello from blender")
    You can run this code from command line terminal or from VSCode internal terminal. Press in VSCode "Ctrl+`" and enter command:
    Цитата:
    python main.py
    You will see in the console terminal this message:
    Цитата:
    "hello from blender"
    If you opened "Scripting" tab in Blender you will see an ability to open Python script in Blender.
  • Click on the "Open" Button in Blender Script editor inside Blender
  • Choose the "main.py" file and click the "Open Text Block" button
  • Open the Blender console terminal. For this you need to select in the main menu of Blender "Window" and select "Toggle System Console"
  • Run the "main.py" script from Blender. For this you need to place your mouse pointer on text area and press "Alt+P" button
  • You will see this message in the Blender console terminal:
    Цитата:
    "hello from blender"
    If you will change a code in an external editor like VSCode you need to reload in the Blender text editor. For this you need to press the "Alt+R+R" button

    You need to add only one file: "main.py" to the Blender text editor. Another files you need place in the work directory: "mock-object-for-primitive_cube_add-api"
  • Copy this code to the "main.py" file:

    main.py

    Python
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    
    import bpy
    import sys
    import os
     
    # Get a path to the directory with .blend file
    # There is the scripts in this directory
    dir = os.path.dirname(bpy.data.filepath)
     
    # Is the the directory in the list with included
    # directories? If no, include the directory
    if not dir in sys.path:
        sys.path.append(dir)
     
    import object3d_service
     
    # Reload module. It is necessary if you use
    # external editor like VSCode
    # For reloading module you need to press in
    # Blender: Alt + R + R
    import importlib
    importlib.reload(object3d_service)
     
    # Note. You do not need to open all scripts in Blender,
    # you need only this script
     
    from object3d_service import Object3DService
     
    def main():
        objectService = Object3DService()
        objectService.create_cube()
     
    if __name__ == "__main__":
        main()
    This is another files that you need to copy to the work directory:

    test_blender_service.py

    Python
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    import unittest
    from unittest.mock import MagicMock
     
    from object3d_service import Object3DService
     
    class BlenderServiceTest(unittest.TestCase):
     
        def test_myTest(self):
            # Arrange
            object3DService = Object3DService()
            object3DService.blender_api.create_cube = MagicMock("create_cube")
            
            # Act
            object3DService.create_cube()
     
            # Assert
            object3DService.blender_api.create_cube.assert_called_once()
    object3d_service.py

    Python
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    from blender_api import BlenderAPI
     
    class Object3DService:
     
        def __init__(self):
            self.blender_api = BlenderAPI()
     
        def create_cube(self):
            self.blender_api.create_cube()
    blender_api.py

    Python
    1
    2
    3
    4
    5
    6
    
    import bpy
     
    class BlenderAPI:
     
        def create_cube(self):
            bpy.ops.mesh.primitive_cube_add()

Delete a default cube from the scene. Now you can reload Blender code editor ("Alt+R+R") and run the code ("Alt + P"). You will see that a new code will be created:

Нажмите на изображение для увеличения
Название: mock-object-primitive_cube_add.png
Просмотров: 67
Размер:	68.1 Кб
ID:	5300

You can set breakpoints in "main.py" because there are mock-object for Blender API. And you can run unit tests using this command:
Цитата:
python -m unittest
You will see that unit test are passed.
Размещено в Без категории
Просмотров 229 Комментарии 0
Всего комментариев 0
Комментарии
 
КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Рейтинг@Mail.ru