0

I run this code in Scripting, making sure that no meshes are selected. Blender freezes and hangs before any error messages are generated.

What is going wrong with the script?

import bpy

Ask the user for text

text = input("Enter text: ")

Set the text and font

bpy.ops.object.text_add(location=(0, 0, 0)) text_object = bpy.context.active_object text_object.data.body = text text_object.data.font = bpy.data.fonts["Arial"]

Set the dimensions of the text

bpy.context.object.dimensions = (text_object.dimensions[0], 10, 0.2)

Set the material of the text to metallic

text_object.active_material = bpy.data.materials.new("Metallic Material") text_object.active_material.use_nodes = True node_tree = text_object.active_material.node_tree node_tree.nodes.clear()

Add a principled BSDF node

principled_bsdf = node_tree.nodes.new("ShaderNodeBsdfPrincipled") principled_bsdf.inputs['Roughness'].default_value = 0.1 principled_bsdf.inputs['Metallic'].default_value = 1 node_tree.links.new(principled_bsdf.outputs['BSDF'], node_tree.nodes['Material Output'].inputs['Surface'])

Add a light source

bpy.ops.object.lamp_add(type='POINT', location=(-1, 1, 1)) bpy.ops.object.lamp_add(type='POINT', location=(1, 1, 1)) bpy.ops.object.lamp_add(type='POINT', location=(0, 0, 1))

Robin Betts
  • 76,260
  • 8
  • 77
  • 190
  • did you get a *crash.txt file in C:\Users[login]\AppData\Local\Temp? You may want to paste a few lines near the top into your post. If a crash file, consider submitting a Bug Report via the Help >> BugReport menu item. – james_t Dec 11 '22 at 19:21

1 Answers1

1

Your script freezes Blender because you ask the user to enter a value in the System Console window. This is not a good idea because this window can be hidden (File > Window > Toggle System Console) and Mac users don't have this menu entry at all. If the System Console window is hidden you can't enter a value and because the script blocks the UI you cannot make the window visible. Deadlock.

Better use Blender's user interface (UI) to get values from the user. Here is a similar question with an example: How to access the User input from a dialog box and also how to change it from String to Float? I am new to Python Blender

Furthermore, your script has a few flaws and issues.

  • 'Arial' is a font on Windows systems, but actually, it's four fonts: normal (regular), bold, italic, bold & italic. So you need to choose one of these not just 'Arial'.

  • the font must be loaded first before you can use it. I don't know how you can script this. But once you have selected a font and it is loaded you can use it in the script. font selection

  • the Material Output node is missing

  • the shader nodes are created overlapping

  • there is no bpy.ops.object.lamp_add() but bpy.ops.object.light_add()

Putting it all together, you can create an operator from your script that allows the user to enter the text.

Once you run the script, you can search for the operator with F3 or main menu Edit > Menu Search.... Search for object.simple_text_operator ▶Simple Text Operator (screenshot is not up to date).

search operator screenshot

On the first run, you will get a 'Hello World!' text and you can change the text in the Adjust Last Operation popup that appears in the bottom left corner. Press F9 to bring it back if you dismissed it and have not done another operation yet.

hello world! screenshot

import bpy
from bpy.props import StringProperty

def main(context, text):

# use default text if no text is entered
if len(text) == 0:
    text = 'Hello World!'   

# Set the text and font
bpy.ops.object.text_add(location=(0, 0, 0))
text_object = bpy.context.active_object
text_object.data.body = text
text_object.data.font = bpy.data.fonts["Arial Black"]    #  font must be loaded 

# Set the dimensions of the text
bpy.context.object.dimensions = (text_object.dimensions[0], 10, 0.2)

# Set the material of the text to metallic
text_object.active_material = bpy.data.materials.new("Metallic Material")
text_object.active_material.use_nodes = True
node_tree = text_object.active_material.node_tree
node_tree.nodes.clear()

# Add Material Output
material_output_node = node_tree.nodes.new("ShaderNodeOutputMaterial")

# Add a principled BSDF node
principled_bsdf = node_tree.nodes.new("ShaderNodeBsdfPrincipled")
principled_bsdf.inputs['Roughness'].default_value = 0.1
principled_bsdf.inputs['Metallic'].default_value = 1
node_tree.links.new(principled_bsdf.outputs['BSDF'], node_tree.nodes['Material Output'].inputs['Surface'])

# move the BSDF node so it doesn't hide the material output node
principled_bsdf.location.x -= 300     

# Add a light source
bpy.ops.object.light_add(type='POINT', location=(-1, 1, 1))
bpy.ops.object.light_add(type='POINT', location=(1, 1, 1))
bpy.ops.object.light_add(type='POINT', location=(0, 0, 1))


class SimpleOperator(bpy.types.Operator): """Tooltip""" bl_idname = "object.simple_text_operator" bl_label = "Simple Text Operator" bl_options = {'REGISTER', 'UNDO'} mytext : StringProperty(name="Text")

def execute(self, context):
    main(context, self.mytext)
    return {'FINISHED'}


def register(): bpy.utils.register_class(SimpleOperator)

def unregister(): bpy.utils.unregister_class(SimpleOperator)

if name == "main": register()

Blunder
  • 13,925
  • 4
  • 13
  • 36