0
$\begingroup$

I got two bugs I'm trying to solve for creating a Cube using Geometry Nodes, all scripted in Python!

I've Frankenstein this script from an older post, but for some reason, I get the error: "line 9, in AttributeError: 'NoneType' object has no attribute 'node_group'"

import bpy

# create a new cube object
cube = bpy.data.objects.new("Cube", None)
bpy.context.scene.collection.objects.link(cube)

# add geometry nodes modifier to cube object
geo_nodes_mod = cube.modifiers.new("AllTheGeoNodes", type='NODES')
geo_nodes = geo_nodes_mod.node_group

# create the input mesh data
points = geo_nodes.nodes.new(type='GeometryNodePoint')
points.location = (0, 0, 0)

primitive = geo_nodes.nodes.new(type='GeometryNodePrimitive')
primitive.geometry_primitive_type = 'CUBE'
primitive.count = 1

# connect the nodes to generate the cube
connect = geo_nodes.links.new
connect(points.outputs[0], primitive.inputs[0])

# execute the modifier to generate the cube mesh
bpy.ops.object.modifier_apply(modifier=geo_nodes_mod.name)

There doesn't seem to be a very cool guide online or way for me to solve this bug.

Consequently, it looks like even older blender python scripts using Geometry Nodes get errors too, for example, the following code yields this error in Blender 3.4: line 17, in AttributeError: 'NoneType' object has no attribute 'inputs'

import bpy

# select and del all object
bpy.ops.object.select_all(action = "SELECT")
bpy.ops.object.delete(use_global=True, confirm=False)

# add cube
bpy.ops.mesh.primitive_cube_add(enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))

# add GeometryNodes modifier
bpy.ops.object.modifier_add(type='NODES')

# access active object node_group
node_group = bpy.context.object.modifiers[0].node_group

# add socket
inputs = node_group.inputs
inputs.new(type = "NodeSocketFloat", name = "cube_size")
# remove first socket
inputs.remove(inputs[0])

inputs.new(type = "NodeSocketFloat", name = "size_x")
inputs.new(type = "NodeSocketFloat", name = "size_y")

# add node
nodes       = node_group.nodes
comXYZ_1    = nodes.new(type="ShaderNodeCombineXYZ")
comXYZ_1.location.x -= 100
comXYZ_1.location.y -= 200

comXYZ_2 = nodes.new(type="ShaderNodeCombineXYZ")

meshline_1 = nodes.new(type="GeometryNodeMeshLine")
meshline_1.location.x += 200
meshline_1.location.y += 100

meshline_2 = nodes.new(type="GeometryNodeMeshLine")
meshline_2.location.x += 200
meshline_2.location.y -= 130

meshpoint = nodes.new(type="GeometryNodeMeshToPoints")
meshpoint.location.x += 400
meshpoint.location.y -= 50

cube = nodes.new(type="GeometryNodeMeshCube")
cube.location.x += 450
cube.location.y -= 300

insonpoint_1 = nodes.new(type="GeometryNodeInstanceOnPoints")
insonpoint_1.location.x += 600
insonpoint_1.location.y += 100

insonpoint_2 = nodes.new(type="GeometryNodeInstanceOnPoints")
insonpoint_2.location.x += 850
insonpoint_2.location.y -= 100

nodes["Group Output"].location.x += 850

# connect
links = node_group.links
links.new(nodes["Group Input"].outputs["cube_size"],    comXYZ_1.inputs[0])
links.new(nodes["Group Input"].outputs["cube_size"],    comXYZ_2.inputs[1])
links.new(nodes["Group Input"].outputs["size_y"],       meshline_1.inputs["Count"])
links.new(comXYZ_2.outputs["Vector"],                   meshline_1.inputs["Offset"])
links.new(comXYZ_1.outputs["Vector"],                   meshline_2.inputs["Offset"])
links.new(nodes["Group Input"].outputs["size_x"],       meshline_2.inputs["Count"])
links.new(nodes["Group Input"].outputs["cube_size"],    cube.inputs["Size"])
links.new(meshline_2.outputs["Mesh"],                   meshpoint.inputs["Mesh"])
links.new(meshline_1.outputs["Mesh"],                   insonpoint_1.inputs["Instance"])
links.new(meshpoint.outputs["Points"],                  insonpoint_1.inputs["Points"])
links.new(cube.outputs["Mesh"],                         insonpoint_2.inputs["Instance"])
links.new(insonpoint_2.outputs["Instances"],            nodes["Group Output"].inputs["Geometry"])
links.new(insonpoint_1.outputs["Instances"],            insonpoint_2.inputs["Points"])

Any help would be apperciated! Thank you!

$\endgroup$
5
  • $\begingroup$ cube = bpy.data.objects.new("Cube", None) creates an Empty object. Empty objects can't have modifiers. You need to either create a new mesh and assgn it to the cube, or a new curve and assign it to the cube.bpy.data.meshes.new $\endgroup$
    – Gorgious
    Feb 16 at 17:10
  • $\begingroup$ @Gorgious New Error: "line 16, in <module> AttributeError: 'NoneType' object has no attribute 'nodes'" ``` # add geometry nodes modifier to cube object geo_nodes_mod = cube.modifiers.new("AllTheGeoNodes", type='NODES') geo_nodes = geo_nodes_mod.node_group $\endgroup$ Feb 16 at 18:20
  • $\begingroup$ @Gorgious, similar error, line 13, in <module> AttributeError: 'NoneType' object has no attribute 'use_nodes': # add geometry nodes modifier to cube object geo_nodes_mod = cubeObject.modifiers.new("AllTheGeoNodes", type='NODES') # create the input mesh data points = geo_nodes.nodes.new(type='GeometryNodePoint') points.location = (0, 0, 0) geo_nodes = geo_nodes_mod.node_group geo_nodes.use_nodes = True $\endgroup$ Feb 16 at 20:54
  • $\begingroup$ you should make your cube the active object with 'bpy.context.view_layer.objects.active = cube' and then assign a node group with 'bpy.ops.node.new_geometry_node_group_assign()' . Otherwise, the node group is of type None. I also couldn't get node type "GeometryNodePrimitive" to work. I can't find it anywhere in the documented geo node types: docs.blender.org/api/current/bpy.types.GeometryNode.html and I'm in Blender 3.5.0 . Where did you get that node type from? $\endgroup$ Feb 21 at 2:20
  • $\begingroup$ I am struggling with something very similar ( I am using Blender 3.5). Were you able to solve this problem? if so, how? $\endgroup$ May 11 at 16:59

0

You must log in to answer this question.

Browse other questions tagged .