Hướng dẫn blender python random float - máy xay sinh tố python float ngẫu nhiên

Đối Tượng Thính Giả Dự Định (Intended Audience)

This tutorial is designed to help technical artists or developers learn to extend Blender. An understanding of the basics of Python is expected for those working through this tutorial.

Nội dung chính ShowShow

  • Đối Tượng Thính Giả Dự Định (Intended Audience)
  • Điều Kiện Tiên Quyết (Đòi Hỏi) (Prerequisites)
  • Những Đường Kết Nối đến Tài Liệu (Documentation Links)
  • Trình Bổ Sung là gì? (What is an Add-on?)
  • Trình Bổ Sung Đầu Tiên của Bạn (Your First Add-on)
  • Viết Tập Lệnh (Write the Script)
  • Viết Trình Bổ Sung [Đơn Giản] (Write the Add-on [Simple])
  • Cài Đặt Trình Bổ Sung (Install the Add-on)
  • Trình Bổ Sung Thứ Hai của Bạn (Your Second Add-on)
  • Viết Tập Lệnh (Write the Script)
  • Viết Trình Bổ Sung [Đơn Giản] (Write the Add-on [Simple])
  • Cài Đặt Trình Bổ Sung (Install the Add-on)
  • Trình Bổ Sung Thứ Hai của Bạn (Your Second Add-on)

Điều Kiện Tiên Quyết (Đòi Hỏi) (Prerequisites)

Những Đường Kết Nối đến Tài Liệu (Documentation Links)

  • Trình Bổ Sung là gì? (What is an Add-on?)

  • Trình Bổ Sung Đầu Tiên của Bạn (Your First Add-on)

  • Viết Tập Lệnh (Write the Script)

  • Viết Trình Bổ Sung [Đơn Giản] (Write the Add-on [Simple])

  • Cài Đặt Trình Bổ Sung (Install the Add-on)

Trình Bổ Sung Thứ Hai của Bạn (Your Second Add-on)

  • Viết Trình Bổ Sung (Write the Add-on)

  • Kết Luận (Conclusions)

Đọc Thêm (Further Reading)

Before going through the tutorial you should...

Be familiar with the basics of working in Blender.Developer Extras in the preferences to enable features that make developing add-ons easier.

Trình Bổ Sung là gì? (What is an Add-on?)

  • Trình Bổ Sung Đầu Tiên của Bạn (Your First Add-on)

  • Viết Tập Lệnh (Write the Script)

  • Viết Trình Bổ Sung [Đơn Giản] (Write the Add-on [Simple])

Trình Bổ Sung là gì? (What is an Add-on?)

Trình Bổ Sung Đầu Tiên của Bạn (Your First Add-on)

Viết Tập Lệnh (Write the Script)

Viết Trình Bổ Sung [Đơn Giản] (Write the Add-on [Simple])

Cài Đặt Trình Bổ Sung (Install the Add-on)

import bpy

scene = bpy.context.scene
for obj in scene.objects:
    obj.location.x += 1.0
3

Trình Bổ Sung Thứ Hai của Bạn (Your Second Add-on)

import bpy

scene = bpy.context.scene
for obj in scene.objects:
    obj.location.x += 1.0
4

Viết Trình Bổ Sung (Write the Add-on)

Kết Luận (Conclusions)

Đọc Thêm (Further Reading)

Before going through the tutorial you should...

Be familiar with the basics of working in Blender.

Know how to run a script in Blender's Text editor.

Have an understanding of Python primitive types (integer, Boolean, string, list, tuple, dictionary, and set).

Trình Bổ Sung Đầu Tiên của Bạn (Your First Add-on)

Viết Tập Lệnh (Write the Script)

Viết Trình Bổ Sung [Đơn Giản] (Write the Add-on [Simple])

Viết Tập Lệnh (Write the Script)

Viết Trình Bổ Sung [Đơn Giản] (Write the Add-on [Simple])

import bpy

scene = bpy.context.scene
for obj in scene.objects:
    obj.location.x += 1.0

Cài Đặt Trình Bổ Sung (Install the Add-on)Run Script button, all objects in the active scene are moved by 1.0 unit.

Viết Trình Bổ Sung [Đơn Giản] (Write the Add-on [Simple])

