0
$\begingroup$

I'm new to python and blender scripting. I'm making an operator addon that will operate on a specific face. i want to create something like the 'lock to object' element in the view panel.

Firstly how do i make a selection box?

Secondly, how do i make is so it can select only faces?

Thirdly, how do i make it so it is limited to selecting one face?

I've tried searching around but can't find much on it at all, so any links will be much appeciated as iknow it's a loaded question.

$\endgroup$
  • $\begingroup$ What's the reason behind the Add-on you're creating? Why should the user only be able to select a single face? What will he do with it? What is the rule / logic? Before we jump to conclusions, please let us know what to strive for. $\endgroup$ – aliasguru Dec 7 '17 at 20:11
  • $\begingroup$ I'm trying to automate dovetail creation for furniture design, so it needs to specify the face on which to apply. So far i have all the settings to create the regions, which will then be translated from the x axis to the long edge of the selected face, perpendicular to the face, to then extrude and boolean difference. $\endgroup$ – Michael White Dec 7 '17 at 21:47
0
$\begingroup$

Sounds like you are trying to do too much at once, but see if this will help to break down the logic and possibly clarify a more specific question.

import bpy
import os
import bmesh
from bpy.types import Operator
from bpy.types import Panel

bl_info = {
    "name": "Blank1",
    "author": "Your Name Here",
    "version": (1, 0),
    "blender": (2, 79, 0),
    "location": "View3D > Tools panel 'T' > Blank1 > Blank2",
    "description": "",
    "warning": "",
    "wiki_url": "",
    "category": "",
    }


def main(context):
    if context.active_object.mode == 'OBJECT':
        print("object mode")
        print("Enumerated list selection")
        print("object list :", context.scene.enum_list1)
        print("Face list :", context.scene.enum_list2)
    if context.active_object.mode == 'EDIT':
        print("edit mode")
        print("selection box : ", context.scene.objects.active.name)
        print("index of faces selected : ", get_index_selected())


class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

    @classmethod
    def poll(cls, context):
        return context.active_object is not None

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


def enum_list_from_all(self, context):
    """EnumProperty callback"""
    enum_items = []
    objs = context.scene.objects
    if objs is None:
        return enum_items
    count = 0
    for obj in objs:
        if obj.type not in ['MESH']:
            continue
        identifier = obj.name
        name = obj.name
        description = obj.name
        number = count
        enum_items.append((identifier, name, description, number))
        count += 1
    return enum_items


def enum_list_from_selected(self, context):
    """EnumProperty callback"""
    enum_items = []
    for obj in bpy.data.objects:
        if obj.name == context.scene.enum_list1:
            sel_obj = obj
    for poly in sel_obj.data.polygons:
        identifier = ((sel_obj.name) + " F: " + str(poly.index))
        name = ((sel_obj.name) + " F: " + str(poly.index))
        description = ("face index" + str(poly.index))
        number = poly.index
        enum_items.append((identifier, name, description, number))
    return enum_items


def get_index_selected():
    obj = bpy.context.edit_object
    me = obj.data
    bm = bmesh.from_edit_mesh(me)
    mystr = ""
    for f in bm.faces:
        if f.select:
            mystr += (str(f.index) + ' ')
    return mystr


def initprop():
    bpy.types.Scene.enum_list1 = bpy.props.EnumProperty(
        items=enum_list_from_all,
        name="enum_list1",
        description="",
        )
    bpy.types.Scene.enum_list2 = bpy.props.EnumProperty(
        items=enum_list_from_selected,
        name="enum_list2",
        description="",
        )


def delprop():
    del bpy.types.Scene.enum_list1
    del bpy.types.Scene.enum_list2


class VIEW3D_PT_Blank1_Blank2(Panel):
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'
    bl_category = "Blank1"
    bl_label = "Blank2"

    def draw(self, context):
        scene = context.scene
        layout = self.layout
        row = layout.row()
        col = layout.column(align=True)
        col.label(text="Enum lists based on blend file data")
        col.prop(scene, "enum_list1", text="Objects")
        col.prop(scene, "enum_list2", text="Faces")
        if context.active_object.mode == 'OBJECT':
            col.operator(SimpleOperator.bl_idname, "test button")
        col.separator()
        col.label(text="Alternative")
        col.label(text="Active object is displayed or set from box")
        col.prop(scene.objects, "active", text="Active Object")
        if context.active_object.mode == 'OBJECT':
            col.label(text="press tab to enter edit mode")
        if context.active_object.mode == 'EDIT':
            col.label(text='Index of Faces ' + get_index_selected())
            col.operator(SimpleOperator.bl_idname, "test button")


def register():
    bpy.utils.register_class(VIEW3D_PT_Blank1_Blank2)
    bpy.utils.register_class(SimpleOperator)
    initprop()


def unregister():
    bpy.utils.unregister_class(VIEW3D_PT_Blank1_Blank2)
    bpy.utils.unregister_class(SimpleOperator)
    delprop()


if __name__ == "__main__":
    register()
$\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.