1
$\begingroup$

I am trying to duplicate multiple objects resulting in the same location/rotation relative to each other. I can get my code to work for a single object but when multiple objects are used obviously the objects get transformed N times (N number of multiple objects). Is there a function that I am missing or a different approach?

import bpy
import bmesh

bm = bmesh.new()
for i, o in enumerate(bpy.context.selected_objects): 
    bm.from_mesh(o.data)
    bm.transform(o.matrix_world)
    
    
me = bpy.data.meshes.new("Mesh")
bm.to_mesh(me)
obj = bpy.data.objects.new("Object", me) # https://docs.blender.org/api/current/bmesh.ops.html
bpy.context.collection.objects.link(obj)

Objects to be copied Output Transformed with o.matrix_world

$\endgroup$

1 Answer 1

3
$\begingroup$

bm.transform transforms every vert.

Question code will be getting an accumulative effect of transforming every current vert in the bmesh by each objects world matrix.

Instead, only transform the newly "from_mesh'd" vertices

import bpy
import bmesh

bm = bmesh.new()

for i, o in enumerate(bpy.context.selected_objects): 
    bm.from_mesh(o.data)
    bmesh.ops.transform(
            bm,
            verts = bm.verts[-len(o.data.vertices):],
            matrix=o.matrix_world,
            )

            
me = bpy.data.meshes.new("Mesh")
bm.to_mesh(me)
obj = bpy.data.objects.new("Object", me) # https://docs.blender.org/api/current/bmesh.ops.html
bpy.context.collection.objects.link(obj)

Related

https://blender.stackexchange.com/a/149269/15543

$\endgroup$
1
  • $\begingroup$ Thanks a lot! I didn't know that you could apply the transforms to verts! Thanks again :) $\endgroup$ Sep 8, 2021 at 15:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .