1

I'm learning to work with blender and also to program with Python. I found an article teaching how to create a maze, multiplying the faces and edges all through Geometry Nodes and carving the walls using Python as below. And I wanted to export this maze to fbx. But when I do this the conventional way I get only the original walls and pillars (meshes) and everything that was done through the Geometry and script is not exported. I tried to export using a script I found here in this post: Export collection and set FBX file name

But it did not work. Has anyone tried this? Would you help me?

This is the code I'm using (and blender file is attached: maze.blend):

''' select a mesh object and then run this script to turn that object into a maze '''

import bpy, bmesh, random, os

active = bpy.context.active_object

if active.type != 'MESH': raise Exception("Active object must be a mesh object")

make sure the active object has the maze nodes modifier

maze_nodes = bpy.data.node_groups['Maze'] maze_modifier = None for modifier in active.modifiers: if modifier.type == 'NODES' and modifier.node_group == maze_nodes: maze_modifier = modifier break if not maze_modifier: maze_modifier = active.modifiers.new('Maze', 'NODES') maze_modifier.node_group = bpy.data.node_groups['Maze'] for i in range(len(active.modifiers) - 1): bpy.ops.object.modifier_move_up(modifier=maze_modifier.name)

get the active mesh and make sure it has the 'closed' attribute

mesh = active.data bm = bmesh.new() bm.from_mesh(mesh) closed = mesh.attributes.get('closed') if not closed: closed = mesh.attributes.new(name='closed', type='BOOLEAN', domain='EDGE') closed.name = 'closed'

reset the maze by closing all the edges

for e in bm.edges: setattr(closed.data[e.index], 'value', True)

randomly choose a cell to start carving out the maze from

current_cell = random.choice(list(bm.faces))

current_cell = list(bm.faces)[255] visited = [current_cell] stack = [current_cell]

loop until the entire maze has been carved out

while len(stack) > 0: current_cell = stack.pop()

# get a list of unvisited neighbors to the current cell
unvisited_neighbors = []
for edge in current_cell.edges:
    for neighbor in edge.link_faces:
        if neighbor != current_cell and neighbor not in visited:
            unvisited_neighbors.append(neighbor)

# if there are any unvisited neighbors then randomly choose one to visit
if len(unvisited_neighbors) > 0:
    stack.append(current_cell)
    chosen_neighbor = random.choice(unvisited_neighbors)
    visited.append(chosen_neighbor)
    stack.append(chosen_neighbor)

    # open all the edges between the current cell and the chosen neighbor
    for edge in current_cell.edges:
        if edge in chosen_neighbor.edges:
            setattr(closed.data[edge.index], 'value', False)

update the mesh so that the maze is visible immediately

setattr(closed.data[0], 'value', False) setattr(closed.data[2], 'value', False)

mesh.update()

C = bpy.context

bpy.ops.export_scene.fbx( filepath=bpy.path.abspath("//{}.fbx".format(C.collection.name)), use_active_collection=True )

Souza
  • 111
  • 3
  • After a long chat with ChatGPT I finally got a solution to this problem. I was able to export the objects generated by Geometry and modified by the Python script. As soon as I have time, I'll post the solution with details and screenshots here. – Souza Feb 16 '23 at 21:44

0 Answers0