Skip to content

Commit c29a427

Browse files
collider export
1 parent 558d5ba commit c29a427

File tree

5 files changed

+171
-11
lines changed

5 files changed

+171
-11
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"python.pythonPath": "/usr/local/bin/python3",
3-
"blender.addon.reloadOnSave": true
3+
"blender.addon.reloadOnSave": true,
4+
"blender.allowModifyExternalPython": true,
45
}

__init__.py

Lines changed: 117 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33
# https://github.com/RenaudRohlinger/blender_gltf_scripts
44

55
import os
6+
7+
8+
if "bpy" in locals():
9+
import importlib
10+
importlib.reload(apply_all)
11+
else:
12+
from . import apply_all
13+
614
import bpy
7-
from bpy_extras.io_utils import ExportHelper
815

916
bl_info = {
1017
"name": "GLTF Scripts",
@@ -20,18 +27,18 @@
2027

2128
loading = False
2229

23-
2430
def main_instance(self, context):
2531
bpy.context.window.cursor_set("WAIT")
2632
loading = True
2733
bpy.ops.object.select_all(action="DESELECT")
2834

2935
# prevent the undo to not work
30-
collections = bpy.data.collections
31-
for collection in collections:
32-
if collection.users_dupli_group:
33-
for obj in collection.all_objects:
34-
obj.select_set(True)
36+
objects=[ob for ob in bpy.context.view_layer.objects if ob.visible_get()]
37+
38+
mesh = [m for m in objects if m.type == 'MESH']
39+
40+
for obj in mesh:
41+
obj.select_set(True)
3542

3643
# name of the glb generated based on the name of the .blend file
3744
basedir = os.path.dirname(bpy.data.filepath)
@@ -94,6 +101,86 @@ def main_instance(self, context):
94101
pass
95102

96103

104+
105+
106+
def export_collider(self, context):
107+
bpy.context.window.cursor_set("WAIT")
108+
loading = True
109+
bpy.ops.object.select_all(action="SELECT")
110+
111+
# prevent the undo to not work
112+
# collections = bpy.data.collections
113+
# for collection in collections:
114+
# for obj in collection.all_objects:
115+
# obj.select_set(True)
116+
117+
118+
119+
bpy.ops.object.select_all(action='DESELECT')
120+
121+
objects=[ob for ob in bpy.context.view_layer.objects if ob.visible_get()]
122+
123+
mesh = [m for m in objects if m.type == 'MESH']
124+
125+
for obj in mesh:
126+
obj.select_set(state=True)
127+
128+
bpy.context.view_layer.objects.active = obj
129+
130+
apply_all.apply_all_modifier(self, context)
131+
132+
bpy.ops.object.join()
133+
134+
# name of the glb generated based on the name of the .blend file
135+
basedir = os.path.dirname(bpy.data.filepath)
136+
137+
# add user basedir path if available
138+
if context.scene.dir_path:
139+
basedir = bpy.path.abspath(context.scene.dir_path)
140+
141+
name = os.path.splitext(os.path.basename(bpy.data.filepath))[0]
142+
143+
if context.scene.filename_path:
144+
name = context.scene.filename_path
145+
146+
if not name:
147+
context.scene.filename_path = "scene"
148+
name = "scene"
149+
150+
fn = os.path.join(basedir, name) + "_physic.glb"
151+
152+
wm = bpy.types.WindowManager
153+
props = wm.operator_properties_last("export_scene.gltf")
154+
dic = {}
155+
for k, v in props.items():
156+
dic[k] = v
157+
158+
if context.scene.advanced_mode:
159+
if props:
160+
fn = dic["filepath"][:-4] + "_physic.glb"
161+
162+
bpy.ops.export_scene.gltf(
163+
filepath=fn,
164+
check_existing=True,
165+
export_format="GLB",
166+
ui_tab="GENERAL",
167+
use_selection=True,
168+
export_materials= "NONE",
169+
export_colors=False,
170+
export_apply=True,
171+
export_normals=False,
172+
export_texcoords=False,
173+
export_draco_mesh_compression_level=context.scene.draco_level,
174+
export_draco_mesh_compression_enable=False,
175+
)
176+
self.report({"INFO"}, "GLTF Export completed")
177+
178+
bpy.context.window.cursor_set("DEFAULT")
179+
loading = False
180+
pass
181+
182+
183+
97184
def main(self, context):
98185
bpy.context.window.cursor_set("WAIT")
99186
loading = True
@@ -252,6 +339,25 @@ def main_gltf(self, context):
252339
pass
253340

254341

342+
class GLTF_Collider(bpy.types.Operator):
343+
bl_idname = "object.gltf_collider"
344+
bl_label = "Collider Export"
345+
346+
def execute(self, context):
347+
try:
348+
self.report(
349+
{"INFO"}, "----- ----- ----- GLTF Scripts ----- ----- -----")
350+
self.report({"INFO"}, "Collider merge processing")
351+
bpy.ops.ed.undo_push(message="Collider Export")
352+
export_collider(self, context)
353+
bpy.ops.ed.undo()
354+
return {"FINISHED"}
355+
except Exception as e:
356+
print("Something went wrong")
357+
self.report({"ERROR"}, "Something went wrong")
358+
# raise the exception again
359+
raise e
360+
255361
class SimpleGLTF(bpy.types.Operator):
256362
bl_idname = "object.simple_gltf"
257363
bl_label = "Quick Scene Export"
@@ -264,7 +370,7 @@ def execute(self, context):
264370
bpy.context.window.cursor_set("WAIT")
265371
# loading = True
266372
main_gltf(self, context)
267-
# bpy.ops.ed.undo()
373+
bpy.ops.ed.undo()
268374
bpy.context.window.cursor_set("DEFAULT")
269375
# loading = False
270376
return {"FINISHED"}
@@ -274,7 +380,6 @@ def execute(self, context):
274380
# raise the exception again
275381
raise e
276382

277-
278383
class BakeCamera(bpy.types.Operator):
279384
bl_idname = "object.simple_operator"
280385
bl_label = "Camera Bake Export"
@@ -476,11 +581,13 @@ def draw(self, context):
476581
)
477582
layout.operator("object.simple_gltf",
478583
icon="SHADERFX", depress=loading)
584+
layout.operator("object.gltf_collider",
585+
icon="MOD_BUILD", depress=loading)
479586
layout.operator("object.curves_export",
480587
icon="FORCE_CURVE", depress=loading)
481588

