Blender python scripts error

Why does attached Python script produce error «Cannot execute boolean operation»?

I am trying to create a 3D model using Python scripting with Blender 2.69. I start from a cube and subtract several spheres from it to make indentations. I then form a ring out of four cylinders and four spheres, which I then also subtract from the sphere. However, the result of this step (as marked in the script) is unexpected (the ring isn’t subtracted, only one cylinder is deleted/unlinked from the scene). When I inspect the cube in the Blender UI, it only says:

There is also something else that seems odd and that could be related: If I uncomment the two lines at the end of union() , it will lead to this error message:

How can I dig deeper and find out more about the root cause, and how does the script have to be patched? I’ve seen this previous answer and a mentioning of the error message in the Blender documentation, however neither seemed to provide sufficient information. I get the sense that the problem may be that several of the objects which I want to subtract are overlapping and it may not be possible to subtract them all at once, but I don’t (yet) know what specific changes to the script are needed to obey this piece of advice from the documentation:

In this case, you can apply the first Boolean modifier of the stack for the target and then use the other Boolean modifier(s) in the stack for subsequent operations.

import bpy import math def deleteAll(): bpy.ops.object.select_all(action = 'SELECT') bpy.ops.object.delete() def createBoard(): # prepare some constants BOARD_WIDTH = 22.7 BOARD_HEIGHT = 22.7 BOARD_DEPTH = 1.5 SPHERE_DIAMETER = 1.9 SPHERE_PADDING = 0.5 DITCH_DIAMETER = 2.5 DITCH_PADDING = 0.8 # create platform origin = (0,0,0) bpy.ops.mesh.primitive_cube_add(location=origin) platform = bpy.context.active_object platform.scale = (BOARD_WIDTH/2,BOARD_HEIGHT/2,BOARD_DEPTH/2) # create spheres for i in [-2,-1,1,2]: for j in [-2,-1,1,2]: x = (SPHERE_DIAMETER + SPHERE_PADDING)*(i - copysign(.5,i)); y = (SPHERE_DIAMETER + SPHERE_PADDING)*(j - copysign(.5,j)); z = BOARD_DEPTH/2 difference(platform,createSphere(x,y,z,SPHERE_DIAMETER/2)) # create ditch xMax = BOARD_WIDTH/2 - DITCH_PADDING - DITCH_DIAMETER/2 xMin = -xMax yMax = BOARD_HEIGHT/2 - DITCH_PADDING - DITCH_DIAMETER/2 yMin = -yMax z = BOARD_DEPTH/2 r = DITCH_DIAMETER/2 ring = createCylinder(xMin,0,z,r,xMax,[math.pi/2,0,0]) union(ring,createSphere(xMin,yMin,z,r)) union(ring,createCylinder(0,yMin,z,r,xMax,[0,math.pi/2,0])) union(ring,createSphere(xMax,yMin,z,r)) union(ring,createCylinder(xMax,0,z,r,yMax,[math.pi/2,0,0])) union(ring,createSphere(xMax,yMax,z,r)) union(ring,createCylinder(0,yMax,z,r,yMax,[0,math.pi/2,0])) union(ring,createSphere(xMin,yMax,z,r)) difference(platform,ring) # this leads to unexpected result def createSphere(x, y, z, r): origin = (x,y,z) bpy.ops.mesh.primitive_uv_sphere_add(location=origin) sphere = bpy.context.active_object sphere.scale = (r,r,r) return sphere def createCylinder(x, y, z, r, height, rotation): origin = (x,y,z) bpy.ops.mesh.primitive_cylinder_add(location=origin) cylinder = bpy.context.active_object cylinder.scale = (r,r,height) cylinder.rotation_mode = 'XYZ' cylinder.rotation_euler = rotation return cylinder def difference(first,second): modifier = first.modifiers.new('Modifier', 'BOOLEAN') modifier.object = second modifier.operation = 'DIFFERENCE' bpy.ops.object.modifier_apply(apply_as='DATA', modifier=modifier.name) scene = bpy.context.scene scene.objects.unlink(second) def union(first,second): modifier = first.modifiers.new('Modifier', 'BOOLEAN') modifier.object = second modifier.operation = 'UNION' bpy.ops.object.modifier_apply(apply_as='DATA', modifier=modifier.name) #scene = bpy.context.scene #scene.objects.unlink(second) if __name__ == "__main__": deleteAll() createBoard() 

Источник

Читайте также:  Functions tutorial in javascript

How to view python error messages? [duplicate]

enter image description here

I’m following a tutorial and I can´t go forward and finish the tutorial. My problem is that when i click on run script it gives me a problem saying «Python script fail, look in the console for now. » I use the recent blender and blender 2.69 and both of them give an error. The problem is in the top red line. The tutorial I’m following is https://www.youtube.com/watch?v=jipaKhQWDeg Can’t pass from the 15:00 minute.

3 Answers 3

On Windows you can toggle the console windows using the Menu: Window / Toggle System Console

enter image description here

$\begingroup$ Oh, it was referring to the System console, not the Python console. I guess that makes sense, though the error message is a bit confusing. $\endgroup$

