To_mesh and creating new object issues?

OK, after a couple days of trying I have no idea how to get this to work. I know there is similar threads ,but still can’t figure this out from reading them.

How do I update this to work for 2.8?

ground = context.object
sc = context.scene


def transform_ground_to_world(sc, ground):
    tmpMesh = ground.to_mesh(sc, True, 'PREVIEW')
    tmpMesh.transform(ground.matrix_world)
    tmp_ground = bpy.data.objects.new('tmpGround', tmpMesh)
    sc.objects.link(tmp_ground)
    sc.update()
    return tmp_ground

This is kinda what I have so far. Having issues with the “to_mesh” line and “new object” line.

ground = context.object
layer = context.view_layer

def transform_ground_to_world(layer, ground):    
    tmpMesh = ground.to_mesh(preserve_all_data_layers=False, depsgraph= (what goes here ?????))    
    tmpMesh.transform(ground.matrix_world)
    tmp_ground = bpy.data.objects.new(name='tmpGround', object_data=tmpMesh)
    layer.active_layer_collection.collection.objects.link(tmp_ground)
    layer.update()
    return tmp_ground

No matter what I try to do on the “to_mesh” line it throws errors on the “new object” line.

Have you tried bpy.context.evaluated_depsgraph_get() to retrieve a Depsgraph?

How do I use it, LOL. I have tried all kinds of stuff.

This is the error I get mostly.

RuntimeError: Error: Can not create object in main database with an evaluated data data-block

Using bmesh you could do something like this:

import bpy
import bmesh

ground = bpy.context.object
layer = bpy.context.view_layer

def transform_ground_to_world(layer, ground):
    bm = bmesh.new()
    bm.from_object(ground, bpy.context.evaluated_depsgraph_get())
    tmp_ground_data=bpy.data.meshes.new(name='Mesh')
    bm.to_mesh(tmp_ground_data)
    tmp_ground = bpy.data.objects.new(name='tmpGround', object_data=tmp_ground_data)
    bm.transform(ground.matrix_world)
    bm.clear()
    bm.free()
    layer.active_layer_collection.collection.objects.link(tmp_ground)
    layer.update()
    return tmp_ground

transform_ground_to_world(layer, ground)

@RainerTrummer

OK, I think I had it before. I thought it was doing something different than the old version ,but seem to do the same.

def transform_ground_to_world(layer, ground):
	depsgraph = bpy.context.evaluated_depsgraph_get()
	object_eval = ground.evaluated_get(depsgraph)
	tmpMesh = bpy.data.meshes.new_from_object(object_eval)        
	tmpMesh.transform(ground.matrix_world)
	tmp_ground = bpy.data.objects.new(name='tmpGround', object_data=tmpMesh)
	layer.active_layer_collection.collection.objects.link(tmp_ground)
	layer.update()
	return tmp_ground
2 Likes