Where is the error in my Python script for Blender?

import bpy # Funktion zum Erstellen eines Materials def create_material(name, base_color):  material = bpy.data.materials.new(name=name) material.use_nodes = True material.node_tree.nodes["Principled BSDF"].inputs["Base Color"].default_value = base_color return material # Funktion zum Erstellen eines Rechtecks def create_rectangle(name, location, scale, material): bpy.ops.mesh.primitive_cube_add(size=1, location=location) rectangle = bpy.context.active_object rectangle.name = name rectangle.scale = scale rectangle.data.materials.append(material) return rectangle # Szene zurücksetzen bpy.ops.wm.read_factory_settings(use_empty=True) # Farben für die verschiedenen Seiten der Handyhülle case_colors = [ (1.0, 0.8, 0.6, 1.0), # Vorderseite (Beispiel: hellbraun) (0.8, 0.8, 0.8, 1.0), # Rückseite (Beispiel: hellgrau) (0.0, 0.0, 0.0, 1.0), # Seiten (Beispiel: schwarz) ] # Farben für Graphen und PCM graphene_color = (0.0, 1.0, 0.0, 1.0) # Grün pcm_color = (0.0, 0.0, 1.0, 1.0) # Blau epoxy_color = (1.0, 1.0, 1.0, 1.0) # Weiß für Epoxidharz # Länge, Breite und Dicke der Handyhülle length = 0.15 width = 0.07 thickness = 0.02 # Ursprungskoordinaten origin = (0, 0, 0) # Material für Graphen erstellen graphene_material = create_material("Graphene_Material", graphene_color) # Material für PCM erstellen pcm_material = create_material("PCM_Material", pcm_color) # Material für Epoxidharz erstellen epoxy_material = create_material("Epoxy_Material", epoxy_color) # Schleife zum Erstellen der Seiten der Handyhülle mit verschiedenen Farben for i, color in enumerate(case_colors): # Material für die Handyhülle erstellen und zuweisen case_material = create_material(f"Case_Material_{i}", color) # Handyhüllen-Rechteck erstellen create_rectangle(f"HandyHuelle_{i}", origin, (length if i == 0 else width, width if i == 0 else thickness, thickness if i == 0 else width), case_material) # Graphen in der Mitte platzieren create_rectangle("Graphene", (origin[0] + length / 2, origin[1] + width / 2, origin[2] + thickness / 2), (width,), graphene_material) # PCM auf der Innenseite platzieren mit Epoxidharz-Beschichtung pcm_rectangle = create_rectangle("PCM", (origin[0] + length / 2, origin[1] + width / 2, origin[2] + thickness / 2), (width,), pcm_material) # Material der Epoxidharz-Beschichtung zuweisen epoxy_material_node = pcm_material.node_tree.nodes.new(type='ShaderNodeBsdfPrincipled') epoxy_material_node.location = (-300, 0) # Position des Materialknotens für Epoxidharz epoxy_material_node.material = bpy.data.materials.get("Epoxy_Material") # Material der Epoxidharz-Beschichtung verbinden pcm_material.node_tree.links.new(pcm_material.node_tree.nodes["Material Output"].inputs['Surface'], epoxy_material_node.outputs['BSDF'])

I don't notice any errors, but Blender says:

 Python: Traceback (most recent call last): File "\Text", line 57, in <module> File "\Text", line 13, in create_rectangle AttributeError: 'Context' object has no attribute 'active_object'

Help. 😀

Greetings from the neighborhood.

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
5 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
KarlRanseierIII
1 year ago
rectangle = bpy.context.active_object

The error message says bpy.context does not know an attribute active_object. What does the documentation say?

https://docs.blender.org/api/current/bpy.context.html#bpy.context.active_object

Is your code executed in the Screen Context? Then there should be the attribute.

regex9
1 year ago

You might come across an alternative way, like the View Layer (bpy.context.view_layer.objects.active) to the object.

KarlRanseierIII
1 year ago

Hmmm, I can’t help anymore.

The only thing I remember would be to dump an introspection of the context in a log or the like, perhaps to see if it is executed in the wrong context, or whether or not a docu error or whatever.