Cài Đặt Trình Bổ Sung (Install the Add-on)

bl_info = {
    "name": "Move X Axis",
    "blender": (2, 80, 0),
    "category": "Object",
}

import bpy


class ObjectMoveX(bpy.types.Operator):
    """My Object Moving Script"""      # Use this as a tooltip for menu items and buttons.
    bl_idname = "object.move_x"        # Unique identifier for buttons and menu items to reference.
    bl_label = "Move X by One"         # Display name in the interface.
    bl_options = {'REGISTER', 'UNDO'}  # Enable undo for the operator.

    def execute(self, context):        # execute() is called when running the operator.

        # The original script
        scene = context.scene
        for obj in scene.objects:
            obj.location.x += 1.0

        return {'FINISHED'}            # Lets Blender know the operator finished successfully.

def menu_func(self, context):
    self.layout.operator(ObjectMoveX.bl_idname)

def register():
    bpy.utils.register_class(ObjectMoveX)
    bpy.types.VIEW3D_MT_object.append(menu_func)  # Adds the new operator to an existing menu.

def unregister():
    bpy.utils.unregister_class(ObjectMoveX)


# This allows you to run the script directly from Blender's Text editor
# to test the add-on without having to install it.
if __name__ == "__main__":
    register()

Know how to run a script in Blender's Text editor.

Have an understanding of Python primitive types (integer, Boolean, string, list, tuple, dictionary, and set).

GHI Chú

Thay vì sử dụng

bl_info = {
    "name": "Move X Axis",
    "blender": (2, 80, 0),
    "category": "Object",
}

import bpy


class ObjectMoveX(bpy.types.Operator):
    """My Object Moving Script"""      # Use this as a tooltip for menu items and buttons.
    bl_idname = "object.move_x"        # Unique identifier for buttons and menu items to reference.
    bl_label = "Move X by One"         # Display name in the interface.
    bl_options = {'REGISTER', 'UNDO'}  # Enable undo for the operator.

    def execute(self, context):        # execute() is called when running the operator.

        # The original script
        scene = context.scene
        for obj in scene.objects:
            obj.location.x += 1.0

        return {'FINISHED'}            # Lets Blender know the operator finished successfully.

def menu_func(self, context):
    self.layout.operator(ObjectMoveX.bl_idname)

def register():
    bpy.utils.register_class(ObjectMoveX)
    bpy.types.VIEW3D_MT_object.append(menu_func)  # Adds the new operator to an existing menu.

def unregister():
    bpy.utils.unregister_class(ObjectMoveX)


# This allows you to run the script directly from Blender's Text editor
# to test the add-on without having to install it.
if __name__ == "__main__":
    register()
1, chúng tôi sử dụng đối số
bl_info = {
    "name": "Move X Axis",
    "blender": (2, 80, 0),
    "category": "Object",
}

import bpy


class ObjectMoveX(bpy.types.Operator):
    """My Object Moving Script"""      # Use this as a tooltip for menu items and buttons.
    bl_idname = "object.move_x"        # Unique identifier for buttons and menu items to reference.
    bl_label = "Move X by One"         # Display name in the interface.
    bl_options = {'REGISTER', 'UNDO'}  # Enable undo for the operator.

    def execute(self, context):        # execute() is called when running the operator.

        # The original script
        scene = context.scene
        for obj in scene.objects:
            obj.location.x += 1.0

        return {'FINISHED'}            # Lets Blender know the operator finished successfully.

def menu_func(self, context):
    self.layout.operator(ObjectMoveX.bl_idname)

def register():
    bpy.utils.register_class(ObjectMoveX)
    bpy.types.VIEW3D_MT_object.append(menu_func)  # Adds the new operator to an existing menu.

def unregister():
    bpy.utils.unregister_class(ObjectMoveX)


# This allows you to run the script directly from Blender's Text editor
# to test the add-on without having to install it.
if __name__ == "__main__":
    register()
2 được chuyển cho
import bpy

scene = bpy.context.scene
for obj in scene.objects:
    obj.location.x += 1.0
