Skip to content

Commit

Permalink
fixes spam in collection creation (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
BradyAJohnston authored Jan 6, 2025
1 parent 0e7a0e0 commit f17b026
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
7 changes: 3 additions & 4 deletions databpy/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,11 @@ def create_collection(
if type(parent) not in [Collection, str, type(None)]:
raise TypeError("Parent must be a Collection, string or None")

coll = _get_collection(name)
if parent is None:
return coll

if isinstance(parent, str):
parent = _get_collection(parent)

coll = _get_collection(name)
if parent is None:
return coll
parent.children.link(coll)
return coll
4 changes: 2 additions & 2 deletions databpy/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,8 @@ def create_object(
mesh = bpy.data.meshes.new(name)
mesh.from_pydata(vertices=vertices, edges=edges, faces=faces)
obj = bpy.data.objects.new(name, mesh)
if not collection:
collection = create_collection("")
if collection is None:
collection = create_collection("Collection")
collection.objects.link(obj)
return obj

Expand Down
11 changes: 11 additions & 0 deletions tests/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,19 @@ def test_collection_missing():
db.collection.create_collection("Collection")


def test_collection_spam():
n_coll = len(list(bpy.data.collections.keys()))
for _ in range(10):
coll = db.collection.create_collection("Collection")
assert coll.name == "Collection"
bob = db.create_bob()
assert n_coll == len(list(bpy.data.collections.keys()))


def test_collection():
assert "Collection" in bpy.data.collections
coll = db.collection.create_collection("Example", parent="Collection")
assert "Collection.001" not in bpy.data.collections
assert coll.name == "Example"
assert coll.name in bpy.data.collections
assert coll.name in bpy.data.collections["Collection"].children

0 comments on commit f17b026

Please sign in to comment.