利用者:Tommy3001/Extensions:2.5/Py/Scripts/Templates/Addon add object

提供: wiki
移動先: 案内検索
bl_addon_info = {
    "name": "New Object",
    "author": "YourNameHere",
    "version": (1, 0),
    "blender": (2, 5, 5),
    "api": 33333,
    "location": "View3D > Add > Mesh > New Object",
    "description": "Adds a new Mesh Object",
    "warning": "",
    "wiki_url": "",
    "tracker_url": "",
    "category": "Add Mesh"}


import bpy
from bpy.props import FloatVectorProperty
from add_utils import AddObjectHelper, add_object_data
from mathutils import Vector


def add_object(self, context):
    scale_x = self.scale.x
    scale_y = self.scale.y

    verts = [Vector((-1 * scale_x,  1 * scale_y, 0)),
             Vector(( 1 * scale_x,  1 * scale_y, 0)),
             Vector(( 1 * scale_x, -1 * scale_y, 0)),
             Vector((-1 * scale_x, -1 * scale_y, 0)),]
    edges = []
    faces = [[0,1,2,3]]

    mesh_data = bpy.data.meshes.new(name='New Object Mesh')
    mesh_data.from_pydata(verts, edges, faces)
    add_object_data(context, mesh_data, operator=self)


class OBJECT_OT_add_object(bpy.types.Operator, AddObjectHelper):
    """Add a Mesh Object"""
    bl_idname = "mesh.add_object"
    bl_label = "Add Mesh Object"
    bl_description = "Create a new Mesh Object"
    bl_options = {'REGISTER', 'UNDO'}

    scale = FloatVectorProperty(name='scale',
                                default=(1,1,1),
                                subtype='TRANSLATION',
                                description='scaling')

    def execute(self, context):
 
        add_object(self, context)
 
        return {'FINISHED'}


#### REGISTER ####

def add_object_button(self, context):
    self.layout.operator(
        OBJECT_OT_add_object.bl_idname,
        text="Add Object",
        icon="PLUGIN")

def register():
    bpy.types.INFO_MT_mesh_add.append(add_object_button)

def unregister():
    bpy.types.INFO_MT_mesh_add.remove(add_object_button)


if __name__ == '__main__':
    register()


Description

Script meta informations

bl_addon_info = {
    "name": "New Object",
    "author": "YourNameHere",
    "version": (1, 0),
    "blender": (2, 5, 5),
    "api": 33333,
    "location": "View3D > Add > Mesh > New Object",
    "description": "Adds a new Mesh Object",
    "warning": "",
    "wiki_url": "",
    "tracker_url": "",
    "category": "Add Mesh"}

name

(string)
Name of the script. This will be displayed in the add-ons menu as the main entry.

description

(string)
This short text helps the user to decide if he needs the addon when he reads the addons list. Any further help text should be put in the help page (see wiki_url).

author

(string)
Author name(s).

version

(tuple of integers)
Script version. Please put any important note into the warning field, don't use text in the version number.

blender

(tuple of integers)
The minimum Blender version required to run the script. In the add-ons panel this is used to check if the user has a Blender version that is new enough to enable the script.

api

(integer)
This is the Blender API SVN revision the script is compatible with.

location

(string)
Explains where the new functionality can be found. For example: "View3D > Properties > Measure"

warning

(string)
Used for warning icon and text in addons panel. If this is empty you will see nothing; if it's non-empty, you will see a warning icon and the text you put here alerts the user about a bug or a problem to be aware of

wiki_url

(string)
Link to the wiki page of the script: here you should put the script manual and useful external links (not allowed anymore from 2.5x).
Note: this url is mandatory to be accepted in bf-extensions's svn (see below)

tracker_url

(string)
Link to the extensions project-page of the script. It shows up as a bug-report button, so users can easily report any bugs they encounter.
Note: this url is mandatory to be accepted in bf-extensions's svn (see below)

category

(string)
Defines the group to which the script belongs. This is used for filtering in the add-ons panel. A list of categories currently used in the extensions project can be found below. Please pay attention to the correct spelling and capitalization of them.
  • 3D View
  • Add Mesh
  • Add Curve
  • Animation
  • Compositing
  • Game Engine
  • Import/Export
  • Lighting
  • Material
  • Mesh
  • Object
  • Physics
  • Pyconstraint
  • Pydriver
  • Pynode
  • Render
  • System
  • Text Editor

Import Modules

import bpy
from bpy.props import FloatVectorProperty
from add_utils import AddObjectHelper, add_object_data
from mathutils import Vector

bpy

Bpy is the top level Blender Python API module.

bpy.props

This module defines properties to extend blenders internal data, the result of these functions is used to assign properties

to classes registered with blender and can’t be used directly. FloatVectorProperty returns a new vector float property definition.

add_utils