482589

483-
blender_classes = [BakeCamera, SimpleGLTF,
590+
blender_classes = [BakeCamera, SimpleGLTF, GLTF_Collider,
484591
GLTF_PT_Panel, GLTF_Instance, Curves_Export]
485592

486593

__pycache__/__init__.cpython-39.pyc

4.36 KB
Binary file not shown.

__pycache__/apply_all.cpython-39.pyc

1.22 KB
Binary file not shown.

apply_all.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import bpy
2+
3+
def apply_all_modifier(self, context):
4+
is_select, is_mod = False, False
5+
message_a, message_b = "", ""
6+
# collect names for objects failed to apply modifiers
7+
collect_names = []
8+
9+
for obj in bpy.context.selected_objects:
10+
is_select = True
11+
12+
# copying context for the operator's override
13+
contx = bpy.context.copy()
14+
contx['object'] = obj
15+
16+
for mod in obj.modifiers[:]:
17+
contx['modifier'] = mod
18+
is_mod = True
19+
try:
20+
bpy.ops.object.modifier_apply(
21+
contx,
22+
modifier=contx['modifier'].name
23+
)
24+
except:
25+
obj_name = getattr(obj, "name", "NO NAME")
26+
collect_names.append(obj_name)
27+
message_b = True
28+
pass
29+
30+
if is_select:
31+
if is_mod:
32+
message_a = "Applying modifiers on all Selected Objects"
33+
else:
34+
message_a = "No Modifiers on Selected Objects"
35+
else:
36+
self.report({"INFO"}, "No Selection. No changes applied")
37+
return {'CANCELLED'}
38+
39+
# applying failed for some objects, show report
40+
message_obj = (",".join(collect_names) if collect_names and
41+
len(collect_names) < 8 else "some objects (Check System Console)")
42+
43+
self.report({"INFO"},
44+
(message_a if not message_b else
45+
"Applying modifiers failed for {}".format(message_obj)))
46+
47+
if (collect_names and message_obj == "some objects (Check System Console)"):
48+
print("\n[Modifier Tools]\n\nApplying failed on:"
49+
"\n\n{} \n".format(", ".join(collect_names)))
50+
51+
return {'FINISHED'}
52+

0 commit comments

Comments
 (0)