9. Trong hầu hết các trường hợp, đây sẽ giống nhau. Tuy nhiên, trong một số trường hợp, các nhà khai thác sẽ được thông qua một bối cảnh tùy chỉnh, vì vậy các tác giả tập lệnh nên thích đối số
bl_info = {
    "name": "Move X Axis",
    "blender": (2, 80, 0),
    "category": "Object",
}

import bpy


class ObjectMoveX(bpy.types.Operator):
    """My Object Moving Script"""      # Use this as a tooltip for menu items and buttons.
    bl_idname = "object.move_x"        # Unique identifier for buttons and menu items to reference.
    bl_label = "Move X by One"         # Display name in the interface.
    bl_options = {'REGISTER', 'UNDO'}  # Enable undo for the operator.

    def execute(self, context):        # execute() is called when running the operator.

        # The original script
        scene = context.scene
        for obj in scene.objects:
            obj.location.x += 1.0

        return {'FINISHED'}            # Lets Blender know the operator finished successfully.

def menu_func(self, context):
    self.layout.operator(ObjectMoveX.bl_idname)

def register():
    bpy.utils.register_class(ObjectMoveX)
    bpy.types.VIEW3D_MT_object.append(menu_func)  # Adds the new operator to an existing menu.

def unregister():
    bpy.utils.unregister_class(ObjectMoveX)


# This allows you to run the script directly from Blender's Text editor
# to test the add-on without having to install it.
if __name__ == "__main__":
    register()
4 được truyền cho các nhà khai thác.

Để kiểm tra tập lệnh, bạn có thể sao chép và dán nó vào trình chỉnh sửa văn bản của Blender và chạy nó. Điều này sẽ thực thi tập lệnh trực tiếp và gọi đăng ký ngay lập tức.

Tuy nhiên, chạy tập lệnh sẽ không di chuyển bất kỳ đối tượng nào. Đối với điều này, bạn cần thực hiện toán tử mới đăng ký.

Trình đơn tìm kiếm Thao Tác.

Mở menu tìm kiếm toán tử và nhập vào "Di chuyển X theo một" (

bl_info = {
    "name": "Move X Axis",
    "blender": (2, 80, 0),
    "category": "Object",
}

import bpy


class ObjectMoveX(bpy.types.Operator):
    """My Object Moving Script"""      # Use this as a tooltip for menu items and buttons.
    bl_idname = "object.move_x"        # Unique identifier for buttons and menu items to reference.
    bl_label = "Move X by One"         # Display name in the interface.
    bl_options = {'REGISTER', 'UNDO'}  # Enable undo for the operator.

    def execute(self, context):        # execute() is called when running the operator.

        # The original script
        scene = context.scene
        for obj in scene.objects:
            obj.location.x += 1.0

        return {'FINISHED'}            # Lets Blender know the operator finished successfully.

def menu_func(self, context):
    self.layout.operator(ObjectMoveX.bl_idname)

def register():
    bpy.utils.register_class(ObjectMoveX)
    bpy.types.VIEW3D_MT_object.append(menu_func)  # Adds the new operator to an existing menu.

def unregister():
    bpy.utils.unregister_class(ObjectMoveX)


# This allows you to run the script directly from Blender's Text editor
# to test the add-on without having to install it.
if __name__ == "__main__":
    register()
5), sau đó trả về.Operator Search menu and type in "Move X by One" (the
bl_info = {
    "name": "Move X Axis",
    "blender": (2, 80, 0),
    "category": "Object",
}

import bpy


class ObjectMoveX(bpy.types.Operator):
    """My Object Moving Script"""      # Use this as a tooltip for menu items and buttons.
    bl_idname = "object.move_x"        # Unique identifier for buttons and menu items to reference.
    bl_label = "Move X by One"         # Display name in the interface.
    bl_options = {'REGISTER', 'UNDO'}  # Enable undo for the operator.

    def execute(self, context):        # execute() is called when running the operator.

        # The original script
        scene = context.scene
        for obj in scene.objects:
            obj.location.x += 1.0

        return {'FINISHED'}            # Lets Blender know the operator finished successfully.

def menu_func(self, context):
    self.layout.operator(ObjectMoveX.bl_idname)

def register():
    bpy.utils.register_class(ObjectMoveX)
    bpy.types.VIEW3D_MT_object.append(menu_func)  # Adds the new operator to an existing menu.

def unregister():
    bpy.utils.unregister_class(ObjectMoveX)


# This allows you to run the script directly from Blender's Text editor
# to test the add-on without having to install it.
if __name__ == "__main__":
    register()
5), then Return.

