4
$\begingroup$

I have a small script to re-size an object while keeping its proportions. There's no problem while using it in the text editor - just write either x or y or z in the field scale, then enter the new dimension in the field new_dim, and that's all, the object is re-sized keeping its proportions.

The idea is to insert it into the UI context and create a panel proposing to change the values ​​of x, y and z.

This panel would be the same as the existing dimension panel, but this one would use the script to keep the proportions of the object based on the value entered in x, y or z.

To create the UI menu is not a problem but I can not generate the 3 fields - X, Y and Z - with the opportunity to enter a value.

How can I do it?

Here is the script in question:

i = 2
ob = bpy.context.active_object
d = ob.dimensions

x = d[0]
y = d[1]
z = d[2]

scale = "x" #value chosen by the user: either x, y or z.
new_dim = 3 #value set by the user number.value set by the user.

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
$\endgroup$
5
$\begingroup$

You would need to create an operator to supply your script with parameter values. This following script derived from the operator_mesh_add.py template (accessible from the template menu in the text editor): Your code was simply copied into the scale function.

It creates an enum property to select the axis and a scale factor which can be entered from the UI in the last operator panel:

enter image description here

Look through the other templates for examples on how to add your operator to a menu, the object properties or whatever you like.

import bpy
from bpy.props import FloatProperty, EnumProperty


class ScaleAxis(bpy.types.Operator):
    """Scale axis """
    bl_idname = "mesh.primitive_scale"
    bl_label = "Scale 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.0,
            )
    # 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 menu_func(self, context):
    self.layout.operator(ScaleAxis.bl_idname, icon='MESH_CUBE')


def register():
    bpy.utils.register_class(ScaleAxis)
    bpy.types.INFO_MT_mesh_add.append(menu_func)


def unregister():
    bpy.utils.unregister_class(ScaleAxis)
    bpy.types.INFO_MT_mesh_add.remove(menu_func)

if __name__ == "__main__":
    register()

A nice tutorial

|improve this answer|||||
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.