3
$\begingroup$

Hello Blender Community,

I am trying to simulate a machining process in blender. The cutting tool used is simplified via cylinder and its position is extracted to space delimited text file from NC Code.

Currently, the code I am running via text editor is

import bpy

with open(bpy.path.abspath("//cutting position trial.txt"),
       'r', buffering = 1) as content:

    for line in content:
        # extract tool position
        x, y, z = line.split(' ',2)

        # create tool
        bpy.ops.mesh.primitive_cylinder_add(
            vertices = 3000, 
            radius = 0.5, 
            depth = 10,
            location = (float(x), float(y), float (z))
            )

        # add BOOLEAN modifier
        bpy.data.objects['ISO 10791 M1 blank cd 0.0025'].modifiers.new('Boolean', 'BOOLEAN')
        bpy.data.objects['ISO 10791 M1 blank cd 0.0025'].modifiers['Boolean'].operation = 'DIFFERENCE'
        bpy.data.objects['ISO 10791 M1 blank cd 0.0025'].modifiers['Boolean'].object = bpy.data.objects["Cylinder"]
        bpy.data.objects['ISO 10791 M1 blank cd 0.0025'].modifiers['Boolean'].solver = 'CARVE'
        # activate the blank
        bpy.context.scene.objects.active = bpy.data.objects['ISO 10791 M1 blank cd 0.0025']
        # apply modifier
        bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Boolean")

        # Remove cylinder
        bpy.ops.object.select_all(action='DESELECT')
        bpy.data.objects['Cylinder'].select = True
        bpy.ops.object.delete()

The code works for small amount of lines, around 15 lines are tested.

However, when loading full file blender quit after a few seconds and give error file:

Terminal Output

Is there something wrong with the code? I thought the file was read line by line so the the problem should not happened.

The complete zip file can be downloaded from: https://drive.google.com/file/d/1aUYUExuDTMFPYKpgOcB34_cXWutqMTPV/view?usp=sharing

Kind regards. iwan

$\endgroup$
  • $\begingroup$ can you please supply a representative .txt file, so people can run the code straight from the TextEditor and help out. $\endgroup$ – zeffii Feb 15 at 9:41
  • $\begingroup$ added the downloadable zip. $\endgroup$ – Iwan Sanjaya Awaluddin Feb 16 at 12:48
0
$\begingroup$

Some of the way there

Here is some changes to get you some of the way there. Unfortunately I have a late version of 2.79 where the boolean modifier no longer has the solver property, so I can't be sure of result.

  • add and remove the cylinder once at start and end respectively.
  • set the cd object as active and select.

  • reference the modifier when added, apply by reference (name).

Edited script:

import bpy
context = bpy.context
scene = context.scene
bpy.ops.mesh.primitive_cylinder_add(
    vertices = 3000, 
    radius = 0.5, 
    depth = 10,
    location = (0, 0, 0)
        )
cylinder = context.object
cylinder.select = False
cd = scene.objects['ISO 10791 M1 blank cd 0.0025']
scene.objects.active = cd
cd.select = True
with open(bpy.path.abspath("//cutting position trial.txt")) as content:            
    for i, line in enumerate(content):
        print(i)

        # extract tool position
        x, y, z = [float(n) for n in line.split(' ',2)]
        cylinder.location = (x, y, z + 5)
        scene.update() # may not be needed.
        # add BOOLEAN modifier
        m = cd.modifiers.new('Boolean', 'BOOLEAN')
        m.operation = 'DIFFERENCE'
        m.object = cylinder
        #m.solver = 'CARVE'

        # apply modifier
        bpy.ops.object.modifier_apply(apply_as='DATA', modifier=m.name)
bpy.data.objects.remove(cylinder)
$\endgroup$
  • $\begingroup$ Thank you for the reply. The BMesh solver gives wrong result for the operation. Using the Carve solver still result in the same error. $\endgroup$ – Iwan Sanjaya Awaluddin Feb 17 at 13:46

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.