| Code: |
|
import bpy, bmesh, mathutils # class VEF_tools: # # Temporary var storage for alpha vector slide function, use operator props later # p1 = mathutils.Vector( (0.0, 0.0, 0.0) ) # p2 = mathutils.Vector( (0.0, 0.0, 0.0) ) # vec = mathutils.Vector( (0.0, 0.0, 0.0) ) # bpy.context.scene["p1"] = p1 # bpy.context.scene["p2"] = p2 # bpy.context.scene["vec"] = vec # def constructTransfVec(): # vec = mathutils.Vector( (p2.x - p1.x, p2.y - p1.y, p2.z - p1.z ) ) # Initalize ID props needed for storage if they don't already exist try: bpy.context.scene['e1v1'] except KeyError: bpy.context.scene['e1v1'] = [0.0, 0.0, 0.0] try: bpy.context.scene['e1v2'] except KeyError: bpy.context.scene['e1v2'] = [0.0, 0.0, 0.0] try: bpy.context.scene['flipDirection'] except KeyError: bpy.context.scene['flipDirection'] = False try: bpy.context.scene['makeUnitVector'] except KeyError: bpy.context.scene['makeUnitVector'] = False ############################################# # SECTION: UI ELEMENTS (Panels, buttons etc.) ############################################# # Main panel containing VEF Tools' UI class VEF_tools_panel(bpy.types.Panel): bl_label = "VEF Tools Alpha, Vector Slide" bl_space_type = "VIEW_3D" bl_region_type = "TOOLS" def draw(self, context): self.layout.operator("vef.e1v1", text = 'e1v1') self.layout.operator("vef.e1v2", text = 'e1v2') self.layout.operator("vef.vec", text = 'Vector Slide') self.layout.prop(context.scene, 'flipDirection', text = "Flip Direction", Toggle = True) self.layout.prop(context.scene, 'makeUnitVector', text = "Make Unit Vector", Toggle = True) # Consider rewriting to reduce code duplication...not sure if execute can take extra custom params class StoreEdge1Vert1Button(bpy.types.Operator): bl_idname = "vef.e1v1" bl_label = "Store as e1v1" def execute(self, context): tempMesh = bmesh.new() # Don't do anything if not in edit mode with one vert selected if (bpy.context.active_object.type == 'MESH' and bpy.context.active_object.mode == 'EDIT'): # Init mesh data. Switch to object then back to edit mode first # to make sure vert selection data is updated/not stale. bpy.ops.object.mode_set(mode='OBJECT') bpy.ops.object.mode_set(mode='EDIT') tempMesh.from_mesh( bpy.context.active_object.data ) # Init point var for the point we're going to grab p1 = mathutils.Vector( (0.0, 0.0, 0.0) ) # Make sure only one vert is selected, iterate through the # mesh verts looking for the selected vert, and store # +++++ # Note: Check again if select_history will work reliably here later if (bpy.context.active_object.data.total_vert_sel == 1): for v in tempMesh.verts: if (v.select): p1 = v.co bpy.context.scene['e1v1'] = p1 else: print("Please select one vert only.") else: print("Select a mesh object and enter edit mode to grab vert data.") # Free the mesh, then exit tempMesh.free() return{'FINISHED'} # Consider rewriting to reduce code duplication...not sure if execute can take extra custom params class StoreEdge1Vert2Button(bpy.types.Operator): bl_idname = "vef.e1v2" bl_label = "Store as e1v2" def execute(self, context): tempMesh = bmesh.new() # Don't do anything if not in edit mode with one vert selected if (bpy.context.active_object.type == 'MESH' and bpy.context.active_object.mode == 'EDIT'): # Init mesh data. Switch to object then back to edit mode first # to make sure vert selection data is updated/not stale. bpy.ops.object.mode_set(mode='OBJECT') bpy.ops.object.mode_set(mode='EDIT') tempMesh.from_mesh( bpy.context.active_object.data ) # Init point var for the point we're going to grab p2 = mathutils.Vector( (0.0, 0.0, 0.0) ) # Make sure only one vert is selected, iterate through the # mesh verts looking for the selected vert, and store # +++++ # Note: Check again if select_history will work reliably here later if (bpy.context.active_object.data.total_vert_sel == 1): for v in tempMesh.verts: if (v.select): p2 = v.co bpy.context.scene['e1v2'] = p2 else: print("Please select one vert only.") else: print("Select a mesh object and enter edit mode to grab vert data.") # Free the mesh, then exit tempMesh.free() return{'FINISHED'} # Performs the transformation, vector slide class VectorSlideButton(bpy.types.Operator): bl_idname = "vef.vec" bl_label = "Vector Slide" def execute(self, context): # Init mesh to active obj tempMesh = bmesh.new() bpy.ops.object.mode_set(mode='OBJECT') bpy.ops.object.mode_set(mode='EDIT') tempMesh.from_mesh( bpy.context.active_object.data ) # Set up the transformation transf = mathutils.Matrix() vec = mathutils.Vector( (bpy.context.scene['e1v2'][0] - bpy.context.scene['e1v1'][0], bpy.context.scene['e1v2'][1] - bpy.context.scene['e1v1'][1], bpy.context.scene['e1v2'][2] - bpy.context.scene['e1v1'][2] ) ) print(vec) transf = transf.Translation(vec) print(transf) # Perform the trans tempMesh.transform(transf) # Write the transf to the mesh datablock and release the tempMesh bpy.ops.object.mode_set(mode='OBJECT') tempMesh.to_mesh(bpy.context.active_object.data) tempMesh.free() return{'FINISHED'} bpy.utils.register_module(__name__) |
| Code: |
| flipDirection = bpy.props.BoolProperty(...)
def invoke(self, context, event): try: context.scene['flipDirection'] self.flipDirection = True except KeyError: self.flipDirection = False # Might have to call parent's invoke here... #return super().invoke(context, event) def draw(self, context): layout = self.layout layout.prop(self, "flipDirection", text="Flip Direction") |