Các đối tượng nên di chuyển như trước.

Giữ phần bổ trợ này mở trong Blender cho bước tiếp theo - cài đặt.

CÀi ĐặT TRÌNH

Khi bạn có tiện ích bổ sung của mình trong trình soạn thảo văn bản của Blender, bạn sẽ muốn cài đặt nó để nó có thể được bật trong các tùy chọn tải khi khởi động.

Mặc dù phần bổ trợ ở trên là một bài kiểm tra, dù sao đi qua các bước để bạn biết cách thực hiện sau này.

Để cài đặt văn bản Blender dưới dạng tiện ích bổ sung, trước tiên bạn sẽ phải lưu nó trên ổ đĩa. Cẩn thận để tuân theo các hạn chế đặt tên áp dụng cho các mô -đun Python và kết thúc bằng phần mở rộng

bl_info = {
    "name": "Move X Axis",
    "blender": (2, 80, 0),
    "category": "Object",
}

import bpy


class ObjectMoveX(bpy.types.Operator):
    """My Object Moving Script"""      # Use this as a tooltip for menu items and buttons.
    bl_idname = "object.move_x"        # Unique identifier for buttons and menu items to reference.
    bl_label = "Move X by One"         # Display name in the interface.
    bl_options = {'REGISTER', 'UNDO'}  # Enable undo for the operator.

    def execute(self, context):        # execute() is called when running the operator.

        # The original script
        scene = context.scene
        for obj in scene.objects:
            obj.location.x += 1.0

        return {'FINISHED'}            # Lets Blender know the operator finished successfully.

def menu_func(self, context):
    self.layout.operator(ObjectMoveX.bl_idname)

def register():
    bpy.utils.register_class(ObjectMoveX)
    bpy.types.VIEW3D_MT_object.append(menu_func)  # Adds the new operator to an existing menu.

def unregister():
    bpy.utils.unregister_class(ObjectMoveX)


# This allows you to run the script directly from Blender's Text editor
# to test the add-on without having to install it.
if __name__ == "__main__":
    register()
6.

Khi tập tin có trên ổ đĩa, bạn có thể cài đặt nó như bạn sẽ tải xuống trực tuyến.

Mở và chọn tệp.

Bây giờ tiện ích bổ sung sẽ được liệt kê và bạn có thể bật nó bằng cách nhấn hộp kiểm, nếu bạn muốn nó được bật khi khởi động lại, hãy nhấn Save dưới dạng mặc định. Toán tử có thể được chạy theo cùng một cách như được mô tả trong phần trước.previous section.

Khi bổ trợ được bật, Blender sẽ thực hiện mã và chạy chức năng

bl_info = {
    "name": "Move X Axis",
    "blender": (2, 80, 0),
    "category": "Object",
}

import bpy


class ObjectMoveX(bpy.types.Operator):
    """My Object Moving Script"""      # Use this as a tooltip for menu items and buttons.
    bl_idname = "object.move_x"        # Unique identifier for buttons and menu items to reference.
    bl_label = "Move X by One"         # Display name in the interface.
    bl_options = {'REGISTER', 'UNDO'}  # Enable undo for the operator.

    def execute(self, context):        # execute() is called when running the operator.

        # The original script
        scene = context.scene
        for obj in scene.objects:
            obj.location.x += 1.0

        return {'FINISHED'}            # Lets Blender know the operator finished successfully.

def menu_func(self, context):
    self.layout.operator(ObjectMoveX.bl_idname)

def register():
    bpy.utils.register_class(ObjectMoveX)
    bpy.types.VIEW3D_MT_object.append(menu_func)  # Adds the new operator to an existing menu.

def unregister():
    bpy.utils.unregister_class(ObjectMoveX)


# This allows you to run the script directly from Blender's Text editor
# to test the add-on without having to install it.
if __name__ == "__main__":
    register()
7. Khi tiện ích bổ sung bị tắt, Blender sẽ chạy chức năng
bl_info = {
    "name": "Move X Axis",
    "blender": (2, 80, 0),
    "category": "Object",
}

