Skip to content

Instantly share code, notes, and snippets.

@eliemichel
Created December 15, 2017 18:07
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 eliemichel/59d7eccbf65075d23734cf9e3b4fa515 to your computer and use it in GitHub Desktop.
Save eliemichel/59d7eccbf65075d23734cf9e3b4fa515 to your computer and use it in GitHub Desktop.
"""
autoRotate
==========
(Blender edition -- also available for Maya)
Align the selected edge to the world X axis of the scene by rotating the whole mesh.
Usage
-----
Select exactly one edge, then run the script.
License
-------
Copyright (c) 2017 Elie Michel -- ATI (Universite Paris 8)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
bl_info = {
"name": "AutoTransform",
"author": "Elie Michel",
"version": (1, 0),
"blender": (2, 79, 0),
"location": "View3D > Mesh > AutoTransform",
"description": "Tools for automatic edge alignment",
"warning": "",
"wiki_url": "",
"category": "Mesh",
}
import bpy
from mathutils import Vector, Matrix
########################## Utility functions ##########################
def getPoints(context):
obj = context.object
# Get editmode changes
obj.update_from_editmode()
mesh = obj.data
selected_edges = [e for e in mesh.edges if e.select]
if len(selected_edges) != 1:
raise TypeError('You must select exactly one edge')
# Get the position of the vertices of the selected edge
edge = selected_edges[0]
p = [mesh.vertices[v].co for v in edge.vertices]
p = [obj.matrix_world * u for u in p]
return p
def applyAlignmentMatrix(context, p, mat):
obj = context.object
c = (p[0] + p[1]) / 2.0
# Translation matrices, in order to rotate around the center of the edge instead of the origin point
t1 = Matrix.Translation(c)
t2 = Matrix.Translation(-c)
obj.matrix_world = t1 * mat * t2 * obj.matrix_world
########################## Operators ##########################
class AlignEdgeOp(bpy.types.Operator):
@classmethod
def poll(cls, context):
return bpy.context.active_object.mode == 'EDIT' and bpy.context.object.type == 'MESH'
class AlignEdgeToXOperator(AlignEdgeOp, bpy.types.Operator):
"""Align the selected edge to the world X axis of
the scene by rotating the whole mesh."""
bl_idname = "transform.align_edge_to_x"
bl_label = "Align Edge to X Axis"
def execute(self, context):
p = getPoints(context)
# Reconstruct rotation matrix from the target X vector
z = Vector((0, 0, 1))
x = (p[1] - p[0]).normalized()
y = z.cross(x).normalized()
z = x.cross(y).normalized()
mat = Matrix((x, y, z)).to_4x4()
applyAlignmentMatrix(context, p, mat)
return {'FINISHED'}
class AlignEdgeToYOperator(AlignEdgeOp, bpy.types.Operator):
"""Align the selected edge to the world Y axis of
the scene by rotating the whole mesh."""
bl_idname = "transform.align_edge_to_y"
bl_label = "Align Edge to Y Axis"
def execute(self, context):
p = getPoints(context)
# Reconstruct rotation matrix from the target X vector
z = Vector((0, 0, 1))
y = (p[1] - p[0]).normalized()
x = y.cross(z).normalized()
z = x.cross(y).normalized()
mat = Matrix((x, y, z)).to_4x4()
applyAlignmentMatrix(context, p, mat)
return {'FINISHED'}
class AlignEdgeToZOperator(AlignEdgeOp, bpy.types.Operator):
"""Align the selected edge to the world Z axis of
the scene by rotating the whole mesh.
Try to rotate around X as much as possible"""
bl_idname = "transform.align_edge_to_z"
bl_label = "Align Edge to Z Axis"
def execute(self, context):
p = getPoints(context)
# Reconstruct rotation matrix from the target X vector
x = Vector((1, 0, 0))
z = (p[1] - p[0]).normalized()
y = z.cross(x).normalized()
x = y.cross(z).normalized()
mat = Matrix((x, y, z)).to_4x4()
applyAlignmentMatrix(context, p, mat)
return {'FINISHED'}
########################## Panel ##########################
class AutoRotatePanel(bpy.types.Panel):
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
bl_category = "Tools"
bl_context = "mesh_edit"
bl_label = "Auto Transform"
def draw(self, context):
layout = self.layout
col = layout.column(align=True)
col.operator("transform.align_edge_to_x")
col.operator("transform.align_edge_to_y")
col.operator("transform.align_edge_to_z")
########################## Registering ##########################
def register():
bpy.utils.register_class(AlignEdgeToXOperator)
bpy.utils.register_class(AlignEdgeToYOperator)
bpy.utils.register_class(AlignEdgeToZOperator)
bpy.utils.register_class(AutoRotatePanel)
def unregister():
bpy.utils.unregister_class(AlignEdgeToXOperator)
bpy.utils.unregister_class(AlignEdgeToYOperator)
bpy.utils.unregister_class(AlignEdgeToZOperator)
bpy.utils.unregister_class(AutoRotatePanel)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment