I have try to make an UI Panel with my operator, but i can not create a panel with the 2 choices (axis and widht).
I can create a button who launch a menu in the tools pannel, and the operator works fine with this :
layout = self.layout
layout.operator(ChooseAxis.bl_idname)
But i want to have exactly this menu in the UI panel, not in tools, so I have try to write this :
layout.prop(obj, "axis")
layout.prop(obj, "newdim")
But that doesn't works, values are modifiable but are ignored and do not modify the object.
How can i have the operator directly on the UI Panel ? (Not a button to launch the operator, but the operator himself with its Properties.)
Moreover, how i can keep the object size value when i launch the operator ? Because when i launch it, it the default value of the newdim property is set to 1 (for example), the object is directly resized. Is it possible to have a default="object actual value" ?
here the entire script with the ui panel created :
from bpy import *
import bpy
from bpy.props import FloatProperty, EnumProperty
class ProportionalDimensions(bpy.types.Panel):
"""Creates a Panel in the Object UI window"""
bl_label = "Proportional Dimensions"
bl_idname = "prop_dim"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
#bl_context = "object"
def draw(self, context):
obj = context.object
#row = layout.row()
#row.label(text="Active object is: " + obj.name)
layout = self.layout
layout.operator(ChooseAxis.bl_idname)
layout.prop(obj, "axis")
layout.prop(obj, "newdim")
class ChooseAxis(bpy.types.Operator):
bl_idname = "object.choose_axis"
bl_label = "Choose_the_Right_Axis"
bl_description = "This is to choose the axis"
bl_options = {'REGISTER', 'UNDO'}
axis: EnumProperty(
name="Axis",
description="Axis selection",
items= [('x', "X", ""),('y', "Y", ""),('z', "Z","")],
default='x'
)
newdim: FloatProperty(
name="Width",
description="Scale",
min=0.01, max=100.0,
default=1,
)
# your code
def scale(self, axis, new_dim):
ob = bpy.context.active_object
d = ob.dimensions
x = d[0]
y = d[1]
z = d[2]
scale = axis
if( scale == "x"):
indice = 0
elif( scale == "y"):
indice = 1
else:
indice = 2
old_dim = d[indice]
factor = new_dim / old_dim
scale_x = ob.scale[0]
scale_y = ob.scale[1]
scale_z = ob.scale[2]
ob.scale[0] = scale_x * factor
ob.scale[1] = scale_y * factor
ob.scale[2] = scale_z * factor
def execute(self, context):
self.scale(self.axis,self.newdim)
return {'FINISHED'}
def register():
bpy.utils.register_class(ProportionalDimensions)
bpy.utils.register_class(ChooseAxis)
def unregister():
bpy.utils.unregister_class(ProportionalDimensions)
bpy.utils.unregister_class(ChooseAxis)
if __name__ == "__main__":
register()