# Python script not working beyond a certain line in the code

I am pretty new to coding in blender, just two days since I started.

I am trying to create a code that will align the local axis of an object to the local axis of another without changing its mesh alignment. So to do this I align the object in Object Mode and then go to edit mode to invert the changes.

I created a basic code but it seems to not work after a certain line.

import bpy
from mathutils import Matrix
from math import pi

class OrientLocalAxis(bpy.types.Panel):
"""Orients Local Axis to another Object without changing current
Structual view"""
bl_label = "Orient Local Axis to object"
bl_idname = "OBJECT_PT_OLAxis"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "object"

def draw(self, context):
layout = self.layout
scene = context.scene
obj = context.object

row = layout.row()
row.label(text="Active object is: " + obj.name)
layout.prop_search(scene, "Epty", scene, "objects")

row = layout.row()
row.operator("object.align_local_axis")

def main(context):

#-------------Obj = Object we want to change----------------------------
#------------TObj = Target Object we want to Align to-------------------

scene = bpy.context.scene
TObj = scene.objects[scene.Epty]

TObjIRE = TObj.rotation_euler
TObjIRM = TObjIRE.to_matrix()

ObjIRE = bpy.context.active_object.rotation_euler
ObjPos = bpy.context.active_object.location
# We will need this later for rotating the mesh
ObjIRM = ObjIRE.to_matrix()

#We Find out the Rotation Matrix to transform TObj to Obj---------------
# We will use this transform the mesh back to its original orientation--

ITM = ObjIRM  #ITM is for Inverse Transformation Matrix

# We set the orientation of the Object to the target

bpy.context.active_object.rotation_euler = TObjIRE

#Now we go to Edit Mode and select all Vertices

bpy.ops.object.mode_set(mode = 'EDIT')


Doesn't seem to do anything after this point

    mesh = bmesh.from_edit_mesh(bpy.context.active_object.data)

for v in mesh.verts: # Not sure if selection is needed Check Later
v.select = True

bpy.context.scene.objects.active = bpy.context.scene.objects.active

bmesh.ops.rotate(mesh, cent=(0,1,0), matrix=TOBjIRM, verts=mesh.verts)

bpy.ops.object.mode_set(mode = 'OBJECT')

# Operator Definition

class AlignLocalAxis(bpy.types.Operator):
"""Tooltip"""
bl_idname = "object.align_local_axis"
bl_label = "Align Local Axis"

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

#--------------------------------------------------------------------------
def register():
bpy.utils.register_class(AlignLocalAxis)
bpy.utils.register_module(__name__)
bpy.types.Scene.Epty = bpy.props.StringProperty()

def unregister():
bpy.utils.unregister_class(AlignLocalAxis)
bpy.utils.unregister_module(__name__)
del bpy.types.Object.Epty

if __name__ == "__main__":
register()


So the code doesn't run after it goes to edit mode, essentially from the for loop on wards. It doesn't select all the vertices or rotate them. I copied the code from the 'for loop' on wards till 'Object mode' and ran it as a separate code and it works just fine.

ps: I am not concerned about whether the code will serve my purpose or not, I would prefer fixing it myself. Thank you.

• You may want to run blender from console in order to get the stack trace in case of errors. – Blender Dadaist Sep 18 '18 at 15:36
• I think the problem was actually me missing to add bmesh, Its weired because it did not show any errors. Even now when I remove bmesh it shows no errors. – Kolandiolaka Sep 18 '18 at 16:26

I have copy-pasted the code in blender and with few changes, same variable wrong names like TObjIRM and TOBjIRM for the same var, include import bmesh at beginning,... the code works now without errors. This is what you want? ;)

import bpy, bmesh
from mathutils import Matrix
from math import pi

class OrientLocalAxis(bpy.types.Panel):
"""Orients Local Axis to another Object without changing current
Structual view"""
bl_label = "Orient Local Axis to object"
bl_idname = "OBJECT_PT_OLAxis"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "object"

def draw(self, context):
layout = self.layout
scene = context.scene
obj = context.object

row = layout.row()
row.label(text="Active object is: " + obj.name)
layout.prop_search(scene, "Epty", scene, "objects")

row = layout.row()
row.operator("object.align_local_axis")

def main(context):

#-------------Obj = Object we want to change----------------------------
#------------TObj = Target Object we want to Align to-------------------

scene = bpy.context.scene
TObj = scene.objects[scene.Epty]

TObjIRE = TObj.rotation_euler
TObjIRM = TObjIRE.to_matrix()

ObjIRE = bpy.context.active_object.rotation_euler
ObjPos = bpy.context.active_object.location
# We will need this later for rotating the mesh
ObjIRM = ObjIRE.to_matrix()

#We Find out the Rotation Matrix to transform TObj to Obj---------------
# We will use this transform the mesh back to its original orientation--

ITM = ObjIRM  #ITM is for Inverse Transformation Matrix

# We set the orientation of the Object to the target

bpy.context.active_object.rotation_euler = TObjIRE

#Now we go to Edit Mode and select all Vertices

bpy.ops.object.mode_set(mode = 'EDIT')

mesh = bmesh.from_edit_mesh(bpy.context.active_object.data)

for v in mesh.verts: # Not sure if selection is needed Check Later
v.select = True

bpy.context.scene.objects.active = bpy.context.scene.objects.active

bmesh.ops.rotate(mesh, cent=(0,1,0), matrix=TObjIRM, verts=mesh.verts)

bpy.ops.object.mode_set(mode = 'OBJECT')

# Operator Definition

class AlignLocalAxis(bpy.types.Operator):
"""Tooltip"""
bl_idname = "object.align_local_axis"
bl_label = "Align Local Axis"

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

#--------------------------------------------------------------------------
def register():
bpy.utils.register_class(AlignLocalAxis)
bpy.utils.register_module(__name__)
bpy.types.Scene.Epty = bpy.props.StringProperty()

if __name__ == "__main__":
register()

• Actually It was the bmesh, the variable mistake was added when I tried to debug. – Kolandiolaka Sep 18 '18 at 16:16