CM3D2 Converter.mate_import

  1import os
  2import re
  3import struct
  4import shutil
  5import bpy
  6from . import common
  7from . import compat
  8from . import cm3d2_data
  9from .translations.pgettext_functions import *
 10
 11
 12@compat.BlRegister()
 13class CNV_OT_import_cm3d2_mate(bpy.types.Operator):
 14    bl_idname = 'material.import_cm3d2_mate'
 15    bl_label = "mateを開く"
 16    bl_description = "mateファイルをマテリアルとして開きます"
 17    bl_options = {'REGISTER', 'UNDO'}
 18
 19    filepath = bpy.props.StringProperty(subtype='FILE_PATH')
 20    filename_ext = ".mate"
 21    filter_glob = bpy.props.StringProperty(default="*.mate", options={'HIDDEN'})
 22
 23    is_decorate = bpy.props.BoolProperty(name="種類に合わせてマテリアルを装飾", default=True)
 24    is_replace_cm3d2_tex = bpy.props.BoolProperty(name="テクスチャを探す", default=True, description="CM3D2本体のインストールフォルダからtexファイルを探して開きます")
 25
 26    @classmethod
 27    def poll(cls, context):
 28        if hasattr(context, 'material_slot'):
 29            if hasattr(context, 'material'):
 30                return True
 31        return False
 32
 33    def invoke(self, context, event):
 34        prefs = common.preferences()
 35        if prefs.mate_default_path:
 36            self.filepath = common.default_cm3d2_dir(prefs.mate_default_path, None, "mate")
 37        else:
 38            self.filepath = common.default_cm3d2_dir(prefs.mate_import_path, None, "mate")
 39        context.window_manager.fileselect_add(self)
 40        return {'RUNNING_MODAL'}
 41
 42    def draw(self, context):
 43        prefs = common.preferences()
 44        if compat.IS_LEGACY:
 45            self.layout.prop(self, 'is_decorate', icon=compat.icon('SHADING_TEXTURE'))
 46        self.layout.prop(prefs, 'is_replace_cm3d2_tex', icon='BORDERMOVE')
 47
 48    def execute(self, context):
 49        prefs = common.preferences()
 50        prefs.mate_import_path = self.filepath
 51
 52        try:
 53            file = open(self.filepath, 'rb')
 54        except:
 55            self.report(type={'ERROR'}, message=f_tip_("ファイルを開くのに失敗しました、アクセス不可かファイルが存在しません。file={}", self.filepath))
 56            return {'CANCELLED'}
 57
 58        try:
 59            with file:
 60                mat_data = cm3d2_data.MaterialHandler.read(file)
 61
 62        except Exception as e:
 63            self.report(type={'ERROR'}, message="mateファイルのインポートを中止します。" + str(e))
 64            return {'CANCELLED'}
 65
 66        if not context.material_slot:
 67            bpy.ops.object.material_slot_add()
 68        root, ext = os.path.splitext(os.path.basename(self.filepath))
 69        mate = context.blend_data.materials.new(name=mat_data.name)
 70        context.material_slot.material = mate
 71        common.setup_material(mate)
 72
 73        if compat.IS_LEGACY:
 74            cm3d2_data.MaterialHandler.apply_to_old(context, mate, mat_data, prefs.is_replace_cm3d2_tex, self.is_decorate, prefs.mate_unread_same_value)
 75        else:
 76            cm3d2_data.MaterialHandler.apply_to(context, mate, mat_data, prefs.is_replace_cm3d2_tex)
 77
 78        return {'FINISHED'}
 79
 80
 81@compat.BlRegister()
 82class CNV_OT_import_cm3d2_mate_text(bpy.types.Operator):
 83    bl_idname = 'text.import_cm3d2_mate_text'
 84    bl_label = "mateを開く"
 85    bl_description = "mateファイルをテキストとして開きます"
 86    bl_options = {'REGISTER', 'UNDO'}
 87
 88    filepath = bpy.props.StringProperty(subtype='FILE_PATH')
 89    filename_ext = ".mate"
 90    filter_glob = bpy.props.StringProperty(default="*.mate", options={'HIDDEN'})
 91
 92    is_overwrite = bpy.props.BoolProperty(name="現在のテキストに上書き", default=False)
 93
 94    @classmethod
 95    def poll(cls, context):
 96        return True
 97
 98    def invoke(self, context, event):
 99        prefs = common.preferences()
