From 76efb0ed473d6559e4e986625d7893c97b994720 Mon Sep 17 00:00:00 2001 From: Swann Martinez Date: Fri, 3 May 2019 18:52:49 +0200 Subject: [PATCH] feat(helpers): fix mesh material loading --- cache_Armature.001.json | 1 - client.py | 2 +- helpers.py | 89 ++++++++++++++++++++++++++++------------- operators.py | 32 ++++++++------- 4 files changed, 80 insertions(+), 44 deletions(-) delete mode 100644 cache_Armature.001.json diff --git a/cache_Armature.001.json b/cache_Armature.001.json deleted file mode 100644 index 331465b..0000000 --- a/cache_Armature.001.json +++ /dev/null @@ -1 +0,0 @@ -{"use_deform_delay": false, "show_names": false, "is_dirty": false, "is_evaluated": false, "tag": false, "pose_position": "POSE", "bones": {}, "id": "user_IM3xRe", "layers": [true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], "use_auto_ik": false, "show_bone_custom_shapes": true, "show_group_colors": true, "is_editmode": false, "users": 0, "name_full": "Armature.001", "use_mirror_x": false, "display_type": "OCTAHEDRAL", "layers_protected": [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], "use_fake_user": false, "name": "Armature.001", "is_library_indirect": false, "show_axes": false, "edit_bones": {}, "original": {"use_deform_delay": false, "show_names": false, "is_dirty": false, "is_evaluated": false, "tag": false, "pose_position": "POSE", "bones": {}, "id": "user_IM3xRe", "layers": [true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], "use_auto_ik": false, "show_bone_custom_shapes": true, "show_group_colors": true, "is_editmode": false, "users": 0, "name_full": "Armature.001", "use_mirror_x": false, "display_type": "OCTAHEDRAL", "layers_protected": [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], "use_fake_user": false, "name": "Armature.001", "is_library_indirect": false, "show_axes": false, "edit_bones": {}, "original": {"use_deform_delay": false, "show_names": false, "is_dirty": false, "is_evaluated": false, "tag": false, "pose_position": "POSE", "bones": {}, "id": "user_IM3xRe", "layers": [true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], "use_auto_ik": false, "show_bone_custom_shapes": true, "show_group_colors": true, "is_editmode": false, "users": 0, "name_full": "Armature.001", "use_mirror_x": false, "display_type": "OCTAHEDRAL", "layers_protected": [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false], "use_fake_user": false, "name": "Armature.001", "is_library_indirect": false, "show_axes": false, "edit_bones": {}, "original": {"is_editmode": false, "name": "Armature.001", "is_library_indirect": false, "use_auto_ik": false, "use_mirror_x": false, "name_full": "Armature.001", "use_deform_delay": false, "show_bone_custom_shapes": true, "original": "Armature.001", "show_names": false, "is_dirty": false, "is_evaluated": false, "show_group_colors": true, "display_type": "OCTAHEDRAL", "pose_position": "POSE", "show_axes": false, "tag": false, "id": "user_IM3xRe", "use_fake_user": false, "users": 0}}}} \ No newline at end of file diff --git a/client.py b/client.py index 8225e5d..6817af0 100644 --- a/client.py +++ b/client.py @@ -84,7 +84,7 @@ class RCFClient(object): # Sync Watchdog self.watchdog_agent = threading.Thread( - target=watchdog_worker, args=(self.serial_feed, 2, self.stop_event), name="watchdog-agent") + target=watchdog_worker, args=(self.serial_feed, 0.2, self.stop_event), name="watchdog-agent") self.watchdog_agent.daemon = True self.watchdog_agent.start() diff --git a/helpers.py b/helpers.py index 94189cb..f2e0956 100644 --- a/helpers.py +++ b/helpers.py @@ -18,10 +18,16 @@ SUPPORTED_TYPES = [ 'Armature', 'Material', logger = logging.getLogger(__name__) logging.basicConfig(level=logging.DEBUG) # UTILITY FUNCTIONS - +def revers(d): + l = [] + for i in d: + l.append(i) + + return l[::-1] def refresh_window(): import bpy + bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1) def get_armature_edition_context(armature): @@ -102,7 +108,7 @@ def load(key, value): load_default(target=target, data=value, create=True, type=target_type) elif target_type == 'Armature': - preload_armature(target=target, data=value, + load_armature(target=target, data=value, create=True) elif target_type == 'Client': load_client(key.split('/')[1], value) @@ -134,10 +140,12 @@ def load_client(client=None, data=None): draw.renderer.draw_client(data) draw.renderer.draw_client_selected_objects(data) + def load_armature(target=None, data=None, create=False): file = "cache_{}.json".format(data['name']) + context = bpy.context - if not target or not target.is_editmode: + if not target: target = bpy.data.armatures.new(data['name']) dump_anything.load(target, data) @@ -148,32 +156,43 @@ def load_armature(target=None, data=None, create=False): target.id = data['id'] else: - # Construct a correct execution context - context = get_armature_edition_context(target) + # Construct a correct execution context file = "cache_{}.json".format(target.name) with open(file, 'r') as fp: data = json.load(fp) if data: - for b in data['bones']: - if b in target.edit_bones.keys(): - # Update the bone - pass - else: - # Add new edit bone and load it - bpy.ops.armature.bone_primitive_add(context) + ob = None + for o in bpy.data.objects: + if o.data == target: + ob = o + if ob: + bpy.context.view_layer.objects.active = ob + bpy.ops.object.mode_set(mode='EDIT', toggle=False) + for eb in data['edit_bones']: + if eb in target.edit_bones.keys(): + # Update the bone + pass + else: + # Add new edit bone and load it - target_new_b = target.bones[b] - dump_anything.load(target_new_b, data['bones'][b]) + target_new_b = target.edit_bones.new[eb] + dump_anything.load(target_new_b, data['bones'][eb]) + + logger.info(eb) + + bpy.ops.object.mode_set(mode='OBJECT', toggle=False) + fp.close() + import os + os.remove(file) - logger.info(b) def load_mesh(target=None, data=None, create=False): import bmesh if not target or not target.is_editmode: - # TODO: handle error + # LOAD GEOMETRY mesh_buffer = bmesh.new() for i in data["vertices"]: @@ -193,16 +212,29 @@ def load_mesh(target=None, data=None, create=False): verts.append(mesh_buffer.verts[v]) if len(verts) > 0: - mesh_buffer.faces.new(verts) + f = mesh_buffer.faces.new(verts) + f.material_index= data["polygons"][p]['material_index'] + if target is None and create: target = bpy.data.meshes.new(data["name"]) mesh_buffer.to_mesh(target) - - # Load other meshes metadata + + # LOAD METADATA dump_anything.load(target, data) + material_to_load = [] + material_to_load = revers(data["materials"]) + + target.materials.clear() + # SLots + i = 0 + while len(material_to_load) > len(target.materials): + target.materials.append(bpy.data.materials[material_to_load[i]]) + i+=1 + + target.id = data['id'] else: logger.debug("Mesh can't be loaded") @@ -244,10 +276,6 @@ def load_object(target=None, data=None, create=False): target.hide_select = False else: target.hide_select = True - - #Post load - if pointer and pointer.__class__.__name__ == 'Armature': - load_armature(pointer) except Exception as e: logger.error("Object {} loading error: {} ".format(data["name"],e)) @@ -450,8 +478,10 @@ def dump(key): elif target_type == 'Light': data = dump_datablock(target, 1) elif target_type == 'Mesh': - data = dump_datablock_attibute( - target, ['name', 'polygons', 'edges', 'vertices', 'id'], 6) + data = dump_datablock(target, 2) + dump_datablock_attibute( + target, ['name', 'polygons', 'edges', 'vertices', 'id'], 6,data) + elif target_type == 'Object': data = dump_datablock(target, 1) elif target_type == 'Collection': @@ -459,8 +489,8 @@ def dump(key): elif target_type == 'Scene': data = dump_datablock_attibute( target, ['name', 'collection', 'id', 'camera', 'grease_pencil'], 4) - elif target_type == 'Armature': - data = dump_datablock(target, 4) + # elif target_type == 'Armature': + # data = dump_datablock(target, 4) return data @@ -479,7 +509,7 @@ def dump_datablock(datablock, depth): return data -def dump_datablock_attibute(datablock, attributes, depth=1): +def dump_datablock_attibute(datablock, attributes, depth=1,dickt=None): if datablock: dumper = dump_anything.Dumper() dumper.type_subset = dumper.match_subset_all @@ -489,6 +519,9 @@ def dump_datablock_attibute(datablock, attributes, depth=1): key = "{}/{}".format(datablock_type, datablock.name) data = {} + + if dickt: + data = dickt for attr in attributes: try: data[attr] = dumper.dump(getattr(datablock, attr)) diff --git a/operators.py b/operators.py index 14efad4..8ef8001 100644 --- a/operators.py +++ b/operators.py @@ -498,22 +498,26 @@ def exist(update): def depsgraph_update(scene): global client_instance global client_keys + global client_state + - if client_instance and client_instance.net_agent.is_alive(): - updates = bpy.context.depsgraph.updates - username = bpy.context.scene.session_settings.username - update_selected_object(bpy.context) + if client_state == 3: + if bpy.context.mode == 'OBJECT': + updates = bpy.context.depsgraph.updates + username = bpy.context.scene.session_settings.username + update_selected_object(bpy.context) + + selected_objects = helpers.get_selected_objects(scene) + # if len(selected_objects) > 0: + # for updated_data in updates: + # if updated_data.id.name in selected_objects: + # if updated_data.is_updated_transform or updated_data.is_updated_geometry: + # client_instance.set( + # "{}/{}".format(updated_data.id.bl_rna.name, updated_data.id.name)) + for update in reversed(updates): + if update.id.id == username: + getattr(bpy.data, helpers.CORRESPONDANCE[update.id.__class__.__name__])[update.id.name].is_dirty= True - selected_objects = helpers.get_selected_objects(scene) - # if len(selected_objects) > 0: - # for updated_data in updates: - # if updated_data.id.name in selected_objects: - # if updated_data.is_updated_transform or updated_data.is_updated_geometry: - # client_instance.set( - # "{}/{}".format(updated_data.id.bl_rna.name, updated_data.id.name)) - for update in reversed(updates): - if update.id.id == username: - getattr(bpy.data, helpers.CORRESPONDANCE[update.id.__class__.__name__])[update.id.name].is_dirty= True def register(): from bpy.utils import register_class