import bpy


class ObjectMoveX(bpy.types.Operator):
    """My Object Moving Script"""      # Use this as a tooltip for menu items and buttons.
    bl_idname = "object.move_x"        # Unique identifier for buttons and menu items to reference.
    bl_label = "Move X by One"         # Display name in the interface.
    bl_options = {'REGISTER', 'UNDO'}  # Enable undo for the operator.

    def execute(self, context):        # execute() is called when running the operator.

        # The original script
        scene = context.scene
        for obj in scene.objects:
            obj.location.x += 1.0

        return {'FINISHED'}            # Lets Blender know the operator finished successfully.

def menu_func(self, context):
    self.layout.operator(ObjectMoveX.bl_idname)

def register():
    bpy.utils.register_class(ObjectMoveX)
    bpy.types.VIEW3D_MT_object.append(menu_func)  # Adds the new operator to an existing menu.

def unregister():
    bpy.utils.unregister_class(ObjectMoveX)


# This allows you to run the script directly from Blender's Text editor
# to test the add-on without having to install it.
if __name__ == "__main__":
    register()
8.

GHI Chú

Điểm đến của tiện ích bổ sung phụ thuộc vào cấu hình máy xay của bạn. Khi cài đặt một tiện ích bổ sung, các đường dẫn đến nguồn và đích được in trong bảng điều khiển. Bạn cũng có thể tìm thấy các vị trí đường dẫn bổ trợ bằng cách chạy điều này trong bảng điều khiển Python:

import addon_utils
print(addon_utils.paths())

Thêm được viết về chủ đề này ở đây: Bố cục thư mục.Directory Layout.

Trình bổ Sung thứ hai của bạn (tiện ích bổ sung thứ hai của bạn) 

Đối với tiện ích bổ sung thứ hai của chúng tôi, chúng tôi sẽ tập trung vào Instance đối tượng-đây là-để tạo các bản sao được liên kết của một đối tượng theo cách tương tự như những gì bạn có thể thấy với bộ sửa đổi mảng.

Viết tập lệNH (viết tập lệnh) 

Như trước đây, đầu tiên chúng ta sẽ bắt đầu với một tập lệnh, phát triển nó, sau đó chuyển đổi nó thành một tiện ích bổ sung.

import bpy
from bpy import context

# Get the current scene
scene = context.scene

# Get the 3D cursor location
cursor = scene.cursor.location

# Get the active object (assume we have one)
obj = context.active_object

# Now make a copy of the object
obj_new = obj.copy()

# The new object has to be added to a collection in the scene
scene.collection.objects.link(obj_new)

# Now we can place the object
obj_new.location = cursor

Bây giờ hãy thử sao chép tập lệnh này vào máy xay và chạy nó trên khối mặc định. Hãy chắc chắn rằng bạn nhấp để di chuyển con trỏ 3D trước khi chạy vì bản sao sẽ xuất hiện tại vị trí của con trỏ.

Sau khi chạy, lưu ý rằng khi bạn chuyển sang chế độ chỉnh sửa để thay đổi khối - tất cả các bản sao thay đổi. Trong Blender, điều này được gọi là các bản sao liên kết.

Tiếp theo, chúng ta sẽ làm điều này trong một vòng lặp, để tạo một loạt các đối tượng giữa đối tượng hoạt động và con trỏ.

import bpy
from bpy import context

scene = context.scene
cursor = scene.cursor.location
obj = context.active_object

# Use a fixed value for now, eventually make this user adjustable
total = 10

# Add 'total' objects into the scene
for i in range(total):
    obj_new = obj.copy()
    scene.collection.objects.link(obj_new)

    # Now place the object in between the cursor
    # and the active object based on 'i'
    factor = i / total
    obj_new.location = (obj.location * factor) + (cursor * (1.0 - factor))

Hãy thử chạy tập lệnh này với đối tượng hoạt động và con trỏ cách nhau để xem kết quả.

Với tập lệnh này, bạn sẽ nhận thấy chúng tôi đang thực hiện một số toán học với vị trí đối tượng và con trỏ, điều này hoạt động vì cả hai đều là các phiên bản 3D ____29, một lớp thuận tiện được cung cấp bởi mô -đun

