3

How to make a hollow cube object by Python?

edges = []
verts = [(1.0, 1, 0), (1.0, -1.0, 0), (-1, -1, 0), (-1, 1, 0),(2.0, 2, 0), (2.0, -2.0, 0), (-2, -2, 0), (-2, 2, 0)]
faces = [(0, 1, 2, 3), (4, 5, 6, 7)]

mesh_data = bpy.data.meshes.new("cube_mesh_data") mesh_data.from_pydata(verts, [], faces) mesh_data.update() hsname='hollowobj' hsobj = bpy.data.objects.new(hsname, mesh_data) bpy.context.collection.objects.link(hsobj) bpy.context.view_layer.objects.active = hsobj hsobj.select_set(True) bpy.ops.object.modifier_add(type='SOLIDIFY') bpy.context.object.modifiers["Solidify"].thickness = 2

But it did not work!

Gorgious
  • 30,723
  • 2
  • 44
  • 101
Derekcbr
  • 99
  • 1
  • 8
  • Hello, could you add a diagram or example of what you're trying to construct ? – Gorgious Jul 07 '21 at 12:12
  • If Tlousky's answer is not what you're after, then perhaps you just want to create two cubes - the inner cube would be the inner surface - of course you would have to flip the normals of the inner cube. – Markus von Broady Jul 07 '21 at 13:51

2 Answers2

7

You can use the bmesh create_cone operator to generate a hollowed out tube (essentially a 4 sided cylinder).

enter image description here

import bpy, bmesh

bm = bmesh.new() bmesh.ops.create_cone( bm, cap_ends=False, cap_tris=False, segments=4, diameter1=1, diameter2=1, depth=2 )

name = 'Hollow Cube' m = bpy.data.meshes.new(name)

C = bpy.context S = bpy.context.scene

bm.to_mesh(m)

o = bpy.data.objects.new(name, m) S.collection.objects.link(o)

You can later add a solidify modifier like you did (or by using the more efficient low-level object.modifiers.new method.

TLousky
  • 16,043
  • 1
  • 40
  • 72
4

Reverse Engineer it.

In answers to

How to draw a circle hole by bmesh on the plane?

and

and Create Circle with Inner Radius (Similarly to @Tlouskey mentions adding a cone, but whereas he is using solidify thickness for wall thickness, cone with depth=0 and hence solidify thickness as height if you will

demonstrate other methods to make a hole in a mesh object.

another would be add plane, inset face, then delete face.

enter image description here Reminder start with cursor in sensible position, anyway set inset to 1

which could be coded, however and having just done that, will use How do I Create a script for geometry I create? to "reverse engineer it into a script"

import bpy

context = bpy.context

verts = ((-2.0, -2.0, 0.0), (2.0, -2.0, 0.0), (-2.0, 2.0, 0.0), (2.0, 2.0, 0.0), (-1.0, 1.0, 0.0), (-1.0, -1.0, 0.0), (1.0, -1.0, 0.0), (1.0, 1.0, 0.0))

faces = ((5, 4, 2, 0), (6, 5, 0, 1), (7, 6, 1, 3), (4, 7, 3, 2))

me = bpy.data.meshes.new("Plane") me.from_pydata(verts, [], faces) ob = bpy.data.objects.new("Plane", me) context.collection.objects.link(ob) context.view_layer.objects.active = ob

edit to change "Plane" to suit, and as mentioned can add modifier by appending

sm = ob.modifiers.new("Solidify", 'SOLIDIFY')
sm.thickness = 2 

to script.

Note.

To make the cube hollow by giving it "wall thickness", can add the modifier directly to the default cube.

A sphere is used via boolean difference modifier for cutaway.

enter image description here

batFINGER
  • 84,216
  • 10
  • 108
  • 233