Skip to content

Commit

Permalink
Try to make GUI work
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan Jose Casafranca committed Oct 10, 2019
1 parent f43f251 commit efc4757
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 84 deletions.
113 changes: 41 additions & 72 deletions addons/io_scene_gltf2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,26 +329,14 @@ def __init__(self):
for user_extension in gltf2_io_user_extensions.UserExtensionLoader().extensions
if 'name' in user_extension.meta
}
# Generate properties for each extension
user_extension_settings_defaults = {}

for extension_name, user_extension in user_extensions.items():
meta = user_extension.meta
if 'settings' in meta:
property_name = 'settings_' + meta['name']
property_group = type(property_name, (bpy.types.PropertyGroup,), meta['settings'])
bpy.utils.register_class(property_group)
# Dynamically add user extension settings as class attribute
locals()[property_name] = PointerProperty(type=property_group)
# Store defaults
user_extension_settings_defaults[extension_name] = {}
for key, prop_def in meta['settings'].items():
user_extension_settings_defaults[extension_name][prop_def[1]['name']] = prop_def[1]['default']

export_user_extensions = CollectionProperty(
name='User Extensions',
type=UserExtensionPropertyGroup,
description='Select extensions to enable'
)
if hasattr(user_extension, 'settings'):
print(user_extension.settings())
for property_name, user_extension_property in user_extension.settings().items():
print(extension_name)
print(property_name)
setattr(ExportGLTF2_Base, extension_name + "_property_" + property_name, user_extension_property)

# Custom scene property for saving settings
scene_key = "glTF2ExportSettings"
Expand Down Expand Up @@ -484,8 +472,8 @@ def draw(self, context):
self.draw_material_settings()
elif self.ui_tab == 'ANIMATION':
self.draw_animation_settings()
elif self.ui_tab == 'EXTENSIONS':
self.draw_user_extensions()
elif self.ui_tab == 'EXTENSIONS':
self.draw_user_extensions()

def draw_general_settings(self):
col = self.layout.box().column()
Expand Down Expand Up @@ -825,57 +813,27 @@ def draw(self, context):
layout.active = operator.export_skins
layout.prop(operator, 'export_all_influences')

def draw_user_extensions(self):
# Ensure we have built the user extension properties
self.generate_user_extensions_properties()

# Draw entries for each user extension
col = self.layout.box().column()
col.label(text='User Extensions:', icon='PLUGIN')

for user_extension_property in self.export_user_extensions:
user_extension = self.user_extensions[user_extension_property.name]
row = col.row()

row.prop(user_extension_property, 'enable', text=user_extension_property.name)

if user_extension.meta.get('isDraft', False):
row.prop(self, 'draft_prop', icon='ERROR', emboss=False)

info_op = row.operator('wm.url_open', icon='INFO', emboss=False)
info_op.url = user_extension.meta.get('url', '')

if user_extension_property.enable:
settings = getattr(self, 'settings_' + user_extension_property.name, None)
has_custom_draw_function = hasattr(user_extension, 'draw_settings')
if settings or has_custom_draw_function:
box = col.box()
if has_custom_draw_function:
user_extension.draw_settings(box, settings)
else:
setting_props = [
name for name in dir(settings)
if not name.startswith('_')
and name not in ('bl_rna', 'name', 'rna_type')
]
for setting_prop in setting_props:
box.prop(settings, setting_prop)


def generate_user_extensions_properties(self):
# Iterate over collection of properties
for user_extension_property in self.export_user_extensions:
# Update the enable state of the user_extension matching the property
user_extension = self.user_extensions[user_extension_property.name];
user_extension.meta['enable'] = user_extension_property.enable

# Clear Collection and re-add entries for all user extensions
self.export_user_extensions.clear()
for user_extension_name, user_extension in self.user_extensions.items():
prop = self.export_user_extensions.add()
prop.name = user_extension_name
prop.enable = user_extension.meta['enable']
class GLTF_PT_export_user_extensions(bpy.types.Panel):
bl_space_type = 'FILE_BROWSER'
bl_region_type = 'TOOL_PROPS'
bl_label = "Extensions"
bl_parent_id = "FILE_PT_operator"
bl_options = {'DEFAULT_CLOSED'}

