0
$\begingroup$

I would like to write an Addon for blender 2.80 with python 3.7 but I can't find any tutorial or other material for learning how to do. I have used the template in blender for creating my base Addon and it work but now I want to add option panel with 2 o 3 input text and 2 or 3 button for the option. Can anyone help me please?

With the documentation that you give me I wrote this code:

bl_info = {
    "name": "Simple Add-on Template",
    "author": "Marco Mameli",
    "location": "View3D > Add > Mesh > Generate",
    "version": (1, 0, 0),
    "blender": (2, 80, 0),
    "description": "Starting point for new add-ons.",
    "category": "Add Mesh"
    }
import bpy
import bmesh
from bpy.types import Operator
from bpy.types import Panel

def add_pointcloud(self, context, naming):
    # qui scrivo i miei calcoli
    obj = context.active_object
    mycollection = bpy.data.collections.new("MyPointCloudCollection")
    bpy.context.scene.collection.children.link(mycollection)
    me = obj.data
    bm = bmesh.new()
    bm.from_mesh(me)
    bmFaces = []

    for face in bm.faces:
        faceLocation = face.calc_center_median()
        print(faceLocation)
        bmFaces.append(obj.matrix_world @ faceLocation) # la @ è il prodotto vettoriale
    for vertex in bm.verts:
        print(vertex.co)
        bmFaces.append(obj.matrix_world @ vertex.co)
    me = bpy.data.meshes.new(obj.name + 'Mesh' + naming)
    ob = bpy.data.objects.new(obj.name + '_PointCloud_' + naming, me)
    ob.show_name = True
    bpy.data.collections['MyPointCloudCollection'].objects.link(ob)
    me.from_pydata(bmFaces, [], [])
    me.update()
    ob.select_set(True)

class OBJECT_OT_add_PointCloud_with_noise(Operator):
    """ Create a Point Cloud """
    bl_idname = "object.add_pointcloud_with_noise"
    bl_label = "Add Mesh object that represent a Point Cloud"
    bl_options = {'REGISTER', 'UNDO'}

    def execute(self,context): # operazioni da eseguire
        # MAI METTERE IL CODICE QUI DENTRO DIRETTAMENTE
        print("Sono in with noise")
        add_pointcloud(self, context, "with_noise")

        return {'FINISHED'}

class OBJECT_OT_add_PointCloud(Operator):
    """ Create a Point Cloud """
    bl_idname = "object.add_mesh_pointcloud"
    bl_label = "Add Mesh object that represent a Point Cloud"
    bl_options = {'REGISTER', 'UNDO'}

    def execute(self,context): # operazioni da eseguire
        # MAI METTERE IL CODICE QUI DENTRO DIRETTAMENTE
        print("sono in no noise")
        add_pointcloud(self, context, "no_noise")

        return {'FINISHED'}

    def draw(self,context):
        layout = self.layout

        scene = context.scene

        layout.label(text="Pointcloud option")

        row = layout.row()
        row.prop(scene, "frame_star")

        layout.label(text="Big Button:")
        row = layout.row()
        row.scale_y = 1.0
        row.operator("object.add_pointcloud_with_noise")

# creo il bottone da aggiungere al menu di blender
def add_pointcloud_button(self, context):
        self.layout.operator(OBJECT_OT_add_PointCloud.bl_idname, text="Generate Point Cloud",
        icon='PLUGIN')

# creo il link al manuale
def add_pointcloud_manual_map():
        url_manual_prefix=""
        url_manual_mapping = (("bpy.ops.mesh.add_pointcloud", "editors/edview/object"),)
        return url_manual_prefix, url_manual_mapping

# Options panel for addon
class OBJECT_PT_add_PointCloud_properties(Panel):
    bl_label = "Properties Layout"
    bl_idname = "SCENE_PT_layout_PointCloud_properties"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "scene"

    def draw(self,context):
        layout = self.layout

        scene = context.scene

        layout.label(text="Pointcloud option")

        row = layout.row()
        row.prop(scene, "frame_star")

        layout.label(text="Big Button:")
        row = layout.row()
        row.scale_y = 3.0
        row.operator("render.render")


classes = (OBJECT_OT_add_PointCloud, OBJECT_PT_add_PointCloud_properties, OBJECT_OT_add_PointCloud_with_noise)      
def register():
    for cls in classes:
        bpy.utils.register_class(cls)
    bpy.utils.register_manual_map(add_pointcloud_manual_map)
    bpy.types.VIEW3D_MT_mesh_add.append(add_pointcloud_button) 


def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)
    bpy.utils.register_manual_map(add_pointcloud_manual_map)
    bpy.types.VIEW3D_MT_mesh_add.remove(add_pointcloud_button)


if __name__ == "__main__":
    register()

When I run the script the Addon is created correctly but not work how I want. In fact the panel is not added if I do not insert draw function in my operator and the first operator is not recognized. How can I fix this?

$\endgroup$
0
$\begingroup$

In order to create 2.8 addons it seems that there must be a prior use and understanding of 2.7+ python API.

Although you can always read the documentation for the conventions and the 2.8 addons structure and follow any older tutorial. AFAIK operators and most of the core stuff remains the same as many other new things have been introduced, you might be careful with add-on s registration and manipulation, but beside that this should work for now.

check also this:

https://wiki.blender.org/wiki/Reference/Release_Notes/2.80/Python_API

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.