Skip to content

Commit

Permalink
Split meshswap block enum into block and method
Browse files Browse the repository at this point in the history
This removes the string concat which is prone to errors, and had at least one error report: -Mw6vXKxlj_b3pou5GOR per #261 listed as -MgRXi1HSdOt7R23ZAZx
  • Loading branch information
TheDuckCow committed Apr 22, 2022
1 parent f3f2934 commit 01aea32
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 20 deletions.
2 changes: 2 additions & 0 deletions MCprep_addon/mcprep_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -1119,8 +1119,10 @@ def meshswap_spawner(self, context):
if scn_props.meshswap_list:
name = scn_props.meshswap_list[scn_props.meshswap_list_index].name
block = scn_props.meshswap_list[scn_props.meshswap_list_index].block
method = scn_props.meshswap_list[scn_props.meshswap_list_index].method
p = row.operator("mcprep.meshswap_spawner", text="Place: " + name)
p.block = block
p.method = method
p.location = util.get_cuser_location(context)
else:
row.operator("mcprep.meshswap_spawner", text="Place block")
Expand Down
34 changes: 25 additions & 9 deletions MCprep_addon/spawner/meshswap.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ def updateMeshswapList(context):
meshswap_list = []

cache = get_meshswap_cache(context)
prefix = "Group/" if hasattr(bpy.data, "groups") else "Collection/"
method = "collection"

for name in cache.get("groups"):
if not name:
continue
if util.nameGeneralize(name).lower() in temp_meshswap_list:
continue
description = "Place {x} block".format(x=name)
meshswap_list.append((prefix + name, name.title(), description))
meshswap_list.append((method, name, name.title(), description))
temp_meshswap_list.append(util.nameGeneralize(name).lower())

# sort the list alphabetically by name
Expand All @@ -150,9 +150,10 @@ def updateMeshswapList(context):
context.scene.mcprep_props.meshswap_list.clear()
for itm in sorted_blocks:
item = context.scene.mcprep_props.meshswap_list.add()
item.block = itm[0]
item.name = itm[1]
item.description = itm[2]
item.method = itm[0]
item.block = itm[1]
item.name = itm[2]
item.description = itm[3]


class face_struct():
Expand Down Expand Up @@ -194,6 +195,14 @@ def swap_enum(self, context):
return getMeshswapList(context)

block = bpy.props.EnumProperty(items=swap_enum, name="Meshswap block")
method = bpy.props.EnumProperty(
name="Import method",
items=[
# Making collection first to be effective default.
("collection", "Collection/group asset", "Collection/group asset"),
("object", "Object asset", "Object asset"),
],
options={'HIDDEN'})
location = bpy.props.FloatVectorProperty(
default=(0, 0, 0),
name="Location")
Expand Down Expand Up @@ -237,19 +246,26 @@ def execute(self, context):
pre_groups = list(util.collections())

meshSwapPath = bpy.path.abspath(context.scene.meshswap_path)
method, block = self.block.split("/")
block = self.block
method = self.method
if method == "collection":
is_object = False
method = "Group" if hasattr(bpy.data, "groups") else "Collection"
elif method == "object":
is_object = True
method = "Object"
link = False
use_cache = False
group = None

if method in ('Collection', 'Group') and block in util.collections():
if not is_object and block in util.collections():
group = util.collections()[block]
# if blender 2.8, see if collection part of the MCprepLib coll.
use_cache = True
else:
util.bAppendLink(os.path.join(meshSwapPath, method), block, link)

if method == "Object":
if is_object:
# Object-level disabled! Must have better
# asset management for this to work
for ob in bpy.context.selected_objects:
Expand Down Expand Up @@ -358,7 +374,7 @@ def execute(self, context):
if util.bv28() and group:
util.move_assets_to_excluded_layer(context, [group])

self.track_param = self.block
self.track_param = "{}/{}".format(self.method, self.block)
return {'FINISHED'}

def prep_collection(self, context, block, pre_groups):
Expand Down
10 changes: 9 additions & 1 deletion MCprep_addon/spawner/spawn_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,15 @@ class ListMobAssets(bpy.types.PropertyGroup):