100        if prefs.mate_default_path:
101            self.filepath = common.default_cm3d2_dir(prefs.mate_default_path, None, "mate")
102        else:
103            self.filepath = common.default_cm3d2_dir(prefs.mate_import_path, None, "mate")
104        context.window_manager.fileselect_add(self)
105        return {'RUNNING_MODAL'}
106
107    def draw(self, context):
108        self.layout.prop(self, 'is_overwrite', icon='SAVE_COPY')
109
110    def execute(self, context):
111        prefs = common.preferences()
112        prefs.mate_import_path = self.filepath
113
114        edit_text = None
115        if self.is_overwrite:
116            edit_text = getattr(context, 'edit_text')
117            if not edit_text:
118                self.report(type={'ERROR'}, message="上書きする為のテキストデータが見つかりません")
119                return {'CANCELLED'}
120            edit_text.clear()
121
122        try:
123            file = open(self.filepath, 'rb')
124        except:
125            self.report(type={'ERROR'}, message=f_tip_("ファイルを開くのに失敗しました、アクセス不可かファイルが存在しません。file={}", self.filepath))
126            return {'CANCELLED'}
127
128        try:
129            with file:
130                mat_data = cm3d2_data.MaterialHandler.read(file)
131
132        except Exception as e:
133            self.report(type={'ERROR'}, message="mateファイルのインポートを中止します。" + str(e))
134            return {'CANCELLED'}
135
136        if not context.material_slot:
137            bpy.ops.object.material_slot_add()
138        root, ext = os.path.splitext(os.path.basename(self.filepath))
139        mate = context.blend_data.materials.new(name=mat_data.name)
140        context.material_slot.material = mate
141        common.setup_material(mate)
142
143        if compat.IS_LEGACY:
144            cm3d2_data.MaterialHandler.apply_to_old(context, mate, mat_data, prefs.is_replace_cm3d2_tex, self.is_decorate, prefs.mate_unread_same_value)
145        else:
146            cm3d2_data.MaterialHandler.apply_to(context, mate, mat_data, prefs.is_replace_cm3d2_tex)
147
148        if not edit_text:
149            edit_text = context.blend_data.texts.new(os.path.basename(mat_data.name))
150            context.area.type = 'TEXT_EDITOR'
151            context.space_data.text = edit_text
152
153        edit_text.write(mat_data.to_text())
154        edit_text.current_line_index = 0
155        return {'FINISHED'}
156
157
158# テキストメニューに項目を登録
159def TEXT_MT_text(self, context):
160    self.layout.separator()
161    self.layout.operator(CNV_OT_import_cm3d2_mate_text.bl_idname, icon_value=common.kiss_icon())
@compat.BlRegister()
class CNV_OT_import_cm3d2_mate(bpy_types.Operator):
13@compat.BlRegister()
14class CNV_OT_import_cm3d2_mate(bpy.types.Operator):
15    bl_idname = 'material.import_cm3d2_mate'
16    bl_label = "mateを開く"
17    bl_description = "mateファイルをマテリアルとして開きます"
18    bl_options = {'REGISTER', 'UNDO'}
19
20    filepath = bpy.props.StringProperty(subtype='FILE_PATH')
21    filename_ext = ".mate"
22    filter_glob = bpy.props.StringProperty(default="*.mate", options={'HIDDEN'})
23
24    is_decorate = bpy.props.BoolProperty(name="種類に合わせてマテリアルを装飾", default=True)
25    is_replace_cm3d2_tex = bpy.props.BoolProperty(name="テクスチャを探す", default=True, description="CM3D2本体のインストールフォルダからtexファイルを探して開きます")
26
27    @classmethod
28    def poll(cls, context):
29        if hasattr(context, 'material_slot'):
30            if hasattr(context, 'material'):
31                return True
32        return False
33
34    def invoke(self, context, event):
35        prefs = common.preferences()
36        if prefs.mate_default_path:
37            self.filepath = common.default_cm3d2_dir(prefs.mate_default_path, None, "mate")
38        else:
39            self.filepath = common.default_cm3d2_dir(prefs.mate_import_path, None, "mate")
40        context.window_manager.fileselect_add(self)
41        return {'RUNNING_MODAL'}
42
43    def draw(self, context):
44        prefs = common.preferences()
45        if compat.IS_LEGACY:
46            self.layout.prop(self, 'is_decorate', icon=compat.icon('SHADING_TEXTURE'))
47        self.layout.prop(prefs, 'is_replace_cm3d2_tex', icon='BORDERMOVE')
48
49    def execute(self, context):
50        prefs = common.preferences()
51        prefs.mate_import_path = self.filepath
52
53        try:
54            file = open(self.filepath, 'rb')
55        except:
56            self.report(type={'ERROR'}, message=f_tip_("ファイルを開くのに失敗しました、アクセス不可かファイルが存在しません。file={}", self.filepath))
57            return {'CANCELLED'}
58
59        try:
60            with file:
61                mat_data = cm3d2_data.MaterialHandler.read(file)
62
63        except Exception as e:
64            self.report(type={'ERROR'}, message="mateファイルのインポートを中止します。" + str(e))
65            return {'CANCELLED'}
66
67        if not context.material_slot:
68            bpy.ops.object.material_slot_add()
69        root, ext = os.path.splitext(os.path.basename(self.filepath))
70        mate = context.blend_data.materials.new(name=mat_data.name)
71        context.material_slot.material = mate
72        common.setup_material(mate)
73
74        if compat.IS_LEGACY:
75            cm3d2_data.MaterialHandler.apply_to_old(context, mate, mat_data, prefs.is_replace_cm3d2_tex, self.is_decorate, prefs.mate_unread_same_value)
76        else:
77            cm3d2_data.MaterialHandler.apply_to(context, mate, mat_data, prefs.is_replace_cm3d2_tex)
78
79        return {'FINISHED'}
bl_idname = 'material.import_cm3d2_mate'
bl_label = 'mateを開く'
bl_description = 'mateファイルをマテリアルとして開きます'
bl_options = {'REGISTER', 'UNDO'}
filepath: <_PropertyDeferred, <built-in function StringProperty>, {'subtype': 'FILE_PATH', 'attr': 'filepath'}> = <_PropertyDeferred, <built-in function StringProperty>, {'subtype': 'FILE_PATH', 'attr': 'filepath'}>
filename_ext = '.mate'
filter_glob: <_PropertyDeferred, <built-in function StringProperty>, {'default': '*.mate', 'options': {'HIDDEN'}, 'attr': 'filter_glob'}> = <_PropertyDeferred, <built-in function StringProperty>, {'default': '*.mate', 'options': {'HIDDEN'}, 'attr': 'filter_glob'}>
is_decorate: <_PropertyDeferred, <built-in function BoolProperty>, {'name': '種類に合わせてマテリアルを装飾', 'default': True, 'attr': 'is_decorate'}> = <_PropertyDeferred, <built-in function BoolProperty>, {'name': '種類に合わせてマテリアルを装飾', 'default': True, 'attr': 'is_decorate'}>
is_replace_cm3d2_tex: <_PropertyDeferred, <built-in function BoolProperty>, {'name': 'テクスチャを探す', 'default': True, 'description': 'CM3D2本体のインストールフォルダからtexファイルを探して開きます', 'attr': 'is_replace_cm3d2_tex'}> = <_PropertyDeferred, <built-in function BoolProperty>, {'name': 'テクスチャを探す', 'default': True, 'description': 'CM3D2本体のインストールフォルダからtexファイルを探して開きます', 'attr': 'is_replace_cm3d2_tex'}>
@classmethod
def poll(cls, context):
27    @classmethod
28    def poll(cls, context):
29        if hasattr(context, 'material_slot'):
30            if hasattr(context, 'material'):
31                return True
32        return False
def invoke(self, context, event):
34    def invoke(self, context, event):
35        prefs = common.preferences()
36        if prefs.mate_default_path:
37            self.filepath = common.default_cm3d2_dir(prefs.mate_default_path, None, "mate")
38        else:
39            self.filepath = common.default_cm3d2_dir(prefs.mate_import_path, None, "mate")
40        context.window_manager.fileselect_add(self)
41        return {'RUNNING_MODAL'}
def draw(self, context):
43    def draw(self, context):
44        prefs = common.preferences()
45        if compat.IS_LEGACY:
46            self.layout.prop(self, 'is_decorate', icon=compat.icon('SHADING_TEXTURE'))
47        self.layout.prop(prefs, 'is_replace_cm3d2_tex', icon='BORDERMOVE')
def execute(self, context):
49    def execute(self, context):
50        prefs = common.preferences()
51        prefs.mate_import_path = self.filepath
52
53        try:
54            file = open(self.filepath, 'rb')
55        except:
56            self.report(type={'ERROR'}, message=f_tip_("ファイルを開くのに失敗しました、アクセス不可かファイルが存在しません。file={}", self.filepath))
57            return {'CANCELLED'}
58
59        try:
60            with file:
61                mat_data = cm3d2_data.MaterialHandler.read(file)
62
63        except Exception as e:
64            self.report(type={'ERROR'}, message="mateファイルのインポートを中止します。" + str(e))
65            return {'CANCELLED'}
66
67        if not context.material_slot:
68            bpy.ops.object.material_slot_add()
69        root, ext = os.path.splitext(os.path.basename(self.filepath))
70        mate = context.blend_data.materials.new(name=mat_data.name)
71        context.material_slot.material = mate
72        common.setup_material(mate)
73
74        if compat.IS_LEGACY:
75            cm3d2_data.MaterialHandler.apply_to_old(context, mate, mat_data, prefs.is_replace_cm3d2_tex, self.is_decorate, prefs.mate_unread_same_value)
76        else:
77            cm3d2_data.MaterialHandler.apply_to(context, mate, mat_data, prefs.is_replace_cm3d2_tex)
78
79        return {'FINISHED'}
bl_rna = <bpy_struct, Struct("MATERIAL_OT_import_cm3d2_mate")>
Inherited Members
bpy_types.Operator
as_keywords
poll_message_set
builtins.bpy_struct
keys
values
items
get
pop
as_pointer
keyframe_insert
keyframe_delete
driver_add
driver_remove
is_property_set
property_unset
is_property_hidden
is_property_readonly
is_property_overridable_library
property_overridable_library_set
path_resolve
path_from_id
type_recast
bl_rna_get_subclass_py
bl_rna_get_subclass
id_properties_ensure
id_properties_clear
id_properties_ui
id_data
@compat.BlRegister()
class CNV_OT_import_cm3d2_mate_text(bpy_types.Operator):
 82@compat.BlRegister()
 83class CNV_OT_import_cm3d2_mate_text(bpy.types.Operator):
 84    bl_idname = 'text.import_cm3d2_mate_text'
 85    bl_label = "mateを開く"
 86    bl_description = "mateファイルをテキストとして開きます"
 87    bl_options = {'REGISTER', 'UNDO'}
 88
 89    filepath = bpy.props.StringProperty(subtype='FILE_PATH')
 90    filename_ext = ".mate"
 91    filter_glob = bpy.props.StringProperty(default="*.mate", options={'HIDDEN'})
 92
 93    is_overwrite = bpy.props.BoolProperty(name="現在のテキストに上書き", default=False)
 94
 95    @classmethod
 96    def poll(cls, context):
 97        return True
 98
 99    def invoke(self, context, event):
