I'm completely new to blender and scripting and trying out some things:
I created a cube out of several smaller cubes (blue) and have one big cube called master (grey). The master cube overlaps with some of the smaller cubes.
I am now iterating through my small cubes and check with the help of this function (slightly updated):
def is_inside(p, obj):
result, location, normal, index = obj.closest_point_on_mesh(p)
p2 = location - p
v = p2.dot(normal)
return not(v <= 0.0)
If they are inside the master cube they turn green, of not they turn red. This works pretty well so far.
But now I want to move my master cube around to overlap with some other of the smaller cubes. If I run my script again it seems to check from the old position of the master and not the new one, because the same cubes as before are coloured green.
This is the situation after the second run of the script, where all visible small cubes should be red and the ones inside the grey cube should be green (FYI, they are not):
How can I get the function to work with the updated postion of the master cube? Do I have to update some other values that I am missing?
This is my code:
master = bpy.data.objects['Master']
bpy.ops.object.select_all(action='DESELECT')
# select all smaller cubes
bpy.ops.object.select_pattern(pattern='Cube.*')
# create colors
green = bpy.data.materials.new("GREEN")
green.diffuse_color = (0.0, 1.0, 0.0)
red = bpy.data.materials.new("RED")
red.diffuse_color = (1.0, 0.0, 0.0)
for cube in bpy.context.selected_objects:
if isInside(cube.location, master):
cube.active_material = green
else:
cube.active_material = red
Thanks in advance for any helpful tips!