|
| 1 | +bl_info = { |
| 2 | + "name": "Unity Mesh Sync", |
| 3 | + "author": "Unity Technologies", |
| 4 | + "blender": (3, 1, 0), |
| 5 | + "description": "Sync Meshes with Unity", |
| 6 | + "location": "View3D > Mesh Sync", |
| 7 | + "tracker_url": "https://github.com/Unity-Technologies/MeshSyncDCCPlugins", |
| 8 | + "support": "COMMUNITY", |
| 9 | + "category": "Import-Export", |
| 10 | +} |
| 11 | + |
| 12 | +import bpy |
| 13 | +from bpy.app.handlers import persistent |
| 14 | +import MeshSyncClientBlender as ms |
| 15 | +from unity_mesh_sync_common import * |
| 16 | + |
| 17 | +class MESHSYNC_PT: |
| 18 | + bl_space_type = "VIEW_3D" |
| 19 | + bl_region_type = "UI" |
| 20 | + bl_category = "Tool" |
| 21 | + |
| 22 | + |
| 23 | +class MESHSYNC_PT_Main(MESHSYNC_PT, bpy.types.Panel): |
| 24 | + bl_label = "MeshSync" |
| 25 | + |
| 26 | + def draw(self, context): |
| 27 | + pass |
| 28 | + |
| 29 | + |
| 30 | +class MESHSYNC_PT_Server(MESHSYNC_PT, bpy.types.Panel): |
| 31 | + bl_label = "Server" |
| 32 | + bl_parent_id = "MESHSYNC_PT_Main" |
| 33 | + |
| 34 | + def draw(self, context): |
| 35 | + scene = bpy.context.scene |
| 36 | + layout = self.layout |
| 37 | + layout.use_property_split = True |
| 38 | + layout.use_property_decorate = False |
| 39 | + layout.prop(scene, "meshsync_server_address") |
| 40 | + layout.prop(scene, "meshsync_server_port") |
| 41 | + |
| 42 | + |
| 43 | +class MESHSYNC_PT_Scene(MESHSYNC_PT, bpy.types.Panel): |
| 44 | + bl_label = "Scene" |
| 45 | + bl_parent_id = "MESHSYNC_PT_Main" |
| 46 | + |
| 47 | + def draw(self, context): |
| 48 | + scene = bpy.context.scene |
| 49 | + layout = self.layout |
| 50 | + layout.use_property_split = True |
| 51 | + layout.use_property_decorate = False |
| 52 | + layout.prop(scene, "meshsync_scale_factor") |
| 53 | + layout.prop(scene, "meshsync_sync_meshes") |
| 54 | + if scene.meshsync_sync_meshes: |
| 55 | + b = layout.box() |
| 56 | + b.prop(scene, "meshsync_curves_as_mesh") |
| 57 | + b.prop(scene, "meshsync_make_double_sided") |
| 58 | + b.prop(scene, "meshsync_bake_modifiers") |
| 59 | + b.prop(scene, "meshsync_bake_transform") |
| 60 | + layout.prop(scene, "meshsync_sync_bones") |
| 61 | + layout.prop(scene, "meshsync_sync_blendshapes") |
| 62 | + #layout.prop(scene, "meshsync_sync_textures") |
| 63 | + layout.prop(scene, "meshsync_sync_cameras") |
| 64 | + layout.prop(scene, "meshsync_sync_lights") |
| 65 | + layout.separator() |
| 66 | + if MESHSYNC_OT_AutoSync._timer: |
| 67 | + layout.operator("meshsync.auto_sync", text="Auto Sync", icon="PAUSE") |
| 68 | + else: |
| 69 | + layout.operator("meshsync.auto_sync", text="Auto Sync", icon="PLAY") |
| 70 | + layout.operator("meshsync.send_objects", text="Manual Sync") |
| 71 | + |
| 72 | + |
| 73 | +class MESHSYNC_PT_Animation(MESHSYNC_PT, bpy.types.Panel): |
| 74 | + bl_label = "Animation" |
| 75 | + bl_parent_id = "MESHSYNC_PT_Main" |
| 76 | + |
| 77 | + def draw(self, context): |
| 78 | + scene = bpy.context.scene |
| 79 | + layout = self.layout |
| 80 | + layout.use_property_split = True |
| 81 | + layout.use_property_decorate = False |
| 82 | + layout.prop(scene, "meshsync_frame_step") |
| 83 | + layout.operator("meshsync.send_animations", text="Sync") |
| 84 | + |
| 85 | + |
| 86 | +class MESHSYNC_PT_Cache(MESHSYNC_PT, bpy.types.Panel): |
| 87 | + bl_label = "Cache" |
| 88 | + bl_parent_id = "MESHSYNC_PT_Main" |
| 89 | + |
| 90 | + def draw(self, context): |
| 91 | + scene = bpy.context.scene |
| 92 | + layout = self.layout |
| 93 | + layout.use_property_split = True |
| 94 | + layout.use_property_decorate = False |
| 95 | + layout.operator("meshsync.export_cache", text="Export Cache") |
| 96 | + |
| 97 | + |
| 98 | +class MESHSYNC_PT_Version(MESHSYNC_PT, bpy.types.Panel): |
| 99 | + bl_label = "Plugin Version" |
| 100 | + bl_parent_id = "MESHSYNC_PT_Main" |
| 101 | + |
| 102 | + def draw(self, context): |
| 103 | + scene = bpy.context.scene |
| 104 | + layout = self.layout |
| 105 | + layout.label(text = msb_context.PLUGIN_VERSION) |
| 106 | + |
| 107 | + |
| 108 | +class MESHSYNC_OT_AutoSync(bpy.types.Operator): |
| 109 | + bl_idname = "meshsync.auto_sync" |
| 110 | + bl_label = "Auto Sync" |
| 111 | + _timer = None |
| 112 | + |
| 113 | + def __del__(self): |
| 114 | + MESHSYNC_OT_AutoSync._timer = None |
| 115 | + |
| 116 | + def invoke(self, context, event): |
| 117 | + scene = bpy.context.scene |
| 118 | + if not MESHSYNC_OT_AutoSync._timer: |
| 119 | + scene.meshsync_auto_sync = True |
| 120 | + if not scene.meshsync_auto_sync: |
| 121 | + # server not available |
| 122 | + return {'FINISHED'} |
| 123 | + MESHSYNC_OT_AutoSync._timer = context.window_manager.event_timer_add(1.0 / 3.0, window=context.window) |
| 124 | + context.window_manager.modal_handler_add(self) |
| 125 | + return {'RUNNING_MODAL'} |
| 126 | + else: |
| 127 | + scene.meshsync_auto_sync = False |
| 128 | + context.window_manager.event_timer_remove(MESHSYNC_OT_AutoSync._timer) |
| 129 | + MESHSYNC_OT_AutoSync._timer = None |
| 130 | + return {'FINISHED'} |
| 131 | + |
| 132 | + def modal(self, context, event): |
| 133 | + if event.type == "TIMER": |
| 134 | + self.update() |
| 135 | + return {'PASS_THROUGH'} |
| 136 | + |
| 137 | + def update(self): |
| 138 | + msb_context.flushPendingList(); |
| 139 | + msb_apply_scene_settings() |
| 140 | + msb_context.setup(bpy.context); |
| 141 | + msb_context.exportUpdatedObjects() |
| 142 | + |
| 143 | + |
| 144 | +class MESHSYNC_OT_ExportCache(bpy.types.Operator): |
| 145 | + bl_idname = "meshsync.export_cache" |
| 146 | + bl_label = "Export Cache" |
| 147 | + bl_description = "Export Cache" |
| 148 | + |
| 149 | + def on_bake_modifiers_updated(self = None, context = None): |
| 150 | + if not self.bake_modifiers: |
| 151 | + self.bake_transform = False |
| 152 | + |
| 153 | + def on_bake_transform_updated(self = None, context = None): |
| 154 | + if self.bake_transform: |
| 155 | + self.bake_modifiers = True |
| 156 | + |
| 157 | + filepath: bpy.props.StringProperty(subtype = "FILE_PATH") |
| 158 | + filename: bpy.props.StringProperty() |
| 159 | + directory: bpy.props.StringProperty(subtype = "FILE_PATH") |
| 160 | + |
| 161 | + # cache properties |
| 162 | + object_scope: bpy.props.EnumProperty( |
| 163 | + name = "Object Scope", |
| 164 | + default = "0", |
| 165 | + items = { |
| 166 | + ("0", "All", "Export all objects"), |
| 167 | + ("1", "Selected", "Export selected objects"), |
| 168 | + }) |
| 169 | + frame_range: bpy.props.EnumProperty( |
| 170 | + name = "Frame Range", |
| 171 | + default = "1", |
| 172 | + items = { |
| 173 | + ("0", "Current", "Export current frame"), |
| 174 | + ("1", "All", "Export all frames"), |
| 175 | + ("2", "Custom", "Export speficied frames"), |
| 176 | + }) |
| 177 | + frame_begin: bpy.props.IntProperty(name = "Frame Begin", default = 1) |
| 178 | + frame_end: bpy.props.IntProperty(name = "Frame End", default = 100) |
| 179 | + frame_step: bpy.props.IntProperty(name = "Frame Step", default = 1, min = 1) |
| 180 | + material_frame_range: bpy.props.EnumProperty( |
| 181 | + name = "Material Range", |
| 182 | + default = "1", |
| 183 | + items = { |
| 184 | + ("0", "None", "Export no materials"), |
| 185 | + ("1", "One", "Export one frame of materials"), |
| 186 | + ("2", "All", "Export all frames of materials"), |
| 187 | + }) |
| 188 | + zstd_compression_level: bpy.props.IntProperty(name = "ZSTD Compression", default = 3) |
| 189 | + curves_as_mesh: bpy.props.BoolProperty(name = "Curves as Mesh", default = True) |
| 190 | + make_double_sided: bpy.props.BoolProperty(name = "Make Double Sided", default = False) |
| 191 | + bake_modifiers: bpy.props.BoolProperty(name = "Bake Modifiers", default = True, update = on_bake_modifiers_updated) |
| 192 | + bake_transform: bpy.props.BoolProperty(name = "Bake Transform", default = False, update = on_bake_transform_updated) |
| 193 | + flatten_hierarchy: bpy.props.BoolProperty(name = "Flatten Hierarchy", default = False) |
| 194 | + merge_meshes: bpy.props.BoolProperty(name = "Merge Meshes", default = False) |
| 195 | + strip_normals: bpy.props.BoolProperty(name = "Strip Normals", default = False) |
| 196 | + strip_tangents: bpy.props.BoolProperty(name = "Strip Tangents", default = False) |
| 197 | + |
| 198 | + def execute(self, context): |
| 199 | + ctx = msb_cache |
| 200 | + ctx.object_scope = int(self.object_scope) |
| 201 | + ctx.frame_range = int(self.frame_range) |
| 202 | + ctx.frame_begin = self.frame_begin |
| 203 | + ctx.frame_end = self.frame_end |
| 204 | + ctx.material_frame_range = int(self.material_frame_range) |
| 205 | + ctx.zstd_compression_level = self.zstd_compression_level |
| 206 | + ctx.frame_step = self.frame_step |
| 207 | + ctx.curves_as_mesh = self.curves_as_mesh |
| 208 | + ctx.make_double_sided = self.make_double_sided |
| 209 | + ctx.bake_modifiers = self.bake_modifiers |
| 210 | + ctx.bake_transform = self.bake_transform |
| 211 | + ctx.flatten_hierarchy = self.flatten_hierarchy |
| 212 | + ctx.merge_meshes = self.merge_meshes |
| 213 | + ctx.strip_normals = self.strip_normals |
| 214 | + ctx.strip_tangents = self.strip_tangents |
| 215 | + ctx.export(self.filepath) |
| 216 | + MS_MessageBox("Finished writing scene cache to " + self.filepath) |
| 217 | + return {'FINISHED'} |
| 218 | + |
| 219 | + def invoke(self, context, event): |
| 220 | + msb_context.setup(bpy.context) |
| 221 | + ctx = msb_cache |
| 222 | + self.object_scope = str(ctx.object_scope); |
| 223 | + self.frame_range = str(ctx.frame_range); |
| 224 | + self.frame_begin = ctx.frame_begin; |
| 225 | + self.frame_end = ctx.frame_end; |
| 226 | + self.material_frame_range = str(ctx.material_frame_range); |
| 227 | + self.frame_end = ctx.frame_end; |
| 228 | + self.zstd_compression_level = ctx.zstd_compression_level; |
| 229 | + self.frame_step = ctx.frame_step; |
| 230 | + self.curves_as_mesh = ctx.curves_as_mesh; |
| 231 | + self.make_double_sided = ctx.make_double_sided; |
| 232 | + self.bake_modifiers = ctx.bake_modifiers; |
| 233 | + self.bake_transform = ctx.bake_transform; |
| 234 | + self.flatten_hierarchy = ctx.flatten_hierarchy; |
| 235 | + self.merge_meshes = ctx.merge_meshes; |
| 236 | + self.strip_normals = ctx.strip_normals; |
| 237 | + self.strip_tangents = ctx.strip_tangents; |
| 238 | + |
| 239 | + path = bpy.data.filepath |
| 240 | + if len(path) != 0: |
| 241 | + tmp = os.path.split(path) |
| 242 | + self.directory = tmp[0] |
| 243 | + self.filename = re.sub(r"\.[^.]+$", ".sc", tmp[1]) |
| 244 | + else: |
| 245 | + self.directory = "" |
| 246 | + self.filename = "Untitled.sc"; |
| 247 | + wm = bpy.context.window_manager |
| 248 | + wm.fileselect_add(self) |
| 249 | + return {'RUNNING_MODAL'} |
| 250 | + |
| 251 | + def draw(self, context): |
| 252 | + layout = self.layout |
| 253 | + if hasattr(layout, "use_property_split"): # false on 2.79 |
| 254 | + layout.use_property_split = True |
| 255 | + layout.prop(self, "object_scope") |
| 256 | + layout.prop(self, "frame_range") |
| 257 | + if self.frame_range == "2": |
| 258 | + b = layout.box() |
| 259 | + b.prop(self, "frame_begin") |
| 260 | + b.prop(self, "frame_end") |
| 261 | + layout.prop(self, "material_frame_range") |
| 262 | + layout.prop(self, "zstd_compression_level") |
| 263 | + layout.prop(self, "frame_step") |
| 264 | + layout.prop(self, "curves_as_mesh") |
| 265 | + layout.prop(self, "make_double_sided") |
| 266 | + layout.prop(self, "bake_modifiers") |
| 267 | + layout.prop(self, "bake_transform") |
| 268 | + layout.prop(self, "flatten_hierarchy") |
| 269 | + #layout.prop(self, "merge_meshes") |
| 270 | + layout.prop(self, "strip_normals") |
| 271 | + layout.prop(self, "strip_tangents") |
| 272 | + |
| 273 | +# --------------------------------------------------------------------------------------------------------------------- |
| 274 | + |
| 275 | +classes = ( |
| 276 | + MESHSYNC_PT_Main, |
| 277 | + MESHSYNC_PT_Server, |
| 278 | + MESHSYNC_PT_Scene, |
| 279 | + MESHSYNC_PT_Animation, |
| 280 | + MESHSYNC_PT_Cache, |
| 281 | + MESHSYNC_PT_Version, |
| 282 | + MESHSYNC_OT_SendObjects, |
| 283 | + MESHSYNC_OT_SendAnimations, |
| 284 | + MESHSYNC_OT_AutoSync, |
| 285 | + MESHSYNC_OT_ExportCache, |
| 286 | +) |
| 287 | + |
| 288 | +def register(): |
| 289 | + msb_initialize_properties() |
| 290 | + for c in classes: |
| 291 | + bpy.utils.register_class(c) |
| 292 | + |
| 293 | +def unregister(): |
| 294 | + msb_context.Destroy() |
| 295 | + for c in classes: |
| 296 | + bpy.utils.unregister_class(c) |
| 297 | + |
| 298 | +def DestroyMeshSyncContext(): |
| 299 | + msb_context.Destroy() |
| 300 | + |
| 301 | +import atexit |
| 302 | +atexit.register(DestroyMeshSyncContext) |
| 303 | + |
| 304 | +# --------------------------------------------------------------------------------------------------------------------- |
| 305 | + |
| 306 | +if __name__ == "__main__": |
| 307 | + register() |
0 commit comments