ok…fixed the code. works in bmesh now. still not sure how to do the rotation?
##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
bl_info = {
"name": "Branch Extrusion",
"author": "Rebellion",
"version": (1, 0),
"blender": (2, 6, 2),
"location": "View3D > Object_Edit > Mesh Tools ",
"description": "Extrude and Resize the face",
"warning": "",
"wiki_url": "",
"tracker_url": "",
"category": "Mesh"}
#set the next face to the set distance (min distance)
#set the taper value = difference between the first and next face
##########
# import
##########
import bpy, mathutils
import bmesh
from bpy.props import IntProperty, FloatProperty
from bpy_extras.view3d_utils import region_2d_to_vector_3d, region_2d_to_location_3d
def extrusion(coord,region,rv3d,vec,loc,face,face_center,taper_value, cons_x,cons_y,cons_z,orientation):
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.mode_set(mode='EDIT')
# extrude face(s)
bpy.ops.mesh.extrude_region_move(MESH_OT_extrude_region={"mirror":False}, TRANSFORM_OT_translate={"value":(loc), "constraint_axis":(cons_x, cons_y, cons_z), "constraint_orientation": orientation})
bpy.ops.transform.translate(value =(-face_center ), constraint_axis=(cons_x, cons_y, cons_z))
bpy.ops.transform.resize(value = (taper_value, taper_value, taper_value)) # to set manually
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.mode_set(mode='EDIT')
class BranchExtrusionOperator(bpy.types.Operator):
bl_idname = "object.branch_extrusion"
bl_label = "Branch Extrusion"
bl_options = {'REGISTER', 'UNDO'}
bpy.types.Scene.min_distance = FloatProperty(name='Min Distance', min=0, max=999, soft_min=0, \
soft_max=20, default= 5, description='Minimun distance from a face to another')
bpy.types.Scene.taper_value = bpy.props.FloatProperty(name= 'Taper Value', min=0, max=999, soft_min=0.001, \
soft_max=2, default= 0.5, description='Difference in size from a face to another')
bpy.types.Scene.cons_x = bpy.props.BoolProperty(name= 'X Axis', default = False)
bpy.types.Scene.cons_y = bpy.props.BoolProperty(name= 'Y Axis', default = False)
bpy.types.Scene.cons_z = bpy.props.BoolProperty(name= 'Z Axis', default = False)
bpy.types.Scene.orientation = bpy.props.EnumProperty(items = [( 'GLOBAL','Global','Global'), ('LOCAL','Local','Normal'), ('NORMAL','Normal','Local')],name = "")
# create scene variables
bpy.context.scene.min_distance = 5
bpy.context.scene.taper_value = 0.5
bpy.context.scene.cons_x = False
bpy.context.scene.cons_y = False
bpy.context.scene.cons_z = False
bpy.context.scene['orientation'] = 1
LMB_Hold = False
def modal(self, context, event):
if event.type == 'MOUSEMOVE' and self.LMB_Hold:
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.mode_set(mode='EDIT')
# create variables
coord = event.mouse_region_x, event.mouse_region_y
region = context.region
rv3d = context.space_data.region_3d
vec = region_2d_to_vector_3d(region, rv3d, coord)
loc = (region_2d_to_location_3d(region, rv3d, coord, vec))
print (loc/30)
# get face center
bpy.ops.object.mode_set(mode='OBJECT')
me = bpy.context.object.data # Get the active mesh
bm = bmesh.new() # create an empty BMesh
bm.from_mesh(me) # fill it in from a Mesh
face = bpy.context.object.data.faces.active
face_center = bm.faces[face].calc_center_bounds()
bpy.ops.object.mode_set(mode='EDIT')
increment = round( mathutils.Vector((loc - face_center)).length, 0)
scn = context.scene
if increment >= scn['min_distance']:
extrusion(coord,region,rv3d,vec,loc,face,face_center,scn['taper_value'],scn.cons_x ,scn.cons_y,scn.cons_z,scn.orientation)
elif event.type == 'LEFTMOUSE':
if event.value == 'PRESS':
self.LMB_Hold = True
elif event.value == 'RELEASE':
self.LMB_Hold = False
elif event.type in {'RIGHTMOUSE', 'NUMPAD_ENTER'}:
return {'FINISHED'}
return {'RUNNING_MODAL'}
def invoke(self, context, event):
if context.object:
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
else:
self.report({'WARNING'}, "No active object, could not finish")
return {'CANCELLED'}
class UI (bpy.types.Panel):
bl_label = 'Branch Extrusion'
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
bl_context = "mesh_edit"
def draw(self, context):
scn = context.scene
layout = self.layout
column = layout.column(align=True)
column.operator('object.branch_extrusion')
column.label(text='Extrusion set:')
column.prop(scn,'min_distance' , slider=True)
column.prop(scn, 'taper_value', slider=True)
column.label(text='Constraint_axis')
column.prop(scn,'cons_x')
column.prop(scn,'cons_y')
column.prop(scn,'cons_z')
column.label(text='ORIENTATION')
column.prop(scn,'orientation')
def Menu_append(self, context):
self.layout.separator()
self.layout.operator(BranchExtrusionOperator.bl_idname, text="Branch Extrusion")
def register():
bpy.utils.register_class(BranchExtrusionOperator)
#bpy.types.VIEW3D_MT_edit_mesh_normals.append(Menu_append)
#bpy.types.VIEW3D_PT_tools_meshedit_options.append(UI)
bpy.utils.register_class(UI)
def unregister():
bpy.utils.unregister_class(BranchExtrusionOperator)
bpy.types.VIEW3D_MT_edit_mesh_normals.remove(Menu_append)
bpy.utils.unregister_class(UI)
if __name__ == "__main__":
register()