f-log

just another web log

23 Apr 2023:
oopsie

Oops, not sure what happened there...

erm, lets just move on :D

23 Apr 2023:
a bit of blender python magic to project the knife post fixed

I could have spent about 10 minutes doing this manually...

but where is the fun in that??

There will be a video soon that shows me creating a D20x20 dice in Blender.

It is for 3D printing, but my numerous print attempts were coming up short and someone made a very obvious suggestion.

"Change the depth of the 20 on each face".

For some reason this was the only time I didn't save a version at a useful point.

This code DOES NOT WORK (well, not consistently)

import bpy;

target = '20-text.015'
base_iso = 'base iso'

def get_context(type) :
    for area in bpy.context.screen.areas:
        if area.type == type:
            override = bpy.context.copy()
            override['area'] = area
            return [override, area]
    raise Exception("ERR no context found")

def align_view_to_face(context, ob) :
    bpy.ops.view3d.view_axis(context[0], type='TOP', align_active=True)
    space = context[1].spaces.active
    space.region_3d.view_perspective = 'ORTHO'

context = get_context('VIEW_3D')
target_ob = bpy.data.objects[target]
base_ob = bpy.data.objects[base_iso]

# reset to known state
bpy.ops.object.mode_set(mode = 'OBJECT')
bpy.ops.object.select_all(action='DESELECT')
bpy.context.view_layer.objects.active = None

# align view to target
bpy.context.view_layer.objects.active = target_ob
align_view_to_face(context, target_ob)

# prep edit
base_ob.select_set(True)
target_ob.select_set(True)

bpy.context.view_layer.objects.active = base_ob
bpy.ops.object.mode_set(mode = 'EDIT')

# knife project
# extrude

It was simple test code that

The scary thing is that it almost works, mostly works. It just every now and the then aligns the view incorrectly. Running it twice means the second time always works.

The rule with writing Blender scripts is to avoid doing actions directly in the view, but Knife Project needs a correctly oriented view to work.

Here is the complete code, mostly copied from this Stack Overflow post

import bpy;

target = '20-text.015'
base_iso = 'base iso'

def getArea(type):
    for screen in bpy.context.workspace.screens:
        for area in screen.areas:
            if area.type == type:
                return area

override = {'area': getArea('VIEW_3D')}

for ns3d in getArea('VIEW_3D').spaces:
    if ns3d.type == "VIEW_3D":
        break

# https://blender.stackexchange.com/questions/15118/how-do-i-override-context-for-bpy-ops-mesh-loopcut
win      = bpy.context.window
scr      = win.screen
areas3d  = [getArea('VIEW_3D')]
region   = [region for region in areas3d[0].regions if region.type == 'WINDOW']

override = {'window':win,
            'screen':scr,
            'area'  :getArea('VIEW_3D'),
            'region':region[0],
            'scene' :bpy.context.scene,
            'space' :getArea('VIEW_3D').spaces[0],
            }

target_ob = bpy.data.objects[target]
base_ob = bpy.data.objects[base_iso]

# reset to known state
bpy.ops.object.mode_set(mode = 'OBJECT')
bpy.ops.object.select_all(action='DESELECT')
bpy.context.view_layer.objects.active = None

# align view to target
bpy.context.view_layer.objects.active = target_ob

if ns3d.region_3d.view_perspective == 'PERSP':
    bpy.ops.view3d.view_persportho(override)

bpy.ops.view3d.view_selected(override)
bpy.ops.view3d.view_axis(override, type='TOP', align_active=True)

# Update region:
ns3d.region_3d.update()

# prep edit
base_ob.select_set(True)
target_ob.select_set(True)

bpy.context.view_layer.objects.active = base_ob
bpy.ops.object.mode_set(mode = 'EDIT')

# knife project
bpy.ops.mesh.knife_project(override)

# extrude
bpy.ops.mesh.extrude_region_shrink_fatten(
    MESH_OT_extrude_region={
    "use_normal_flip":False, 
    "use_dissolve_ortho_edges":False, 
    "mirror":False}, 
    TRANSFORM_OT_shrink_fatten={"value":-5, 
    "use_even_offset":False, 
    "mirror":False, 
    "use_proportional_edit":False, 
    "proportional_edit_falloff":'SMOOTH', 
    "proportional_size":4.59497, 
    "use_proportional_connected":False, 
    "use_proportional_projected":False, 
    "snap":False, 
    "release_confirm":False, 
    "use_accurate":False})

The main differences are

There appear to spaces and regions with in the areas that make up views in the workspace.

Only by accessing all these in order can you manipulate the view in a consistent manner.

Oh, Blender wrote that extrude_region_shrink_fatten command.

The upcoming video was created to record some of the steps that took me ages to figure out and hopefully these Python notes can help me in the future.

16 Apr 2023:
a bit of blender python magic to project the knife
loading results, please wait loading animateloading animateloading animate
[More tags]
rss feed

email

root

flog archives


Disclaimer: This page is by me for me, if you are not me then please be aware of the following
I am not responsible for anything that works or does not work including files and pages made available at www.jumpstation.co.uk I am also not responsible for any information(or what you or others do with it) available at www.jumpstation.co.uk In fact I'm not responsible for anything ever, so there!