trimesh icon indicating copy to clipboard operation
trimesh copied to clipboard

render_scene crashes with viewport width, height == 0,0 after iterative calls for saving

Open johanjohan opened this issue 2 years ago • 1 comments

Hi, I enjoy trimesh, thank you. I am trying to capture a sequence of screenshots for 3d object archival. I am on Win 10 py 3.9.13.

Running the following code, has 2 issues

  • the viewport is not always in the requested resolution
  • the viewport crashes on 0,0 which accidentally occurs

Here is the code:

    # create settings
    settings_list = []
    for flags in flags_list:
        for camera_transform in camera_transforms:
            for line_width in (list(set([1])) if flags["wireframe"] else [999]): # 0 crashes
                line_settings = {
                    'point_size': line_width,
                    'line_width': line_width,
                }
                settings_list.append({
                    "flags": flags,
                    "line_settings": line_settings,
                    "camera_transform": camera_transform,
                })
                
    settings_list   = list_of_dicts_make_unique(settings_list)
    mesh            = trimesh.load(mesh_path)
    scene           = trimesh.Scene([mesh])
    basename, ext   = os.path.splitext(out_pic_path)
    for cnt, settings in enumerate(settings_list):
        print(cnt, "."*44)
        print(GRAY, json.dumps(settings, indent=4, sort_keys=True), RESET)
        scene.camera_transform = settings["camera_transform"]
        scene.camera.fov = [60., 45.]        
        try:
            if False:
                png = scene.save_image(
                    resolution=resolution, 
                    background=background, 
                    flags=settings["flags"],
                    smooth=smooth,
                    offset_lines=False,
                    visible=True,
                    line_settings=settings["line_settings"],
                ) 
            else:
                png = trimesh.viewer.render_scene(
                    scene,
                    resolution=resolution, 
                    background=background, 
                    flags=settings["flags"],
                    smooth=smooth,
                    offset_lines=False,
                    visible=True,
                    line_settings=settings["line_settings"],
                )
                
            out_path = basename + f"_{cnt:03d}.png"
            print("\t\t\t", CYAN, "saving:", out_path, RESET)
                        
            with open(out_path, 'wb') as fp:
                fp.write(png)       
                         
        except Exception as e:
            print(RED, "ERROR", YELLOW, e, RESET)
            time.sleep(1)  
            
    ### for settings />   

I have tried a lot of other things, like using callbacks without success on the matter. Either I cannot set the line_settings, or the viewport misbehaves.

Any ideas?

johanjohan avatar Aug 11 '22 01:08 johanjohan

btw, the following warning appears always: v:\00shared\dev8\PY\python_code\3d\venv\lib\site-packages\pyglet\image\codecs\wic.py:406: UserWarning: [WinError -2147417850] Cannot change thread mode after it is set warnings.warn(str(err))

johanjohan avatar Aug 11 '22 01:08 johanjohan

I have switched the rendering over to pyrender and fixed it.

johanjohan avatar Aug 15 '22 15:08 johanjohan