100        prefs = common.preferences()
101        if prefs.mate_default_path:
102            self.filepath = common.default_cm3d2_dir(prefs.mate_default_path, None, "mate")
103        else:
104            self.filepath = common.default_cm3d2_dir(prefs.mate_import_path, None, "mate")
105        context.window_manager.fileselect_add(self)
106        return {'RUNNING_MODAL'}
107
108    def draw(self, context):
109        self.layout.prop(self, 'is_overwrite', icon='SAVE_COPY')
110
111    def execute(self, context):
112        prefs = common.preferences()
113        prefs.mate_import_path = self.filepath
114
115        edit_text = None
116        if self.is_overwrite:
117            edit_text = getattr(context, 'edit_text')
118            if not edit_text:
119                self.report(type={'ERROR'}, message="上書きする為のテキストデータが見つかりません")
120                return {'CANCELLED'}
121            edit_text.clear()
122
123        try:
124            file = open(self.filepath, 'rb')
125        except:
126            self.report(type={'ERROR'}, message=f_tip_("ファイルを開くのに失敗しました、アクセス不可かファイルが存在しません。file={}", self.filepath))
127            return {'CANCELLED'}
128
129        try:
130            with file:
131                mat_data = cm3d2_data.MaterialHandler.read(file)
132
133        except Exception as e:
134            self.report(type={'ERROR'}, message="mateファイルのインポートを中止します。" + str(e))
135            return {'CANCELLED'}
136
137        if not context.material_slot:
138            bpy.ops.object.material_slot_add()
139        root, ext = os.path.splitext(os.path.basename(self.filepath))
140        mate = context.blend_data.materials.new(name=mat_data.name)
141        context.material_slot.material = mate
142        common.setup_material(mate)
143
144        if compat.IS_LEGACY:
145            cm3d2_data.MaterialHandler.apply_to_old(context, mate, mat_data, prefs.is_replace_cm3d2_tex, self.is_decorate, prefs.mate_unread_same_value)
146        else:
147            cm3d2_data.MaterialHandler.apply_to(context, mate, mat_data, prefs.is_replace_cm3d2_tex)
148
149        if not edit_text:
150            edit_text = context.blend_data.texts.new(os.path.basename(mat_data.name))
151            context.area.type = 'TEXT_EDITOR'
152            context.space_data.text = edit_text
153
154        edit_text.write(mat_data.to_text())
155        edit_text.current_line_index = 0
156        return {'FINISHED'}
bl_idname = 'text.import_cm3d2_mate_text'
bl_label = 'mateを開く'
bl_description = 'mateファイルをテキストとして開きます'
bl_options = {'REGISTER', 'UNDO'}
filepath: <_PropertyDeferred, <built-in function StringProperty>, {'subtype': 'FILE_PATH', 'attr': 'filepath'}> = <_PropertyDeferred, <built-in function StringProperty>, {'subtype': 'FILE_PATH', 'attr': 'filepath'}>
filename_ext = '.mate'
filter_glob: <_PropertyDeferred, <built-in function StringProperty>, {'default': '*.mate', 'options': {'HIDDEN'}, 'attr': 'filter_glob'}> = <_PropertyDeferred, <built-in function StringProperty>, {'default': '*.mate', 'options': {'HIDDEN'}, 'attr': 'filter_glob'}>
is_overwrite: <_PropertyDeferred, <built-in function BoolProperty>, {'name': '現在のテキストに上書き', 'default': False, 'attr': 'is_overwrite'}> = <_PropertyDeferred, <built-in function BoolProperty>, {'name': '現在のテキストに上書き', 'default': False, 'attr': 'is_overwrite'}>
@classmethod
def poll(cls, context):
95    @classmethod
96    def poll(cls, context):
97        return True
def invoke(self, context, event):
 99    def invoke(self, context, event):