You need to have the python console open to see the real python error. On Windows the console window opens separately when Blender launches. On OSX and Linux, you need to launch the blender.app file from the Terminal to have access to the separate python console window.

Sorry if this answer isn’t quite to your question, but having to do with the subject:

If you are on a mac, here is something for you:

with my own «getting annoyed» experience and help from the idea of this guy (sambler) I made a simple app for the purpose of opening blender with terminal.

—Please try it, it is easy to install and super handy—

here is the application.
This is how you use it:

  1. Navigate yourself to blender.app with finder.
  2. Right click on blender and select «Show Package contents».
  3. Download the app using the link above and unzip it.
  4. Drag the app into blender’s «Contents» folder.
  5. Drag the app to dock and open for the first time.
  6. (optional) Dance around the room and sing about how fortunate you are to have such an app.

please visit this post for source code and an image guide.

Источник

Blender python scripts error

For help with Blender problems, also consider visiting [BA](https://blenderartists.org/c/support) or [BSE](https://blender.stackexchange.com/).

I am trying to import .nif files, but when I press «OK» it tells me Python Script Error (See title)

Blender NIF Scripts 2.5.9 (running on Blender 249, PyFFI 2.1.11) Traceback (most recent call last): File «C:\Program Files (x86)\Blender Foundation\Blender.blender\scripts\bpymodules\nif_common.py», line 1235, in gui_button_event self.gui_exit() File «C:\Program Files (x86)\Blender Foundation\Blender.blender\scripts\bpymodules\nif_common.py», line 1598, in gui_exit self.callback(**self.config) File «C:\Program Files (x86)\Blender Foundation\Blender.blender\scripts\import\import_nif.py», line 3771, in config_callback importer = NifImport(**config) File «C:\Program Files (x86)\Blender Foundation\Blender.blender\scripts\import\import_nif.py», line 170, in init data.inspect(niffile) File «C:\Python26\lib\site-packages\pyffi\formats\nif_init_.py», line 1286, in inspect self.header.read(stream, data=self) File «C:\Python26\lib\site-packages\pyffi\object_models\xml\struct_.py», line 354, in read attr_value.read(stream, data) File «C:\Python26\lib\site-packages\pyffi\object_models\xml\array.py», line 300, in read elem.read(stream, data) File «C:\Python26\lib\site-packages\pyffi\object_models\common.py», line 691, in read % (length, stream.tell())) ValueError: string too long (0x6C0C0000 at 0x0000003A)

Edit: i am new, sorry about the format

Related Blender 3D computer graphics software Software Information & communications technology Technology

Источник

Simple Python script: errors

I try to run this script in Blender 2.8 to add the function to the driver but it constantly fails. Why? My second problem is to get this working:

 import bpy activeobj = bpy.context.active_object def driver_func(val): if activeobj.name = "A": output = val * val else: output = val + 20 return output #add function to driver_namespace bpy.app.driver_namespace['driver_func'] = driver_func 
SyntaxError: invalid syntax location: :-1 Error: Python script failed, check the message in the system console 

$\begingroup$ The error message is: AttributeError: ‘IndentationError’ object has no attribute ‘msg’ Error: Python script failed, check the message in the system console $\endgroup$

$\begingroup$ Try indented like this i.stack.imgur.com/i4Szf.jpg (use tabs) It is the way python interprets things. $\endgroup$

$\begingroup$ Hi. Please only ask one question per post. If you need to ask multiple questions, ask them separately. $\endgroup$

2 Answers 2

I would recommend that you use a proper IDE (Integrated Development Environment) that supports Python. This will allow you to check for syntax problems. The correct indentation of the code is important for Python because it is used to determine what part of the code belongs to, e.g. an if, else condition or a function.

Correctly formatted your first code should look like this:

import bpy def driver_func(val): if val < 5: output = val * val else: output = val + 20 return output # add function to driver_namespace bpy.app.driver_namespace['driver_func'] = driver_func 

Note that everything that belong into the function is indented by four spaces. What belongs inside the condition is indent by another four additional spaces. This is necessary in Python, because unlike for instance C, it doesn't use curly braces to determine the scope.

Your second script is problematic for two reasons.

  1. The syntax is invalid. If you want to perform an equality check you have to use == instead of = . The latter is only used to assign a value to a variable.
  2. You're declaring a variable outside the function, but are using it inside without passing it as an argument. This is bad because a) you don't want to create global state b) in this particular case the driver will not be able to access the variable because it has gone out of scope. The bpy.app.driver_namespace['driver_func'] only contains a reference to the function, but not the variable you're trying to use.
  3. You cannot access the active object like through bpy.context.active_object when using the code in a driver. It should be bpy.context.view_layer.objects.active .

The correct script would therefore be:

import bpy def driver_func(val): active_obj = bpy.context.view_layer.objects.active if active_obj.name == "A": output = val * val else: output = val + 20 return output #add function to driver_namespace bpy.app.driver_namespace['driver_func'] = driver_func 

Источник

Оцените статью