@classmethod
def poll(cls, context):
sfile = context.space_data
operator = sfile.active_operator

return operator.bl_idname == "EXPORT_SCENE_OT_gltf"

def draw(self, context):
layout = self.layout
layout.use_property_split = True
layout.use_property_decorate = False # No animation.

sfile = context.space_data
operator = sfile.active_operator

class ExportGLTF2(bpy.types.Operator, ExportGLTF2_Base, ExportHelper):
"""Export scene as glTF 2.0 file"""
Expand Down Expand Up @@ -987,14 +945,25 @@ def menu_func_import(self, context):
GLTF_PT_export_animation_export,
GLTF_PT_export_animation_shapekeys,
GLTF_PT_export_animation_skinning,
GLTF_PT_export_user_extensions,
ImportGLTF2
)


def register():
for c in classes:
bpy.utils.register_class(c)
# bpy.utils.register_module(__name__)


user_extensions = {
# Record name and new instance for each extension
user_extension.meta['name'] : user_extension()
for user_extension in gltf2_io_user_extensions.UserExtensionLoader().extensions
if 'name' in user_extension.meta
} # bpy.utils.register_module(__name__)

for name, user_extension in user_extensions.items():
user_extension.registerExtension()

# add to the export / import menu
if bpy.app.version < (2, 80, 0):
Expand Down
54 changes: 42 additions & 12 deletions addons/io_scene_gltf2/io/exp/extensions/example_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,36 @@
from io_scene_gltf2.io.exp.gltf2_io_user_extensions import UserExtensionBase
from io_scene_gltf2.io.com.gltf2_io_extensions import Extension



class GLTF_PT_export_example_extension(bpy.types.Panel):
bl_space_type = 'FILE_BROWSER'
bl_region_type = 'TOOL_PROPS'
bl_label = "Example Extension"
bl_parent_id = "GLTF_PT_export_user_extensions"
bl_options = {'DEFAULT_CLOSED'}

def __init__(self):
from io_scene_gltf2.io.exp import gltf2_io_draco_compression_extension
self.is_draco_available = gltf2_io_draco_compression_extension.dll_exists()

@classmethod
def poll(cls, context):
sfile = context.space_data
operator = sfile.active_operator
if operator.is_draco_available:
return operator.bl_idname == "EXPORT_SCENE_OT_gltf"

def draw_header(self, context):
sfile = context.space_data
operator = sfile.active_operator
#self.layout.prop(operator, "ExampleExtension_property_ExampleExtension_enabled", text="")

def draw(self, context):
layout = self.layout
layout.use_property_split = True
layout.use_property_decorate = False # No animation.

class ExampleExtension (UserExtensionBase):

# As this user extension lives in the 'user_extensions/extensions' folder it is dynamically loaded.
Expand All @@ -14,19 +44,19 @@ class ExampleExtension (UserExtensionBase):
'isDraft': False,
'enable': True,
'url': 'https://www.kdab.com',
'settings': {
"ExampleBoolProperty": bpy.props.BoolProperty(
name='ExampleBoolProperty',
description='This is an example of a BoolProperty used by a UserExtension.',
default=True
),
"ExampleStringProperty": bpy.props.StringProperty(
name='ExampleStringProperty',
description='This is an example of a StringProperty used by a UserExtension.',
default="My String"
)
}
}

export_ExampleExtension_enabled = bpy.props.BoolProperty(
name='ExampleExtension_enabled',
description='This is an example of a BoolProperty used by a UserExtension.',
default=True
)

def export(self, blender_object, export_settings) -> Extension:
return None

def registerExtension(self):
bpy.utils.register_class(GLTF_PT_export_example_extension)

def settings(self):
return {"ExampleExtension_enabled": self.export_ExampleExtension_enabled}
1 change: 1 addition & 0 deletions addons/io_scene_gltf2/io/exp/gltf2_io_user_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import imp
import importlib
import os.path
import bpy
from io_scene_gltf2.io.com.gltf2_io_extensions import Extension

class GLTF2ExtensionMesh:
Expand Down

0 comments on commit efc4757

Please sign in to comment.