I really hope this is an easy question. I literally searched for hours now and I just cannot figure this out. I simplified my (quite extensive) code to these lines that throw the error:
you take a selected object and unlink it from its collection.
then you try to link it to some other collection.
before you call the unlink operation, there is a selected object.
but when you have called the unlink operation your object is gone. Basically its deleted, thatâs what you have done.
and when you try to link it to the other collection, you try to link âNULLâ where an object is expected.
thatâs why you get the runtime error.
and the 2nd issue, simply is a Blender feature.
you cannot switch modes when nothingâs there.
start your Blender with its default scene. if you have any objects placed in it delete each of them.
on the blank scene try to switch the mode and you end up exactly at the same point.
Solution.
to copy something from one Collection to an other there is a nice builtin function for.
Which moves all selected objects to a given Collection.
collection_index = (default= -1) counting from top to bottom. 0 = Scene Collection. 1 = user collection, 2 = âŚ
is_new = (default = False) want to create a new collection
new_collection_name = (default = ââ) the Collectionâs name if you choosed to create a new one.
first of all: this forum is awesome! I never got replies so quickly. Really appreciated!
Thanks, londo, will try this out asap!
Just some things I want to understand:
The link op actually works when I run the code. So, the object disappears and then reappears in the new collection. It then is still displayed as selected in the Layer View. It is displayed in that darker orange color indicating it is selected but not active. This is probably not intended then.
Also, I thought collections are just grouping views on objects and unlinking objects from them is like removing a user from them, keeping the object, at least in this âbefore-purgeâ userless state.
I did not consider the object as âremovedâ because it was literally displayed and selectable after linking it to the collection. Maybe this is not the intended behavior then?
Whatever, thanks a lot. I think I know what is going on now. Will try this out asap (weekend is family time ).
when you unlink the object itâs not going to be recognized by the Blender context anymore.
What in fact is the core layer for user interactions.
But the object itself is still in Blenderâs data structure and you can work with it but not by using the context path.
you would have to take the data path to properly access the object.
bpy.data.objects[âAnyObjectNameâ]
use this instead of
bpy.context.selected_objects[0]
after youâd properly linked it up, you end up with the object reassigned to the given collection. and as you said, itâll be highlighted in a darker orange.
what simply is a selected object but not the active one.
so, basically youâd do:
select the object
unlink it
link it
make active object
essentially what â.move_to_collectionâ provides you anyway.
at last, why you can link the object anyway.
well, you store the selected_object in a variable and use this to do some actions.
in general any action youâd carry out will affect the original data itself but does not affect the data stored in your variable.
as long as you do not delete, overwrite or reassign the variable or the instance as long you can work with the values in it, even if the original data has changed. thatâs why you can link the object up again.
well, advices like this make me just shake my head. Itâs like to give you a car and wish you a nice ride but have the whole gear train removed in the first place.
bpy.ops.mesh.primitive_cube_add()
to avoid .ops, would not even let you add a simple cube to your scene, u know.