0
$\begingroup$

I'm writing a script to generate a (smooth) curve joined from the centers of several meshes.

So, here is my code:

import bpy
import os

# PRECONDITION: The segmented meshes should be all in the following directory.

#------------------------------------------------------------------------------
# A. IMPORTING ALL MESHES 
# REFERENCE: https://blender.stackexchange.com/questions/5064/how-to-batch-import-wavefront-obj-files

path_to_objects = '/home/rahul/Samples/Original-Samples/Segmented/'

# Get list of all files in directory
file_list = sorted(os.listdir(path_to_objects))

# Get a list of files ending in 'stl' 
obj_list = [item for item in file_list if item.endswith('.stl')]
N = len(obj_list)

# Loop through the strings in obj_list and add the files to the scene
for item in obj_list:
    path_to_file = os.path.join(path_to_objects, item)
    bpy.ops.import_mesh.stl(filepath = path_to_file)
#endFor

#------------------------------------------------------------------------------
# B. EXTRACTING THE CENTERS OF EACH MESH

# Selecting everything
bpy.ops.object.select_all(action='SELECT')
# Select origins of each mesh
bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')

#------------------------------------------------------------------------------
# C. FINDING SPLINE

# Selecting the BTrace window -> Objects Connect
bpy.data.window_managers["WinMan"].curve_tracer.btrace_toolmenu = 'tool_objectsConnect'
bpy.ops.group.create(name="Btrace")

# Executing 'Run' button: 
# Iteratively execting 3 commands for each mesh
for i in range(0, N):
    bpy.ops.object.editmode_toggle()
    bpy.ops.object.hook_add_selob()
    bpy.ops.object.editmode_toggle()
#endFor


bpy.ops.object.select_all(action='DESELECT')    
bpy.ops.object.group_link(group='Btrace')
bpy.ops.object.btobjectsconnect() # >> ERROR <<

#-----------------------------------------------------------------------------
# D. EXPORTING THE GENERATED CURVE

bpy.ops.object.select_all(action='DESELECT') 
N = len(bpy.data.objects) - 1 # all the mesh + 1 curve in bpy.data.objects

# Using the name of last object as the suffix of the name of our curve: 
name = bpy.data.objects[N].name # get it's name
stl_path = os.path.join('/home/rahul/Samples/Original-Samples/Segmented/', name + '.SmoothSpline.stl')

bpy.ops.object.select_all(action='DESELECT') 

# NOTE**: the curve is at index i = 0.  

# Now, deleting next N meshes
for i in range(0,N):
    bpy.data.objects[1].select = True # always select the second mesh**
    bpy.ops.object.delete() # and delete the mesh
#endFor

# Into the EDIT mode
bpy.ops.object.editmode_toggle()
# Select all points
bpy.ops.curve.select_all(action='SELECT')
# Smoothify
bpy.ops.curve.handle_type_set(type='AUTOMATIC')
bpy.ops.object.editmode_toggle() # back in to OBJECT mode

# Exporting the mesh (curve)
bpy.ops.export_mesh.stl(filepath=stl_path)

#------------------------------------------------------------------------------
# E. FINDING FRONTAL NORMAL (N2)


#------------------------------------------------------------------------------
# F. FINDING TRANSVERSE NORMAL (N1)


#------------------------------------------------------------------------------

Here is the screenshot of the script execution: enter image description here

But, when I run the part (PART C.) of this script related to generating the curve using BTrace add-on manually and rest of the script in console, things went perfectly fine. enter image description here

So, could you please help me out debugging the error? Any insight about correcting this is appreciated. Thanks in advance.

$\endgroup$
  • $\begingroup$ I don't know why I'm getting this feeling that add-ons could not be automated (scripted). Not sure. $\endgroup$ – Rahul Nalawade Apr 4 '19 at 17:23
0
$\begingroup$

The operator that you're trying to call "bpy.ops.object.btobjectsconnect()" is part of the bTrace addon and not in Blender by default. It's defined in the plugin file btrace.py. You can find the code that built that operator around line 534 here: https://github.com/scottjwood/bTrace/blob/master/bTrace.py

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.