Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom prop blacklist - WIP #1917

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
4 changes: 4 additions & 0 deletions addons/io_scene_gltf2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
12 changes: 4 additions & 8 deletions addons/io_scene_gltf2/blender/com/gltf2_blender_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,15 @@
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

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])
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion addons/io_scene_gltf2/blender/exp/gltf2_blender_gather.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion addons/io_scene_gltf2/blender/imp/gltf2_blender_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
2 changes: 1 addition & 1 deletion addons/io_scene_gltf2/blender/imp/gltf2_blender_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion addons/io_scene_gltf2/blender/imp/gltf2_blender_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions addons/io_scene_gltf2/blender/imp/gltf2_blender_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion addons/io_scene_gltf2/blender/imp/gltf2_blender_scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 3 additions & 0 deletions addons/io_scene_gltf2/io/com/gltf2_io_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
1 change: 1 addition & 0 deletions example-addons/example_gltf_exporter_extension/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```
1 change: 1 addition & 0 deletions example-addons/example_gltf_importer_extension/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```