0
$\begingroup$

I'm new to the Blender API, so I've been doing a lot of playing around with it. I'm having some issues trying to do something that should be simple.

My goal: make a copy of a "base" object, enter edit mode, rotate it by 90 degrees on the Y axis (so it's on its side), and then put the new edited object into the "base shape" collection.

My problem: When I get the object and try to rotate it, it affects all of the other objects in the "base shape" collection.

My function:

def make_side_arch():
base = C.collection.children['Base Shapes'].objects['Semi_Arch']

ob = base.copy()
ob.data = base.data.copy()

bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.transform.rotate(value=radians(90), orient_axis='Y')
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.object.mode_set(mode='OBJECT')

ob.name = 'Side_Arch'
ob.data.name = 'Side_Arch'
ob.data = ob.data.copy()

C.collection.children['Base Shapes'].objects.link(ob)
return ob

In the screenshot below, I am trying to apply it to the objects called Semi_Arch. (I'll attach the blend file at the end). It seems to be affecting all the other shapes in the file though, and I'm struggling to understand why. I know I'm probably in the wrong context or something, but it's been really, really confusing wrapping my head around it. Any help is greatly appreciated!

I know people say to avoid bpy.ops, but I don't know how to do most things without it.

enter image description here

$\endgroup$

1 Answer 1

0
$\begingroup$

Staying away from ops is a good idea, since they are context-dependent. However, if everything is working, it's ok to use them. In your case, you have to make sure your ob is active and selected before using them. I don't see that in your code. If you don't mind them in the first place, I think the quickest fix would be, to replace

ob = base.data.copy()

with:

bpy.ops.object.duplicate()
ob = bpy.context.active_object

But make sure the correct base object is active and selected.

$\endgroup$
2
  • $\begingroup$ What would be the best way to do that without ops? I'd like to make it a habit of not using ops when possible, just to make things faster for me down the road. I was able to get my script working to an extent... except that I forgot that the object origin is on the bottom face, so when the objects get rotated they're no long on the same level... so if you have a fix for that as well, that would be great, ha. I tried entering edit mode, but it doesn't seem to work. $\endgroup$
    – n0ah
    Commented Aug 3, 2022 at 6:24
  • $\begingroup$ Something along the line of make a mesh bmesh, or regular from .data or .data.copy(), then use matrix multiplication on each vertex. blender.stackexchange.com/questions/102917/… $\endgroup$ Commented Aug 3, 2022 at 9:53

You must log in to answer this question.

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