5
$\begingroup$

I'm trying to write a script which when run would ask the user for dimensions of different objects. But I came to know that the input and raw_input functions are not available in blender python api. Please help me out.

$\endgroup$

1 Answer 1

7
$\begingroup$

No simply there is not... but I have made this solution for you, it creates a entire menu that pops up and ask the info(scale/name) for the cube to be added. Here is the code:

import bpy
import mathutils as math

# This class is the actual dialog that pops up to create the cube
class AddCubeDialogOperator(bpy.types.Operator):
    bl_idname = "object.add_cube_dialog"
    bl_label = "Add Cube"

    # Here you declare everything you want to show in the dialog 
    name = bpy.props.StringProperty(name = "Objects Name:", default = "Cube")
    scale = bpy.props.FloatVectorProperty(name = "Scale:", default = (1.0, 1.0, 1.0)) 

    # This is the method that is called when the ok button is pressed
    # which is what calls the AddCube() method 
    def execute(self, context):        
        AddCube(self.name, self.scale)
        self.report({'INFO'}, "Added Cube")
        return {'FINISHED'}

    # This is called when the operator is called, this "shows" the dialog 
    def invoke(self, context, event):
        wm = context.window_manager
        return wm.invoke_props_dialog(self)

# This method adds a cube to the current scene and then applys scale and
# name to the cube  
def AddCube(name, scale):
    bpy.ops.mesh.primitive_cube_add() 
    bpy.context.scene.objects.active.name = name
    bpy.context.scene.objects.active.scale = scale

# Registers the cube dialog to blender so that it can be called 
bpy.utils.register_class(AddCubeDialogOperator)

# Calls the menu when the script is ran
bpy.ops.object.add_cube_dialog('INVOKE_DEFAULT')

I tried to comment everything to make you understand it all, just reply if you don't understand something. If you run this code you should get something like:

Blender pic 1 Blender pic 2

$\endgroup$
2
  • $\begingroup$ thank you @skylumz for creating this popop. After 3 years, is this still the only way to get user input? The input() isn't working yet. Can this be considered a bug? $\endgroup$ Commented Mar 4, 2021 at 10:23
  • 2
    $\begingroup$ Recommend to start here: How to create a custom UI? @gianni $\endgroup$ Commented Mar 4, 2021 at 10:24

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.