import addon_utils
print(addon_utils.paths())
0 cho phép các vectơ được nhân với các số và ma trận.

Nếu bạn quan tâm đến lĩnh vực này, hãy đọc thành

bl_info = {
    "name": "Move X Axis",
    "blender": (2, 80, 0),
    "category": "Object",
}

import bpy


class ObjectMoveX(bpy.types.Operator):
    """My Object Moving Script"""      # Use this as a tooltip for menu items and buttons.
    bl_idname = "object.move_x"        # Unique identifier for buttons and menu items to reference.
    bl_label = "Move X by One"         # Display name in the interface.
    bl_options = {'REGISTER', 'UNDO'}  # Enable undo for the operator.

    def execute(self, context):        # execute() is called when running the operator.

        # The original script
        scene = context.scene
        for obj in scene.objects:
            obj.location.x += 1.0

        return {'FINISHED'}            # Lets Blender know the operator finished successfully.

def menu_func(self, context):
    self.layout.operator(ObjectMoveX.bl_idname)

def register():
    bpy.utils.register_class(ObjectMoveX)
    bpy.types.VIEW3D_MT_object.append(menu_func)  # Adds the new operator to an existing menu.

def unregister():
    bpy.utils.unregister_class(ObjectMoveX)


# This allows you to run the script directly from Blender's Text editor
# to test the add-on without having to install it.
if __name__ == "__main__":
    register()
9-có nhiều chức năng tiện ích tiện dụng như có được góc giữa các vectơ, sản phẩm chéo, sản phẩm DOT cũng như các chức năng nâng cao hơn trong
import addon_utils
print(addon_utils.paths())
2 như nội suy spline của Bézier và đường ray hình tam giác .

Bây giờ chúng tôi sẽ tập trung vào việc biến tập lệnh này thành một tiện ích bổ sung, nhưng thật tốt khi biết rằng mô-đun toán 3D này có sẵn và có thể giúp bạn với chức năng nâng cao hơn sau này.

Viết tr là

Bước đầu tiên là chuyển đổi tập lệnh AS-IS thành một tiện ích bổ sung:

bl_info = {
    "name": "Cursor Array",
    "blender": (2, 80, 0),
    "category": "Object",
}

import bpy


class ObjectCursorArray(bpy.types.Operator):
    """Object Cursor Array"""
    bl_idname = "object.cursor_array"
    bl_label = "Cursor Array"
    bl_options = {'REGISTER', 'UNDO'}

    def execute(self, context):
        scene = context.scene
        cursor = scene.cursor.location
        obj = context.active_object

        total = 10

        for i in range(total):
            obj_new = obj.copy()
            scene.collection.objects.link(obj_new)

            factor = i / total
            obj_new.location = (obj.location * factor) + (cursor * (1.0 - factor))

        return {'FINISHED'}

def register():
    bpy.utils.register_class(ObjectCursorArray)


def unregister():
    bpy.utils.unregister_class(ObjectCursorArray)


if __name__ == "__main__":
    register()

Tất cả mọi thứ ở đây đã được đề cập trong các bước trước, bạn có thể muốn thử chạy bổ trợ vẫn còn và xem xét những gì có thể làm để làm cho nó hữu ích hơn.

Hai trong số những điều còn thiếu rõ ràng nhất là - có tổng số cố định ở mức 10 và phải truy cập toán tử với tìm kiếm (tìm kiếm) không thuận tiện lắm.Tìm Kiếm (Search) is not very convenient.

Cả hai bổ sung này được giải thích tiếp theo, với kịch bản cuối cùng sau đó.

TínH CHấT CủA Toán tử (Thuộc tính toán tử) 

Có nhiều loại thuộc tính được sử dụng cho cài đặt công cụ, các loại thuộc tính chung bao gồm: int, float, vector, màu, boolean và chuỗi.

Các thuộc tính này được xử lý khác nhau với các thuộc tính lớp Python điển hình vì máy xay sinh tố cần hiển thị chúng trong giao diện, lưu trữ cài đặt của chúng trong keymaps và tiếp tục cài đặt để tái sử dụng.

Mặc dù điều này được xử lý theo một cách khá pythonic, hãy chú ý rằng thực tế bạn đang xác định các cài đặt công cụ được tải vào máy xay và được truy cập bởi các bộ phận khác của máy xay sinh tố, bên ngoài Python.

