active ob on layer 1?

normal layer is 0 but
if I add a primitive circle on layer 1

I cannot select it with

obj_act = bpy.context.active_object
obj_act.name=“1-1”

is there a way to select active ob and rename it after adding it on layer 1 ?

thanks

If layer 0 is the only visible layer, and you add a mesh primitive using bpy.ops to layer 1, the new object will nonetheless become the active object. Hence, you can get a grip on the object using bpy.context.object and rename it without trouble.

very strange here

I did another script and it works fine!



    
import bpy
import bmesh
import mathutils
from math import *
from bpy.props import *
import random
    
    
layers = [False]*20
layers[0] = True
layers[1] = True
    

layers1 =layers
    
    
    
    
# Add small circle on top  near Handle
    
cx,cy,cz= 12,10,0
#        myCircleMesh =bpy.ops.mesh.primitive_circle_add(vertices=seg1, radius=d2/2.0, view_align=False, enter_editmode=False, location=(cx,cy,cz), rotation=(0, 0, 0), layers=[True]+[False]*19)
    
myCircleMesh =bpy.ops.mesh.primitive_circle_add(vertices=32, radius=20, view_align=False, enter_editmode=False, location=(cx,cy,cz), rotation=(0, 0, 0), layers=layers1)
    
obj_act = bpy.context.active_object
print (' active ob =',obj_act )
obj_act.name="2-10"
    
    


but when I use this in my big script it does not work !

I 'm inside addmesh function also using Bmesh
is there any constraints to use this ?

thanks

I reckon it doesn’t work in your script because you do some operation between the creation via bpy.ops and obj_act = bpy.context.object.

But anyway, if you use bmesh module, why don’t you use bmesh.ops.create_circle() and get a direct reference? Do it low level, so you don’t need to rely on context at all.

these circles are only working lines
and I would like to add these on another layer
so that you can choose to see these or not

I could move these out of the bmesh loop

but it is strange that it works if I add it on layer 0 but not on layer 1!

as soon as I add it on layer 1 when I try to change name
it gives error :NONE type for the ob active !

I can make short version of script with error if needed

thanks

I believe it is because selection of an object can not occur unless that object is visible thus the bpy.ops creation routine presents a different state. Your code is kind of making an assumption on a return state but as we know bpy.ops has no return value.

We need to differentiate here: “not visible” is ambiguous, it can mean invisible in the viewport, because it’s on a layer which is not enabled, as well as invisible because it is hidden. If it’s really hidden, then it can’t be the active object, try it:

Select an object in viewpot and see what bpy.context.object is (it is the object you made active). Now hit H to hide it and check bpy.context.object again - it’s now None!

If you create a mesh primitive through bpy.ops on a layer, which isn’t currently enabled, the context object will be this new object despite it not being visible to the user.

There may be more circumstances under which an object is created, but not made the active object, 'cause it doesn’t make sense (most operators operate on the active object, but you wouldn’t want to edit an object you can’t see!)

what is the command to make ob invisible = equivalent to H key I think

I made a smaller version

try this

adderror1.blend (573 KB)

run it from Add mesh = shape

and it will give error on select ob

I’m adding circle on layer 1
but normal it is layer 0 which I visible and active I think!

so are you saying that if u add ob on layer 1 which is not visible it will still select the ob?

thanks

Try

obj_act = bpy.context.object

That works for me.

ok what is the difference ?

I also found another way to make it work
just have to enable scene layer 1 and 0 visible
then it works !

thanks