class ListMeshswapAssets(bpy.types.PropertyGroup):
"""For UI drawing of meshswap assets and holding data"""
block = bpy.props.StringProperty() # virtual enum, Group/name
block = bpy.props.StringProperty() # block name only like "fire"
method = bpy.props.EnumProperty(
name="Import method",
# Collection intentionally first to be default for operator calls.
items=[
("collection", "Collection/group asset", "Collection/group asset"),
("object", "Object asset", "Object asset"),
]
)
description = bpy.props.StringProperty()


Expand Down
31 changes: 21 additions & 10 deletions test_files/addon_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1031,46 +1031,57 @@ def meshswap_spawner(self):

if bpy.app.version >= (2, 80):
# Add with make real = False
bpy.ops.mcprep.meshswap_spawner(block='Collection/banner', make_real=False)
bpy.ops.mcprep.meshswap_spawner(
block='banner', method="collection", make_real=False)

# test doing two of the same one (first won't be cached, second will)
# Add one with make real = True
bpy.ops.mcprep.meshswap_spawner(block='Collection/fire', make_real=True)
bpy.ops.mcprep.meshswap_spawner(
block='fire', method="collection", make_real=True)
if 'fire' not in bpy.data.collections:
return "Fire not in collections"
elif not bpy.context.selected_objects:
return "Added made-real meshswap objects not selected"

bpy.ops.mcprep.meshswap_spawner(block='Collection/fire', make_real=False)
# Test that cache is properly used. Also test that the default
# 'method=colleciton' is used, since that's the only mode of
# support for meshswap spawner at the moment.
bpy.ops.mcprep.meshswap_spawner(block='fire', make_real=False)
if 'fire' not in bpy.data.collections:
return "Fire not in collections"
count_torch = sum([1 for itm in bpy.data.collections if 'fire' in itm.name])
if count_torch != 1:
count = sum([1 for itm in bpy.data.collections if 'fire' in itm.name])
if count != 1:
return "Imported extra fire group, should have cached instead!"

# test that added item ends up in location location=(1,2,3)
loc = (1, 2, 3)
bpy.ops.mcprep.meshswap_spawner(
block='Collection/fire', make_real=False, location=loc)
block='fire', method="collection", make_real=False, location=loc)
if not bpy.context.object:
return "Added meshswap object not added as active"
elif not bpy.context.selected_objects:
return "Added meshswap object not selected"
if bpy.context.object.location != Vector(loc):
return "Location not properly applied"
count = sum([1 for itm in bpy.data.collections if 'fire' in itm.name])
if count != 1:
return "Should have 1 fire groups exactly, did not cache"
else:
# Add with make real = False
bpy.ops.mcprep.meshswap_spawner(block='Group/banner', make_real=False)
bpy.ops.mcprep.meshswap_spawner(
block='banner', method="collection", make_real=False)

# test doing two of the same one (first won't be cached, second will)
# Add one with make real = True
bpy.ops.mcprep.meshswap_spawner(block='Group/fire', make_real=True)
bpy.ops.mcprep.meshswap_spawner(
block='fire', method="collection", make_real=True)
if 'fire' not in bpy.data.groups:
return "Fire not in groups"
elif not bpy.context.selected_objects:
return "Added made-real meshswap objects not selected"

bpy.ops.mcprep.meshswap_spawner(block='Group/fire', make_real=False)
bpy.ops.mcprep.meshswap_spawner(
block='fire', method="collection", make_real=False)
if 'fire' not in bpy.data.groups:
return "Fire not in groups"
count_torch = sum([1 for itm in bpy.data.groups if 'fire' in itm.name])
Expand All @@ -1080,7 +1091,7 @@ def meshswap_spawner(self):
# test that added item ends up in location location=(1,2,3)
loc = (1, 2, 3)
bpy.ops.mcprep.meshswap_spawner(
block='Group/fire', make_real=False, location=loc)
block='fire', method="collection", make_real=False, location=loc)
if not bpy.context.object:
return "Added meshswap object not added as active"
elif not bpy.context.selected_objects:
Expand Down

0 comments on commit 01aea32

Please sign in to comment.