Skip to content

Instantly share code, notes, and snippets.

@squarednob
Last active March 3, 2016 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save squarednob/cce7e00689df9de728c6 to your computer and use it in GitHub Desktop.
Save squarednob/cce7e00689df9de728c6 to your computer and use it in GitHub Desktop.
import bpy
# Meta information.
bl_info = {
"name": "Addon-template1: Add IcoSphere",
"author": "Gappy",
"version": (1, 0),
"blender": (2, 75, 0),
"location": "Object > Ball",
"description": "Add IcoSphere",
"warning": "",
"support": "TESTING",
"wiki_url": "",
"tracker_url": "",
"category": "Object"
}
# Operation.
class CreateObject(bpy.types.Operator):
bl_idname = "object.create_object" # Access the class by bpy.ops.object.create_object.
bl_label = "Ball"
bl_description = "Add IcoSphere"
bl_options = {'REGISTER', 'UNDO'}
# Input property-------------------
# xyz vector values.:
float_vector_input = bpy.props.FloatVectorProperty(
name = "Float Vector",
description = "Set float vector",
default = [2.0, 3.2, 4.4],
min = 1.0,
max = 10.0,
subtype = "XYZ"
)
# File selection.:
string_input = bpy.props.StringProperty(
name = "String",
description = "Set string",
default = "testfile.csv",
subtype = "FILE_PATH" # Set as file selection.
)
# Select box.
select_box = bpy.props.EnumProperty(
name = "Select box",
description = "Set select box",
# [(ID, name, description)]
items = [('3D_CURSOR', "3D cursor", "Locate on 3D cursor"),
('ORIGIN', "origin", "Locate on origin")]
)
# A check box.
check_box = bpy.props.BoolProperty(
name = "Check box",
description = "Set check box"
)
# xyz check box.
xyz_check_box = bpy.props.BoolVectorProperty(
name = "XYZ check box",
description = "Set XYZ check box",
default = [True, True, False],
subtype = "XYZ" # Set xyz check box
)
# -----------------------------------------
# Execute function.
def execute(self, context):
bpy.ops.mesh.primitive_ico_sphere_add()
active_obj = context.active_object
# Input before execute on the left tool shelf.
active_obj.scale = active_obj.scale * context.scene.float_input
# Input after execute on the left below panel.
active_obj.location = active_obj.location + self.float_vector_input #
print("Added ball in 3D view.")
return {'FINISHED'}
# Menu setting.
class CreateObjectPanel(bpy.types.Panel):
bl_label = "CREATE Object"
bl_idname = "create_object" # class ID.
bl_space_type = "VIEW_3D"
bl_region_type = "TOOLS" # Put the menu on the left tool shelf.
bl_category = "Tools" # Tab name of the tool shelf.
bl_context = (("objectmode"))
# Menu and input:
def draw(self, context):
obj = context.object
scene = context.scene
layout = self.layout
row = layout.row()
row.label("Row menu")
row.prop(scene, "float_input") # Input button for bpy.types.Scene.float_input.
box = layout.box()
box.label("Box menu")
box.operator("object.select_all").action = 'TOGGLE' # Select all button.
box.operator("object.select_random") # Random select button.
# Execute button for CreateObject.
layout.operator(CreateObject.bl_idname)
def register():
bpy.utils.register_module(__name__)
# bpy.types.Scene~ = To show the input in the left tool shelf, store "bpy.props.~".
# In draw() in the subclass of Panel, access the input value by "context.scene".
# In execute() in the class, access the input value by "context.scene.float_input".
bpy.types.Scene.float_input = bpy.props.FloatProperty(
name = "Float",
description = "Set float",
default = 2.0,
min = 1.0,
max = 10.0
)
print("This add-on was activated.")
def unregister():
del bpy.types.Scene.float_input
bpy.utils.unregister_module(__name__)
print("This add-on was deactivated.")
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment