1
$\begingroup$

I created a city scene which has a vehicle model inside, I need to duplicate the vehicle model to display a dozen of them in the city scene through python API. The vehicle object name is BMWM1. I have tried on these two approaches enter image description here Approach 1 from stackoverflow

scn = bpy.context.scene
new_obj = bpy.data.objects["BMWM1"].copy()
new_obj.data = bpy.data.objects["BMWM1"].data.copy()
new_obj.animation_data_clear()
scn.objects.link(new_obj)
new_obj.location = (-8, 6, 2)
scn.objects.active = new_obj
new_obj.select = True

Approach 2

bpy.data.objects['BMWM1'].select = True
new_obj = bpy.ops.object.duplicate()
scn.objects.link(new_obj)
new_obj.location = (-8, 6, 2)
scn.objects.active = new_obj
new_obj.select = True

Both approaches received a error saying

"warning DM_ensure_tessface: could not create tessfaces from 144 polygons, dm->type=2 warning DM_ensure_tessface: could not create tessfaces from 144 polygons, dm->type=2 "

And the second approach additionally received a error:

" scn.objects.link(new_obj) TypeError: SceneObjects.link(): error with argument 1, "object" - Function.object expected a Object type, not set "

I have also seen the tutorials like Three ways to create objects, two ways to create a new object

Add the data, and then the object. For a mesh: 

me = bpy.data.meshes.new(meshName)
ob = bpy.data.objects.new(obName, me)

and for an armature:

amt = bpy.data.armatures.new(amtname)
ob = bpy.data.objects.new(obname, amt)

While I tried another way

me = bpy.data.objects["BMWM1"].data;
me_copy = me.copy()
new_obj = bpy.data.objects.new("BMWM1",me_copy)

scn.objects.link(new_obj)
new_obj.location = (-8, 6, 2)
scn.objects.active = new_obj
new_obj.select = True

but still at the end the error

"warning DM_ensure_tessface: could not create tessfaces from 144 polygons, dm->type=2 warning DM_ensure_tessface: could not create tessfaces from 144 polygons, dm->type=2 "

happened again

I tried the duplicate again as commented by

bpy.data.objects['BMWM1'].select = True
set = bpy.ops.object.duplicate()

for new_obj in set: 
  scn.objects.link(new_obj)

still produced a error

scn.objects.link(new_obj) TypeError: SceneObjects.link(): error with argument 1, "object" - Function.object expected a Object type, not str

$\endgroup$
4
  • $\begingroup$ The return value of bpy.ops.object.duplicate() is not the object that was duplicated by the result of the op, which is a set. That is why you are getting an error while linking to the scene. $\endgroup$ Commented Jun 8, 2017 at 3:00
  • $\begingroup$ I tried another way, set = bpy.ops.object.duplicate(), for new_obj in set , but still has a error scn.objects.link(new_obj) TypeError: SceneObjects.link(): error with argument 1, "object" - Function.object expected a Object type, not str $\endgroup$ Commented Jun 8, 2017 at 5:22
  • $\begingroup$ Okay, I forgot to add. The op does not return the name of the objects created. You will have to find another way to get the duplicated object. $\endgroup$ Commented Jun 8, 2017 at 5:53
  • 2
    $\begingroup$ Operator return values Personally I'd go with your "approach 1", using bpy.data.objects.new(...). Is the tessface error similar to the one outlined here? $\endgroup$ Commented Jun 8, 2017 at 8:55

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.