I have an object already rotated on X,Y axis and I'm trying to find a solution to use the value of a slider to rotate this object on it's 'Z' local axis.
I've made an little script to explain my problem. You will have the create a new scene, rotate the default cube using RR on all axis, then use the script :
import bpy
from bpy.props import FloatProperty
from mathutils import Vector, Matrix, Quaternion, Euler
from math import degrees, radians
class HelloWorldPanel(bpy.types.Panel):
"""Creates a Panel in the Object properties window"""
bl_label = "Hello World Panel"
bl_idname = "OBJECT_PT_hello"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "object"
def draw(self, context):
layout = self.layout
obj = context.object
row = layout.row()
row.label(text="Active object is: " + obj.name)
row = layout.row()
row.prop(obj, "name")
row = layout.row()
row.prop(context.window_manager, "rotate")
def get_rotation(self):
try:
rotate = self["rotate"]
#-- Here, the cube is always spinning...
#obj = bpy.context.object
#obj.matrix_world *= Matrix.Rotation(radians(self["rotate"]), 4, 'Z')
except KeyError:
self["rotate"] = 0
return self["rotate"]
def set_rotation(self, value):
self["rotate"] = value
#-- Here, it's working at the beginning, but fail when going toward 0.
obj = bpy.context.object
obj.matrix_world *= Matrix.Rotation(radians(self["rotate"]), 4, 'Z')
def update_rotation(self, context):
pass
#-- Here, it's working at the beginning, but fail when going toward 0.
#obj = bpy.context.object
#obj.matrix_world *= Matrix.Rotation(radians(self["rotate"]), 4, 'Z')
def register():
bpy.types.WindowManager.rotate = FloatProperty(name="Rotation", description="Rotate object.",
get=get_rotation, set=set_rotation,
update=update_rotation)
bpy.utils.register_class(HelloWorldPanel)
def unregister():
del bpy.types.WindowManager.rotate
bpy.utils.unregister_class(HelloWorldPanel)
if __name__ == "__main__":
register()
As you can see, when using the script, the value of the slider will be add to the rotation, instead I would like the rotation to match the value of the slider. I've tried with an update and the getter/setter, but i failed to find a solution. I don't know if it's possible without an operator. thanks a lot for your help.