Để loại bỏ theo nghĩa đen 10 cho

import addon_utils
print(addon_utils.paths())
3, chúng tôi sẽ sử dụng thuộc tính toán tử. Thuộc tính toán tử được xác định thông qua mô -đun BPY.props, điều này được thêm vào phần thân lớp:

# moved assignment from execute() to the body of the class...
total: bpy.props.IntProperty(name="Steps", default=2, min=1, max=100)

# and this is accessed on the class
# instance within the execute() function as...
self.total

Các thuộc tính này từ

import addon_utils
print(addon_utils.paths())
4 được xử lý đặc biệt bởi máy xay sinh tố khi lớp được đăng ký để chúng hiển thị dưới dạng các nút trong giao diện người dùng. Có nhiều đối số bạn có thể chuyển đến các thuộc tính để đặt giới hạn, thay đổi mặc định và hiển thị một chú giải công cụ.

Tài liệu này không đi vào chi tiết về việc sử dụng các loại thuộc tính khác. Tuy nhiên, liên kết trên bao gồm các ví dụ về việc sử dụng tài sản tiên tiến hơn.

Bố trí bàn phím (keymap) 

Trong Blender, các tiện ích bổ sung có keymaps riêng để không can thiệp vào keymaps tích hợp của Blender.

Trong ví dụ dưới đây, chế độ đối tượng mới

import addon_utils
print(addon_utils.paths())
5 được thêm vào, sau đó
import addon_utils
print(addon_utils.paths())
6 được thêm vào keymap tham chiếu toán tử mới được thêm vào của chúng tôi, sử dụng Shift-CTRL-T làm phím tắt để kích hoạt nó.

# store keymaps here to access after registration
addon_keymaps = []

def register():

    # handle the keymap
    wm = bpy.context.window_manager
    km = wm.keyconfigs.addon.keymaps.new(name='Object Mode', space_type='EMPTY')

    kmi = km.keymap_items.new(ObjectCursorArray.bl_idname, 'T', 'PRESS', ctrl=True, shift=True)
    kmi.properties.total = 4

    addon_keymaps.append((km, kmi))


def unregister():

    # handle the keymap
    for km, kmi in addon_keymaps:
        km.keymap_items.remove(kmi)
    addon_keymaps.clear()

Lưu ý cách mục KEYMAP có thể có cài đặt

import addon_utils
print(addon_utils.paths())
3 khác với bộ mặc định của toán tử, điều này cho phép bạn có nhiều khóa truy cập vào cùng một toán tử với các cài đặt khác nhau.

GHI Chú

Mặc dù Shift-CTRL-T không phải là phím tắt máy xay mặc định, nhưng thật khó để đảm bảo các tiện ích bổ sung sẽ không ghi đè lên keymaps của nhau. Do đó, ít nhất hãy cẩn thận khi gán các khóa mà chúng không xung đột với chức năng quan trọng của máy xay sinh tố (xem thêm bố trí phím chưa sử dụng đến phải khônBố Trí Phím Chưa Sử Dụng Đến Phải Không (Is Key Free)).

Đối với tài liệu API trên các chức năng được liệt kê ở trên, xem:

  • import addon_utils
    print(addon_utils.paths())
    
    8

  • import addon_utils
    print(addon_utils.paths())
    
    5

  • import bpy
    from bpy import context
    
    # Get the current scene
    scene = context.scene
    
    # Get the 3D cursor location
    cursor = scene.cursor.location
    
    # Get the active object (assume we have one)
    obj = context.active_object
    
    # Now make a copy of the object
    obj_new = obj.copy()
    
    # The new object has to be added to a collection in the scene
    scene.collection.objects.link(obj_new)
    
    # Now we can place the object
    obj_new.location = cursor
    
    0

  • import addon_utils
    print(addon_utils.paths())
    
    6

Tổng hợp toào bộ (mang tất cả lại với nhau) 

bl_info = {
    "name": "Cursor Array",
    "blender": (2, 80, 0),
    "category": "Object",
}

import bpy


