blender-python learning

import bpy
bpy.ops.mesh.primitive_cube_add() #添加立方体

ops indicates the code of the operation command, such as adding some commands in the following list
bpy.context
bpy.data
"." indicates that
the attributes in each level of brackets are the attributes of the cube, and it is also possible to clear the attributes inside. If you do not know the attributes, you can command Line input bpy.ops.mesh.primitive_cube_add(, then click the auto-completion of the console.
for i in range(7):
print(iy
bpy.context.selected_objects[i].name = “g”

import bpy
bpy.ops.object.align(align_mode='OPT_1', relative_to='OPT_1',align_axis={'Z'})

placed on the ground

import bpy
class fengfengToools(bpy.types.Operator):
    bl_idname = 'obj.dimian' #字符串不能有大写字母
    bl_label = '地面对齐‘
    # 编辑器不能写入中文,得建立txt文档 输入中文 ,然后复制粘贴进去。
    
    def execute(self,context):
        bpy.ops.object.align(align_mode='OPT_1', relative_to='OPT_1',align_axis={'Z'})
        return{'FINISHED'}
bpy.utils.register_class(fengfengToools)

Then open the menu preferences, the first "Interface" in it, find and check "Development Options", then run the script, click on the object, press F3, come out the search box, search for "ground alignment" and click, then place it on the ground.

Script -> Turn into a plugin

The naming is one-time, how to automatically load the script -> become a plug-in .

bl_info = {
    "name": "地面对齐",
    "author": "kidominox",
    "version": (1, 0),
    "blender": (3, 0, 0),
    "location": "F3搜索这个命令",
    "description": "物体对齐到地面上",
    "warning": "",
    "doc_url": "",
    "category": "Object",
}
# 
import bpy
class fengfengToools(bpy.types.Operator):
    bl_idname = "obj.dimian"
    bl_label = "地面对齐"
    
    def execute(self,context):
        bpy.ops.object.align(align_mode='OPT_1',relative_to='OPT_1',align_axis={'Z'})
        return{'FINISHED'}

def register():
    bpy.utils.register_class(fengfengToools)
    
def unregister():
    bpy.utils.unregister_class(fengfengToools)
if __name__ == "__main__":
    register()

script into button

bl_info = {
    "name": "地面对齐",
    "author": "kidominox",
    "version": (1, 0),
    "blender": (3, 0, 0),
    "location": "F3搜索这个命令",
    "description": "物体对齐到地面上",
    "warning": "",
    "doc_url": "",
    "category": "Object",
}
# 
import bpy
class fengfengToools(bpy.types.Operator):
    bl_idname = "obj.dimian"
    bl_label = "地面对齐"
    
    def execute(self,context):
        bpy.ops.object.align(align_mode='OPT_1',relative_to='OPT_1',align_axis={'Z'})
        return{'FINISHED'}

class kidominoxTools(bpy.types.Header):
    
    bl_space_type = 'INFO' # indicate
    
    def draw(self,context):
        self.layout.operator("obj.dimian")
        self.layout.operator("mesh.primitive_cube_add")
        #在命令中是bpy.ops.mesh.primitive_cube_add() 而mesh.primitive_cube_add是他的id
    
    
    
def register():
    bpy.utils.register_class(fengfengToools)
    bpy.utils.register_class(kidominoxTools)
    
def unregister():
    bpy.utils.unregister_class(fengfengToools)
    bpy.utils.unregister_class(kidominoxTools)

if __name__ == "__main__":
    register()

The main version of up is 2.83, but my own version 3.0.0 can't get the button.
Put the button above:

class kidominoxTools(bpy.types.Header):
    
    bl_space_type = 'INFO' # indicate
    bl_space_type = 'TOPBAR'
    
    def draw(self,context):
    if context.region.alignment == 'RIGHT':
            self.layout.operator("obj.dimian")
            self.layout.operator("mesh.primitive_cube_add")

Quickly set commands to buttons on the interface

insert image description here
First run the command you want to turn it into an id, for example, after running it is bpy.ops.mesh.extrude_region_move(), so the id is mesh.extrude_region_move, just put it in the code.
If the button is cleaned up, then: insert image description here
study from blender-python teaching

Guess you like

Origin blog.csdn.net/Carol_learning/article/details/121902673