100        prefs = common.preferences()
101        if prefs.mate_default_path:
102            self.filepath = common.default_cm3d2_dir(prefs.mate_default_path, None, "mate")
103        else:
104            self.filepath = common.default_cm3d2_dir(prefs.mate_import_path, None, "mate")
105        context.window_manager.fileselect_add(self)
106        return {'RUNNING_MODAL'}
def draw(self, context):
108    def draw(self, context):
109        self.layout.prop(self, 'is_overwrite', icon='SAVE_COPY')
def execute(self, context):
111    def execute(self, context):
112        prefs = common.preferences()
113        prefs.mate_import_path = self.filepath
114
115        edit_text = None
116        if self.is_overwrite:
117            edit_text = getattr(context, 'edit_text')
118            if not edit_text:
119                self.report(type={'ERROR'}, message="上書きする為のテキストデータが見つかりません")
120                return {'CANCELLED'}
121            edit_text.clear()
122
123        try:
124            file = open(self.filepath, 'rb')
125        except:
126            self.report(type={'ERROR'}, message=f_tip_("ファイルを開くのに失敗しました、アクセス不可かファイルが存在しません。file={}", self.filepath))
127            return {'CANCELLED'}
128
129        try:
130            with file:
131                mat_data = cm3d2_data.MaterialHandler.read(file)
132
133        except Exception as e:
134            self.report(type={'ERROR'}, message="mateファイルのインポートを中止します。" + str(e))
135            return {'CANCELLED'}
136
137        if not context.material_slot:
138            bpy.ops.object.material_slot_add()
139        root, ext = os.path.splitext(os.path.basename(self.filepath))
140        mate = context.blend_data.materials.new(name=mat_data.name)
141        context.material_slot.material = mate
142        common.setup_material(mate)
143
144        if compat.IS_LEGACY:
145            cm3d2_data.MaterialHandler.apply_to_old(context, mate, mat_data, prefs.is_replace_cm3d2_tex, self.is_decorate, prefs.mate_unread_same_value)
146        else:
147            cm3d2_data.MaterialHandler.apply_to(context, mate, mat_data, prefs.is_replace_cm3d2_tex)
148
149        if not edit_text:
150            edit_text = context.blend_data.texts.new(os.path.basename(mat_data.name))
151            context.area.type = 'TEXT_EDITOR'
152            context.space_data.text = edit_text
153
154        edit_text.write(mat_data.to_text())
155        edit_text.current_line_index = 0
156        return {'FINISHED'}
bl_rna = <bpy_struct, Struct("TEXT_OT_import_cm3d2_mate_text")>
Inherited Members
bpy_types.Operator
as_keywords
poll_message_set
builtins.bpy_struct
keys
values
items
get
pop
as_pointer
keyframe_insert
keyframe_delete
driver_add
driver_remove
is_property_set
property_unset
is_property_hidden
is_property_readonly
is_property_overridable_library
property_overridable_library_set
path_resolve
path_from_id
type_recast
bl_rna_get_subclass_py
bl_rna_get_subclass
id_properties_ensure
id_properties_clear
id_properties_ui
id_data
def TEXT_MT_text(self, context):
160def TEXT_MT_text(self, context):
161    self.layout.separator()
162    self.layout.operator(CNV_OT_import_cm3d2_mate_text.bl_idname, icon_value=common.kiss_icon())