refactor: cleanup code

Related to #18
This commit is contained in:
Swann Martinez
2019-11-28 11:25:53 +01:00
parent dedc5a04e0
commit 67b9e1a8cf
+46 -32
View File
@@ -7,74 +7,85 @@ from .. import presence, operators
from .bl_datablock import BlDatablock
# WIP
class BlArmature(BlDatablock):
def construct(self, data):
return bpy.data.armatures.new(data["name"])
def load(self, data, target):
# Load parent object
parent_object = utils.find_from_attr('uuid',data['user'],bpy.data.objects)
if parent_object is None:
parent_object = bpy.data.objects.new(data['user_name'], self.pointer)
parent_object.uuid = data['user']
parent_object = utils.find_from_attr(
'uuid',
data['user'],
bpy.data.objects
)
is_object_in_master = (data['user_collection'][0] == "Master Collection")
#TODO: recursive parent collection loading
if parent_object is None:
parent_object = bpy.data.objects.new(
data['user_name'], self.pointer)
parent_object.uuid = data['user']
is_object_in_master = (
data['user_collection'][0] == "Master Collection")
# TODO: recursive parent collection loading
# Link parent object to the collection
if is_object_in_master:
parent_collection = bpy.data.scenes[data['user_scene'][0]].collection
elif data['user_collection'][0] not in bpy.data.collections.keys():
parent_collection = bpy.data.collections.new(data['user_collection'][0])
parent_collection = bpy.data.scenes[data['user_scene']
[0]].collection
elif data['user_collection'][0] not in bpy.data.collections.keys():
parent_collection = bpy.data.collections.new(
data['user_collection'][0])
else:
parent_collection = bpy.data.collections[data['user_collection'][0]]
parent_collection = bpy.data.collections[data['user_collection'][0]]
if parent_object.name not in parent_collection.objects:
parent_collection.objects.link(parent_object)
# Link parent collection to the scene master collection
if not is_object_in_master and parent_collection.name not in bpy.data.scenes[data['user_scene'][0]].collection.children:
bpy.data.scenes[data['user_scene'][0]].collection. children.link(parent_collection)
bpy.data.scenes[data['user_scene'][0]
].collection. children.link(parent_collection)
current_mode = bpy.context.mode
current_active_object = bpy.context.view_layer.objects.active
# LOAD ARMATURE BONES
if bpy.context.mode != 'OBJECT':
bpy.ops.object.mode_set(mode='OBJECT')
bpy.context.view_layer.objects.active = parent_object
bpy.context.view_layer.objects.active = parent_object
bpy.ops.object.mode_set(mode='EDIT')
print(bpy.context.mode)
for bone in data['bones']:
if bone not in self.pointer.edit_bones:
new_bone = self.pointer.edit_bones.new(bone)
else:
new_bone = self.pointer.edit_bones[bone]
new_bone.tail = data['bones'][bone]['tail_local']
new_bone.head = data['bones'][bone]['head_local']
new_bone.head = data['bones'][bone]['head_local']
new_bone.tail_radius = data['bones'][bone]['tail_radius']
new_bone.head_radius = data['bones'][bone]['head_radius']
if 'parent' in data['bones'][bone]:
new_bone.parent = self.pointer.edit_bones[data['bones'][bone]['use_connect']['name']]
new_bone.parent = self.pointer.edit_bones[data['bones']
[bone]['use_connect']['name']]
new_bone.use_connect = data['bones'][bone]['use_connect']
if bpy.context.mode != 'OBJECT':
bpy.ops.object.mode_set(mode='OBJECT')
bpy.context.view_layer.objects.active = current_active_object
# TODO: clean way to restore previous context
if 'EDIT' in current_mode :
bpy.ops.object.mode_set(mode='EDIT')
# TODO: clean way to restore previous context
if 'EDIT' in current_mode:
bpy.ops.object.mode_set(mode='EDIT')
def dump_implementation(self, data, pointer=None):
assert(pointer)
dumper = utils.dump_anything.Dumper()
dumper.depth = 3
dumper.include_filter = [
dumper.include_filter = [
'bones',
'tail_local',
'head_local',
@@ -86,15 +97,17 @@ class BlArmature(BlDatablock):
]
data = dumper.dump(pointer)
#get the parent Object
# get the parent Object
object_users = utils.get_datablock_users(pointer)[0]
data['user'] = object_users.uuid
data['user_name'] = object_users.name
#get parent collection
# get parent collection
container_users = utils.get_datablock_users(object_users)
data['user_collection'] = [item.name for item in container_users if isinstance(item,bpy.types.Collection)]
data['user_scene'] = [item.name for item in container_users if isinstance(item,bpy.types.Scene)]
data['user_collection'] = [
item.name for item in container_users if isinstance(item, bpy.types.Collection)]
data['user_scene'] = [
item.name for item in container_users if isinstance(item, bpy.types.Scene)]
return data
def resolve(self):
@@ -104,10 +117,11 @@ class BlArmature(BlDatablock):
def is_valid(self):
return bpy.data.armatures.get(self.data['name'])
bl_id = "armatures"
bl_class = bpy.types.Armature
bl_rep_class = BlArmature
bl_delay_refresh = 1
bl_delay_apply = 0
bl_automatic_push = True
bl_icon = 'ARMATURE_DATA'
bl_icon = 'ARMATURE_DATA'