class ObjectCursorArray(bpy.types.Operator):
    """Object Cursor Array"""
    bl_idname = "object.cursor_array"
    bl_label = "Cursor Array"
    bl_options = {'REGISTER', 'UNDO'}

    total: bpy.props.IntProperty(name="Steps", default=2, min=1, max=100)

    def execute(self, context):
        scene = context.scene
        cursor = scene.cursor.location
        obj = context.active_object

        for i in range(self.total):
            obj_new = obj.copy()
            scene.collection.objects.link(obj_new)

            factor = i / self.total
            obj_new.location = (obj.location * factor) + (cursor * (1.0 - factor))

        return {'FINISHED'}


def menu_func(self, context):
    self.layout.operator(ObjectCursorArray.bl_idname)

# store keymaps here to access after registration
addon_keymaps = []


def register():
    bpy.utils.register_class(ObjectCursorArray)
    bpy.types.VIEW3D_MT_object.append(menu_func)

    # handle the keymap
    wm = bpy.context.window_manager
    # Note that in background mode (no GUI available), keyconfigs are not available either,
    # so we have to check this to avoid nasty errors in background case.
    kc = wm.keyconfigs.addon
    if kc:
        km = wm.keyconfigs.addon.keymaps.new(name='Object Mode', space_type='EMPTY')
        kmi = km.keymap_items.new(ObjectCursorArray.bl_idname, 'T', 'PRESS', ctrl=True, shift=True)
        kmi.properties.total = 4
        addon_keymaps.append((km, kmi))

def unregister():
    # Note: when unregistering, it's usually good practice to do it in reverse order you registered.
    # Can avoid strange issues like keymap still referring to operators already unregistered...
    # handle the keymap
    for km, kmi in addon_keymaps:
        km.keymap_items.remove(kmi)
    addon_keymaps.clear()

    bpy.utils.unregister_class(ObjectCursorArray)
    bpy.types.VIEW3D_MT_object.remove(menu_func)


if __name__ == "__main__":
    register()

Trong trình Đơn.

Chạy tập lệnh (hoặc lưu nó và thêm nó thông qua các tùy chọn như trước) và nó sẽ xuất hiện trong menu đối tượng.

Tính Chất CủA Nhà điều hành.

Sau khi chọn nó từ menu, bạn có thể chọn có bao nhiêu phiên bản của khối lập phương bạn muốn tạo.

GHI Chú

Mặc dù Shift-CTRL-T không phải là phím tắt máy xay mặc định, nhưng thật khó để đảm bảo các tiện ích bổ sung sẽ không ghi đè lên keymaps của nhau. Do đó, ít nhất hãy cẩn thận khi gán các khóa mà chúng không xung đột với chức năng quan trọng của máy xay sinh tố (xem thêm bố trí phím chưa sử dụng đến phải khôn

Đối với tài liệu API trên các chức năng được liệt kê ở trên, xem:

Tổng hợp toào bộ (mang tất cả lại với nhau) 

Trong trình Đơn.

Chạy tập lệnh (hoặc lưu nó và thêm nó thông qua các tùy chọn như trước) và nó sẽ xuất hiện trong menu đối tượng.

Tính Chất CủA Nhà điều hành.

Sau khi chọn nó từ menu, bạn có thể chọn có bao nhiêu phiên bản của khối lập phương bạn muốn tạo.

Trực tiếp thực hiện tập lệnh nhiều lần cũng sẽ thêm menu mỗi lần. Mặc dù không có hành vi hữu ích, không có gì phải lo lắng vì các tiện ích bổ sung sẽ không đăng ký nhiều lần khi được bật thông qua các tùy chọn.

  • Kết luận (kết luận) 

  • Các tiện ích bổ sung có thể gói gọn chức năng nhất định một cách gọn gàng cho các công cụ viết để cải thiện quy trình làm việc của bạn hoặc cho các tiện ích viết cho người khác sử dụng.

  • Mặc dù có những giới hạn đối với những gì Python có thể làm trong Blender, nhưng chắc chắn có rất nhiều điều có thể đạt được mà không cần phải đi sâu vào mã C/C ++ của Blender.

  • Ví dụ được đưa ra trong hướng dẫn bị hạn chế, nhưng hiển thị API máy xay được sử dụng cho các tác vụ phổ biến mà bạn có thể mở rộng để viết các công cụ của riêng mình.