diff --git a/addons/io_scene_gltf2/__init__.py b/addons/io_scene_gltf2/__init__.py index 3e8a96bfe..ffc2742dc 100644 --- a/addons/io_scene_gltf2/__init__.py +++ b/addons/io_scene_gltf2/__init__.py @@ -1771,9 +1771,13 @@ def unit_import(self, filename, import_settings): import time from .io.imp.gltf2_io_gltf import glTFImporter, ImportError from .blender.imp.gltf2_blender_gltf import BlenderGlTF + from .io.com.gltf2_io_constants import BLENDER_CUSTOM_PROP_BLACK_LIST + from .io.imp.gltf2_io_user_extensions import import_user_extensions try: gltf_importer = glTFImporter(filename, import_settings) + gltf_importer.import_settings['blender_custom_prop_black_list'] = BLENDER_CUSTOM_PROP_BLACK_LIST + import_user_extensions('gather_import_custom_prop_black_list', gltf_importer) gltf_importer.read() gltf_importer.checks() diff --git a/addons/io_scene_gltf2/blender/com/gltf2_blender_extras.py b/addons/io_scene_gltf2/blender/com/gltf2_blender_extras.py index 6c93e7b4d..067e611fa 100755 --- a/addons/io_scene_gltf2/blender/com/gltf2_blender_extras.py +++ b/addons/io_scene_gltf2/blender/com/gltf2_blender_extras.py @@ -17,11 +17,7 @@ from .gltf2_blender_json import is_json_convertible -# Custom properties, which are in most cases present and should not be imported/exported. -BLACK_LIST = ['cycles', 'cycles_visibility', 'cycles_curves', 'glTF2ExportSettings'] - - -def generate_extras(blender_element): +def generate_extras(blender_element, export_settings): """Filter and create a custom property, which is stored in the glTF extra field.""" if not blender_element: return None @@ -29,7 +25,7 @@ def generate_extras(blender_element): extras = {} for custom_property in blender_element.keys(): - if custom_property in BLACK_LIST: + if custom_property in export_settings['blender_custom_prop_black_list']: continue value = __to_json_compatible(blender_element[custom_property]) @@ -76,13 +72,13 @@ def __to_json_compatible(value): return None -def set_extras(blender_element, extras, exclude=[]): +def set_extras(blender_element, extras, import_settings, exclude=[]): """Copy extras onto a Blender object.""" if not extras or not isinstance(extras, dict): return for custom_property, value in extras.items(): - if custom_property in BLACK_LIST: + if custom_property in import_settings['blender_custom_prop_black_list']: continue if custom_property in exclude: continue diff --git a/addons/io_scene_gltf2/blender/exp/animation/fcurves/gltf2_blender_gather_fcurves_animation.py b/addons/io_scene_gltf2/blender/exp/animation/fcurves/gltf2_blender_gather_fcurves_animation.py index 2ba7e1df6..425c3f616 100644 --- a/addons/io_scene_gltf2/blender/exp/animation/fcurves/gltf2_blender_gather_fcurves_animation.py +++ b/addons/io_scene_gltf2/blender/exp/animation/fcurves/gltf2_blender_gather_fcurves_animation.py @@ -57,5 +57,5 @@ def __gather_channels_fcurves( def __gather_extras(blender_action, export_settings): if export_settings['gltf_extras']: - return generate_extras(blender_action) + return generate_extras(blender_action, export_settings) return None diff --git a/addons/io_scene_gltf2/blender/exp/animation/gltf2_blender_gather_action.py b/addons/io_scene_gltf2/blender/exp/animation/gltf2_blender_gather_action.py index 434ffaee3..d061f5feb 100644 --- a/addons/io_scene_gltf2/blender/exp/animation/gltf2_blender_gather_action.py +++ b/addons/io_scene_gltf2/blender/exp/animation/gltf2_blender_gather_action.py @@ -544,5 +544,5 @@ def __is_armature_action(blender_action) -> bool: def __gather_extras(blender_action, export_settings): if export_settings['gltf_extras']: - return generate_extras(blender_action) + return generate_extras(blender_action, export_settings) return None diff --git a/addons/io_scene_gltf2/blender/exp/animation/gltf2_blender_gather_scene_animation.py b/addons/io_scene_gltf2/blender/exp/animation/gltf2_blender_gather_scene_animation.py index e8abcab8c..57b9701e4 100644 --- a/addons/io_scene_gltf2/blender/exp/animation/gltf2_blender_gather_scene_animation.py +++ b/addons/io_scene_gltf2/blender/exp/animation/gltf2_blender_gather_scene_animation.py @@ -128,5 +128,5 @@ def gather_scene_animations(export_settings): def __gather_extras(blender_scene, export_settings): if export_settings['gltf_extras']: - return generate_extras(blender_scene) + return generate_extras(blender_scene, export_settings) return None diff --git a/addons/io_scene_gltf2/blender/exp/animation/sampled/armature/gltf2_blender_gather_armature_action_sampled.py b/addons/io_scene_gltf2/blender/exp/animation/sampled/armature/gltf2_blender_gather_armature_action_sampled.py index e263a6f71..06b89d93a 100644 --- a/addons/io_scene_gltf2/blender/exp/animation/sampled/armature/gltf2_blender_gather_armature_action_sampled.py +++ b/addons/io_scene_gltf2/blender/exp/animation/sampled/armature/gltf2_blender_gather_armature_action_sampled.py @@ -73,5 +73,5 @@ def __gather_channels(armature_uuid, blender_action_name, export_settings) -> ty def __gather_extras(blender_action, export_settings): if export_settings['gltf_extras']: - return generate_extras(blender_action) if blender_action else None + return generate_extras(blender_action, export_settings) if blender_action else None return None diff --git a/addons/io_scene_gltf2/blender/exp/animation/sampled/object/gltf2_blender_gather_object_action_sampled.py b/addons/io_scene_gltf2/blender/exp/animation/sampled/object/gltf2_blender_gather_object_action_sampled.py index 801fb4d11..b849115c3 100644 --- a/addons/io_scene_gltf2/blender/exp/animation/sampled/object/gltf2_blender_gather_object_action_sampled.py +++ b/addons/io_scene_gltf2/blender/exp/animation/sampled/object/gltf2_blender_gather_object_action_sampled.py @@ -54,5 +54,5 @@ def __gather_channels(object_uuid: str, blender_action_name: str, export_setting def __gather_extras(blender_action, export_settings): if export_settings['gltf_extras']: - return generate_extras(blender_action) if blender_action else None + return generate_extras(blender_action, export_settings) if blender_action else None return None diff --git a/addons/io_scene_gltf2/blender/exp/animation/sampled/shapekeys/gltf2_blender_gather_sk_action_sampled.py b/addons/io_scene_gltf2/blender/exp/animation/sampled/shapekeys/gltf2_blender_gather_sk_action_sampled.py index b44d86556..7f2c84ee4 100644 --- a/addons/io_scene_gltf2/blender/exp/animation/sampled/shapekeys/gltf2_blender_gather_sk_action_sampled.py +++ b/addons/io_scene_gltf2/blender/exp/animation/sampled/shapekeys/gltf2_blender_gather_sk_action_sampled.py @@ -54,5 +54,5 @@ def __gather_channels(object_uuid: str, blender_action_name: str, export_setting def __gather_extras(blender_action, export_settings): if export_settings['gltf_extras']: - return generate_extras(blender_action) if blender_action else None + return generate_extras(blender_action, export_settings) if blender_action else None return None diff --git a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather.py b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather.py index dd1bc02a3..f4c7da05e 100644 --- a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather.py +++ b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather.py @@ -124,5 +124,5 @@ def __gather_scene(blender_scene, export_settings): def __gather_extras(blender_object, export_settings): if export_settings['gltf_extras']: - return generate_extras(blender_object) + return generate_extras(blender_object, export_settings) return None diff --git a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_cameras.py b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_cameras.py index 66e3a91b6..4b83ad743 100644 --- a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_cameras.py +++ b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_cameras.py @@ -49,7 +49,7 @@ def __gather_extensions(blender_camera, export_settings): def __gather_extras(blender_camera, export_settings): if export_settings['gltf_extras']: - return generate_extras(blender_camera) + return generate_extras(blender_camera, export_settings) return None diff --git a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_joints.py b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_joints.py index f3c54fbc7..0686531cf 100644 --- a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_joints.py +++ b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_joints.py @@ -112,5 +112,5 @@ def gather_joint_vnode(vnode, export_settings): def __gather_extras(blender_bone, export_settings): if export_settings['gltf_extras']: - return generate_extras(blender_bone.bone) + return generate_extras(blender_bone.bone, export_settings) return None diff --git a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_lights.py b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_lights.py index eacdb59d2..cd9934bd7 100644 --- a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_lights.py +++ b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_lights.py @@ -129,7 +129,7 @@ def __gather_extensions(blender_lamp, export_settings) -> Optional[dict]: def __gather_extras(blender_lamp, export_settings) -> Optional[Any]: if export_settings['gltf_extras']: - return generate_extras(blender_lamp) + return generate_extras(blender_lamp, export_settings) return None diff --git a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_mesh.py b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_mesh.py index 71c8ffb41..bd009bf0c 100644 --- a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_mesh.py +++ b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_mesh.py @@ -114,7 +114,7 @@ def __gather_extras(blender_mesh: bpy.types.Mesh, extras = {} if export_settings['gltf_extras']: - extras = generate_extras(blender_mesh) or {} + extras = generate_extras(blender_mesh, export_settings) or {} if export_settings['gltf_morph'] and blender_mesh.shape_keys: morph_max = len(blender_mesh.shape_keys.key_blocks) - 1 diff --git a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_nodes.py b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_nodes.py index 92c7f4bc5..e2eb2063d 100644 --- a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_nodes.py +++ b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_nodes.py @@ -197,7 +197,7 @@ def __gather_extensions(blender_object, export_settings): def __gather_extras(blender_object, export_settings): if export_settings['gltf_extras']: - return generate_extras(blender_object) + return generate_extras(blender_object, export_settings) return None diff --git a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gltf2_exporter.py b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gltf2_exporter.py index c5bfdcff6..4c2b2e544 100644 --- a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gltf2_exporter.py +++ b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gltf2_exporter.py @@ -17,7 +17,7 @@ from typing import List from ... import get_version_string -from ...io.com import gltf2_io, gltf2_io_extensions +from ...io.com import gltf2_io, gltf2_io_extensions, gltf2_io_constants from ...io.com.gltf2_io_path import path_to_uri, uri_to_path from ...io.com.gltf2_io_constants import ComponentType, DataType from ...io.exp import gltf2_io_binary_data, gltf2_io_buffer, gltf2_io_image_data @@ -36,6 +36,10 @@ def __init__(self, export_settings): self.export_settings = export_settings self.__finalized = False + # Allow user to set a list of custom prop to ignore during export of extras + self.export_settings['blender_custom_prop_black_list'] = gltf2_io_constants.BLENDER_CUSTOM_PROP_BLACK_LIST + export_user_extensions('gather_custom_prop_black_list', export_settings) + copyright = export_settings['gltf_copyright'] or None asset = gltf2_io.Asset( copyright=copyright, diff --git a/addons/io_scene_gltf2/blender/exp/material/gltf2_blender_gather_materials.py b/addons/io_scene_gltf2/blender/exp/material/gltf2_blender_gather_materials.py index 69066b02c..0e3802dc0 100644 --- a/addons/io_scene_gltf2/blender/exp/material/gltf2_blender_gather_materials.py +++ b/addons/io_scene_gltf2/blender/exp/material/gltf2_blender_gather_materials.py @@ -221,7 +221,7 @@ def __gather_extensions(blender_material, emissive_factor, export_settings): def __gather_extras(blender_material, export_settings): if export_settings['gltf_extras']: - return generate_extras(blender_material) + return generate_extras(blender_material, export_settings) return None diff --git a/addons/io_scene_gltf2/blender/imp/gltf2_blender_camera.py b/addons/io_scene_gltf2/blender/imp/gltf2_blender_camera.py index 1bc31ca2a..321743bcc 100644 --- a/addons/io_scene_gltf2/blender/imp/gltf2_blender_camera.py +++ b/addons/io_scene_gltf2/blender/imp/gltf2_blender_camera.py @@ -33,7 +33,7 @@ def create(gltf, vnode, camera_id): pycamera.name = "Camera" cam = bpy.data.cameras.new(pycamera.name) - set_extras(cam, pycamera.extras) + set_extras(cam, pycamera.extras, gltf.import_settings) # Blender create a perspective camera by default if pycamera.type == "orthographic": diff --git a/addons/io_scene_gltf2/blender/imp/gltf2_blender_light.py b/addons/io_scene_gltf2/blender/imp/gltf2_blender_light.py index b742afbf1..0b37c98b1 100644 --- a/addons/io_scene_gltf2/blender/imp/gltf2_blender_light.py +++ b/addons/io_scene_gltf2/blender/imp/gltf2_blender_light.py @@ -43,7 +43,7 @@ def create(gltf, vnode, light_id): # TODO range - set_extras(light, pylight.get('extras')) + set_extras(light, pylight.get('extras'), gltf.import_settings) return light diff --git a/addons/io_scene_gltf2/blender/imp/gltf2_blender_material.py b/addons/io_scene_gltf2/blender/imp/gltf2_blender_material.py index 7858ec607..e5fd08c30 100644 --- a/addons/io_scene_gltf2/blender/imp/gltf2_blender_material.py +++ b/addons/io_scene_gltf2/blender/imp/gltf2_blender_material.py @@ -40,7 +40,7 @@ def create(gltf, material_idx, vertex_color): mat = bpy.data.materials.new(name) pymaterial.blender_material[vertex_color] = mat.name - set_extras(mat, pymaterial.extras) + set_extras(mat, pymaterial.extras, gltf.import_settings) BlenderMaterial.set_double_sided(pymaterial, mat) BlenderMaterial.set_alpha_mode(pymaterial, mat) BlenderMaterial.set_viewport_color(pymaterial, mat, vertex_color) diff --git a/addons/io_scene_gltf2/blender/imp/gltf2_blender_mesh.py b/addons/io_scene_gltf2/blender/imp/gltf2_blender_mesh.py index 33e9fa301..45b4ce56e 100644 --- a/addons/io_scene_gltf2/blender/imp/gltf2_blender_mesh.py +++ b/addons/io_scene_gltf2/blender/imp/gltf2_blender_mesh.py @@ -54,7 +54,7 @@ def create_mesh(gltf, mesh_idx, skin_idx): try: tmp_ob = bpy.data.objects.new('##gltf-import:tmp-object##', mesh) do_primitives(gltf, mesh_idx, skin_idx, mesh, tmp_ob) - set_extras(mesh, gltf.data.meshes[mesh_idx].extras, exclude=['targetNames']) + set_extras(mesh, gltf.data.meshes[mesh_idx].extras, gltf.import_settings, exclude=['targetNames']) finally: if tmp_ob: diff --git a/addons/io_scene_gltf2/blender/imp/gltf2_blender_node.py b/addons/io_scene_gltf2/blender/imp/gltf2_blender_node.py index 5eb1f782b..a739ca974 100644 --- a/addons/io_scene_gltf2/blender/imp/gltf2_blender_node.py +++ b/addons/io_scene_gltf2/blender/imp/gltf2_blender_node.py @@ -101,7 +101,7 @@ def create_object(gltf, vnode_id): # Set extras (if came from a glTF node) if isinstance(vnode_id, int): pynode = gltf.data.nodes[vnode_id] - set_extras(obj, pynode.extras) + set_extras(obj, pynode.extras, gltf.import_settings) # Set transform trans, rot, scale = vnode.trs() @@ -206,7 +206,7 @@ def visit(id): # Depth-first walk if isinstance(id, int): pynode = gltf.data.nodes[id] - set_extras(editbone, pynode.extras) + set_extras(editbone, pynode.extras, gltf.import_settings) # Set all bone parents for id in bone_ids: @@ -235,7 +235,7 @@ def visit(id): # Depth-first walk if isinstance(id, int): pynode = gltf.data.nodes[id] - set_extras(pose_bone, pynode.extras) + set_extras(pose_bone, pynode.extras, gltf.import_settings) if gltf.import_settings['bone_heuristic'] == "BLENDER": pose_bone.custom_shape = bpy.data.objects[gltf.bone_shape] diff --git a/addons/io_scene_gltf2/blender/imp/gltf2_blender_scene.py b/addons/io_scene_gltf2/blender/imp/gltf2_blender_scene.py index 1933a5d88..c5ba9a323 100644 --- a/addons/io_scene_gltf2/blender/imp/gltf2_blender_scene.py +++ b/addons/io_scene_gltf2/blender/imp/gltf2_blender_scene.py @@ -39,7 +39,7 @@ def create(gltf): if gltf.data.scene is not None: import_user_extensions('gather_import_scene_before_hook', gltf, gltf.data.scenes[gltf.data.scene], scene) pyscene = gltf.data.scenes[gltf.data.scene] - set_extras(scene, pyscene.extras) + set_extras(scene, pyscene.extras, gltf.import_settings) compute_vnodes(gltf) diff --git a/addons/io_scene_gltf2/io/com/gltf2_io_constants.py b/addons/io_scene_gltf2/io/com/gltf2_io_constants.py index c4f98f13f..9277c92a4 100644 --- a/addons/io_scene_gltf2/io/com/gltf2_io_constants.py +++ b/addons/io_scene_gltf2/io/com/gltf2_io_constants.py @@ -164,3 +164,6 @@ class BufferViewTarget(IntEnum): # Rounding digit used for normal/tangent rounding ROUNDING_DIGIT = 4 + +# List of custom properties to not import/export +BLENDER_CUSTOM_PROP_BLACK_LIST = ['cycles', 'cycles_visibility', 'cycles_curves', 'glTF2ExportSettings'] diff --git a/example-addons/example_gltf_exporter_extension/readme.md b/example-addons/example_gltf_exporter_extension/readme.md index fcb0a6e2f..5de96cccf 100644 --- a/example-addons/example_gltf_exporter_extension/readme.md +++ b/example-addons/example_gltf_exporter_extension/readme.md @@ -72,4 +72,5 @@ gather_animation_object_sampled_channel_target_hook(self, blender_object, channe gather_attribute_keep(self, keep_attribute, export_settings) gather_attribute_change(self, attribute, data, is_normalized_byte_color, export_settings) gather_attributes_change(self, attributes, export_settings) +gather_custom_prop_black_list(self, export_settings) ``` diff --git a/example-addons/example_gltf_importer_extension/readme.md b/example-addons/example_gltf_importer_extension/readme.md index e256b75d4..b46d310fb 100644 --- a/example-addons/example_gltf_importer_extension/readme.md +++ b/example-addons/example_gltf_importer_extension/readme.md @@ -50,4 +50,5 @@ gather_import_animation_weight_before_hook(self, gltf_node, blender_animation, g gather_import_animation_weight_after_hook(self, gltf_node, blender_animation, gltf) gather_import_decode_primitive(self, gltf_mesh, gltf_primitive, skin_idx, gltf) gather_import_gltf_before_hook(self, gltf) +gather_import_custom_prop_black_list(self, gltf) ```