0

I want to cut a solid with other (ex: cubes) using script

A script like:

import bpy
context = bpy.context 
scene = context.scene
cube1 = context.active_object
cube2 = cube1.copy()
cube2.data = cube1.data.copy()
cube1.scale = (2.0, 2.0, 0.9)
mod = cube1.modifiers.new("SomeName", type='BOOLEAN')
mod.operation = 'DIFFERENCE'
mod.object = cube2
bpy.ops.object.modifier_apply(apply_as='DATA', modifier=mod.name)

doesn't work.

I use Blender v2.82

The error said:

AttributeError: 'NoneType' object has no attribute 'modifiers'

What is wrong with my example


Maybe something is not selected, but what and how?

I try to change Blender version to 2.9301

and the error now is:

Traceback (most recent call last):
  File "\Text", line 18, in <module>
  File "E:\Programs\for_CAD3D\Blender2v901w64\2.90\scripts\modules\bpy\ops.py", line 201, in __call__
    ret = op_call(self.idname_py(), None, kw)
TypeError: Converting py args to operator properties: : keyword "apply_as" unrecognized
Error: Python script failed, check the message in the system console

Could be an error from python version?

Best regards, Ciprian

Robert Gützkow
  • 25,622
  • 3
  • 47
  • 78
  • Based on the Python error, it looks like you don't have a valid reference to an object; i.e., cube1 is None at the point where you try to access cube1.modifiers. Probably you accidentally did not specify the appropriate active object before running your script. – NeverConvex Aug 27 '21 at 11:41
  • To verify that the code itself is fine: I entered your code in a Scripting tab with just the default cube present initially (in v2.83, but the small minor version number difference probably doesn't matter here), and it behaved as expected (generated a wider, shorter cube with its 'center' removed by the differencing operation). – NeverConvex Aug 27 '21 at 11:45
  • 1
    Your code won't run if you didn't actually select an object (either via the interface or through code) – Gorgious Aug 27 '21 at 13:12
  • Logic would lead to: Error message posted doesn't match this script. @Gorgious et al how do you feel about a catch all wiki Q for this and similar type of q. (like resources for blender) "Common python errors when starting blender scripting" . to close as dupe to.? This one comes up a lot. ... gets closed as off topic with comment search error type at stackoverflow yada yada – batFINGER Aug 27 '21 at 13:55
  • @batFINGER I think that would be a great idea, experienced people could contribute to answers in a single place, and newcomers wouldn't be booted off to foreign lands never to be seen again :) – Gorgious Aug 27 '21 at 14:04
  • Please take a look at the tour page to learn how Stack Exchange works. This is a Q&A website not a forum. Don't post edits or clarifications as an answer. Additionally, if you have new question unrelated to the original post, please create a new question. – Robert Gützkow Aug 28 '21 at 08:23
  • Does this answer your question? Boolean difference not making any diiference (Python scripting) Have updated script in that answer (& this question) to 2.8. – batFINGER Aug 28 '21 at 15:14

2 Answers2

0

I solved with substract like this:

import bpy
import sys
context = bpy.context
bpy.ops.mesh.primitive_cube_add(location=(0, -1.5, 0))
small_cube = context.object
bpy.ops.mesh.primitive_cube_add(location=(0.5, 0, 0.5))
large_cube = context.object
mod = large_cube.modifiers.new("Boolean", type='BOOLEAN')
mod.operation = 'DIFFERENCE'
mod.object = small_cube
bpy.ops.object.modifier_apply(modifier=mod.name)
context.scene.objects.unlink(small_cube)

Now I want to unlink and delete an object!

The error is:

"Traceback (most recent call last):
  File "\substract_blender_v3901.py", line 23, in <module>
AttributeError: 'bpy_prop_collection' object has no attribute 'unlink'
Error: Python script failed, check the message in the system console"

Probably the command: context.scene.objects.unlink(small_cube) is wrong in sense of definition. I founded into the documentation!

Best regards, Ciprian

Robert Gützkow
  • 25,622
  • 3
  • 47
  • 78
  • Please edit your answer to fix the error instead of posting a new answer. – Robert Gützkow Aug 28 '21 at 08:25
  • In the end it looks like your question is upgrading the script, in answer to duplicate question, to 2.8. See also https://blender.stackexchange.com/questions/133291/scriptinghow-to-correctly-add-a-boolean-modifer-to-an-object Please consider also linking to source of script as well as posting the code. – batFINGER Aug 28 '21 at 15:40
-3

I solved also the problem with unlink

Since 2.8+ the context.scene.objects.unlink(small_cube) is deprecated and modify to: currentCollection.objects.unlink(small_cube)

  • 3
    Kudos on your enthusiasm. Suggest a solution that has an error is not a solution (your other two answers) ... IMO an issue with this answer, is it no longer matches the question. – batFINGER Aug 27 '21 at 20:10
  • For me it working in this way. If others have new ideas, please share it with us, here, with complete functional codes , for them, course, with explanation about files and versions and maybe also about System version ! Best regards, – Dromihetex Aug 29 '21 at 07:00