Hi, I’m new to Python in Blender and I’m having a rough go of trying to get my script to function. Basically I am trying to replicate the “Link Scale” option in Blender 2.4x. If you check the Link Scale box, when you type or drag the object dimensions slider for either x, y, or z, the other 2 dimensions will keep their proportions. Anyway, I will post what I have and let me know where I’m going about it wrong. And yes I know the code is messy, I was planning on cleaning up once it actually works. lol
Thanks so much!
import bpy
from bpy.props import *
#
# Store properties in the active scene
#
def initSceneProperties(scn):
bpy.types.Scene.MyBool = BoolProperty(
name = "Link Scale",
description = "Change object dims proportionally")
scn['MyBool'] = False
initSceneProperties(bpy.context.scene)
#
# Menu in UI region
#
class UIPanel(bpy.types.Panel):
bl_label = "Link Scale"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
def draw(self, context):
layout = self.layout
scn = context.scene
layout.prop(scn, 'MyBool')
#
# Actual script begins here
#
class LinkScale(bpy.types.Operator):
"""Link Scale when changing object dimensions"""
bl_idname = "object.link_scale"
bl_label = "Link Scale"
def modal(self, context, event):
while bpy.context.scene.MyBool == True:
if bpy.context.active_object.dimensions.x != obj_dim[0]:
bpy.context.active_object.dimensions.y = bpy.context.active_object.dimensions.x / ratio_x_y
bpy.context.active_object.dimensions.z = bpy.context.active_object.dimensions.x / ratio_x_z
elif bpy.context.active_object.dimensions.y != obj_dim[1]:
bpy.context.active_object.dimensions.x = bpy.context.active_object.dimensions.y * ratio_x_y
bpy.context.active_object.dimensions.z = bpy.context.active_object.dimensions.y / ratio_y_z
elif bpy.context.active_object.dimensions.z != obj_dim[2]:
bpy.context.active_object.dimensions.x = bpy.context.active_object.dimensions.z * ratio_x_z
bpy.context.active_object.dimensions.y = bpy.context.active_object.dimensions.z * ratio_y_z
return {'FINISHED'}
return {'RUNNING_MODAL'}
def invoke(self, context, event):
if context.object:
obj_dim = bpy.context.active_object.dimensions.copy()
ratio_x_y = obj_dim[0] / obj_dim[1]
ratio_x_z = obj_dim[0] / obj_dim[2]
ratio_y_z = obj_dim[1] / obj_dim[2]
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
else:
self.report({'WARNING'}, "No active object, could not finish")
return {'CANCELLED'}
# Registration
def register():
bpy.utils.register_class(UIPanel)
bpy.utils.register_class(LinkScale)
def unregister():
bpy.utils.unregister_class(LinkScale)
if __name__ == "__main__":
register()