+# Search for VMF compiler executables in subdirectory
+#
+class CXR_DETECT_COMPILERS(bpy.types.Operator):
+ bl_idname="convexer.detect_compilers"
+ bl_label="Find compilers"
+
+ def execute(self,context):
+ scene = context.scene
+ settings = scene.cxr_data
+ subdir = settings.subdir
+
+ for exename in ['studiomdl','vbsp','vvis','vrad']:
+ searchpath = os.path.normpath(F'{subdir}/../bin/{exename}.exe')
+ if os.path.exists(searchpath):
+ settings[F'exe_{exename}'] = searchpath
+
+ return {'FINISHED'}
+
+def cxr_compiler_path( compiler ):
+ settings = bpy.context.scene.cxr_data
+ subdir = settings.subdir
+ path = os.path.normpath(F'{subdir}/../bin/{compiler}.exe')
+
+ if os.path.exists( path ): return path
+ else: return None
+
+def cxr_winepath( path ):
+ return 'z:'+path.replace('/','\\')
+
+# Main compile function
+#
+class CXR_COMPILER_CHAIN(bpy.types.Operator):
+ bl_idname="convexer.chain"
+ bl_label="Compile Chain"
+
+ # 'static'
+ USER_EXIT = False
+ SUBPROC = None
+ TIMER = None
+ TIMER_LAST = 0.0
+ WAIT_REDRAW = False
+ FILE = None
+ LOG = []
+
+ JOBINFO = None
+ JOBID = 0
+ JOBSYS = None
+
+ def cancel(_,context):
+ global cxr_jobs_batch
+ static = _.__class__
+ wm = context.window_manager
+
+ if static.SUBPROC != None:
+ static.SUBPROC.terminate()
+ static.SUBPROC = None
+
+ if static.TIMER != None:
+ wm.event_timer_remove( static.TIMER )
+ static.TIMER = None
+
+ static.FILE.close()
+
+ cxr_jobs_batch = None
+ scene_redraw()
+ return {'FINISHED'}
+
+ def modal(_,context,ev):
+ static = _.__class__
+
+ if ev.type == 'TIMER':
+ global cxr_jobs_batch
+
+ if static.WAIT_REDRAW:
+ scene_redraw()
+ return {'PASS_THROUGH'}
+ static.WAIT_REDRAW = True
+
+ if static.USER_EXIT:
+ print( "Chain USER_EXIT" )
+ return _.cancel(context)
+
+ if static.SUBPROC != None:
+ # Deal with async modes
+ status = static.SUBPROC.poll()
+ if status == None:
+
+ # Cannot redirect STDOUT through here without causing
+ # undefined behaviour due to the Blender Python specification.
+ #
+ # Have to write it out to a file and read it back in.
+ #
+ with open("/tmp/convexer_compile_log.txt","r") as log:
+ static.LOG = log.readlines()
+ return {'PASS_THROUGH'}
+ else:
+ #for l in static.SUBPROC.stdout:
+ # print( F'-> {l.decode("utf-8")}',end='' )
+ static.SUBPROC = None
+
+ if status != 0:
+ print(F'Compiler () error: {status}')
+ return _.cancel(context)
+
+ static.JOBSYS['jobs'][static.JOBID] = None
+ cxr_jobs_update_graph( static.JOBINFO )
+ scene_redraw()
+ return {'PASS_THROUGH'}
+
+ # Compile syncronous thing
+ for sys in static.JOBINFO:
+ for i,target in enumerate(sys['jobs']):
+ if target != None:
+
+ if callable(sys['exec']):
+ print( F"Run (sync): {static.JOBID} @{time.time()}" )
+
+ if not sys['exec'](*target):
+ print( "Job failed" )
+ return _.cancel(context)
+
+ sys['jobs'][i] = None
+ static.JOBID += 1
+ else:
+ # Run external executable (wine)
+ static.SUBPROC = subprocess.Popen( target,
+ stdout=static.FILE,\
+ stderr=subprocess.PIPE,\
+ cwd=sys['cwd'])
+ static.JOBSYS = sys
+ static.JOBID = i
+
+ cxr_jobs_update_graph( static.JOBINFO )
+ scene_redraw()
+ return {'PASS_THROUGH'}
+
+ # All completed
+ print( "All jobs completed!" )
+ cxr_jobs_batch = None
+
+ scene_redraw()
+ return _.cancel(context)
+
+ return {'PASS_THROUGH'}
+
+ def invoke(_,context,event):
+ static = _.__class__
+ wm = context.window_manager
+
+ if static.TIMER != None:
+ print("Chain exiting...")
+ static.USER_EXIT=True
+ return {'RUNNING_MODAL'}
+
+ print("Launching compiler toolchain")
+
+ # Run static compilation units now (collect, vmt..)
+ filepath = bpy.data.filepath
+ directory = os.path.dirname(filepath)
+ settings = bpy.context.scene.cxr_data
+
+ asset_dir = F"{directory}/modelsrc"
+ material_dir = F"{settings.subdir}/materials/{settings.project_name}"
+ model_dir = F"{settings.subdir}/models/{settings.project_name}"
+ output_vmf = F"{directory}/{settings.project_name}.vmf"
+
+ bsp_local = F"{directory}/{settings.project_name}.bsp"
+ bsp_remote = F"{settings.subdir}/maps/{settings.project_name}.bsp"
+ bsp_packed = F"{settings.subdir}/maps/{settings.project_name}_pack.bsp"
+ packlist = F"{directory}/{settings.project_name}_assets.txt"
+
+ os.makedirs( asset_dir, exist_ok=True )
+ os.makedirs( material_dir, exist_ok=True )
+ os.makedirs( model_dir, exist_ok=True )
+
+ static.FILE = open(F"/tmp/convexer_compile_log.txt","w")
+ static.LOG = []
+
+ sceneinfo = cxr_scene_collect()
+ image_jobs = []
+ qc_jobs = []
+
+ # Collect materials
+ a_materials = set()
+ for brush in sceneinfo['geo']:
+ for ms in brush['object'].material_slots:
+ a_materials.add( ms.material )
+ if ms.material.cxr_data.shader == 'VertexLitGeneric':
+ errmat = ms.material.name
+ errnam = brush['object'].name
+ print( F"Vertex shader {errmat} used on {errnam}")
+ return {'CANCELLED'}
+
+ a_models = set()
+ model_jobs = []
+ for ent in sceneinfo['entities']:
+ if ent['object'] == None: continue
+
+ if ent['classname'] == 'prop_static':
+ obj = ent['object']
+ if isinstance(obj,bpy.types.Collection):
+ target = obj
+ a_models.add( target )
+ model_jobs += [(target, ent['origin'], asset_dir, \
+ settings.project_name, ent['transform'])]
+ else:
+ target = obj.instance_collection
+ if target in a_models:
+ continue
+ a_models.add( target )
+
+ # TODO: Should take into account collection instancing offset
+ model_jobs += [(target, [0,0,0], asset_dir, \
+ settings.project_name, ent['transform'])]
+
+ elif ent['object'].type == 'MESH':
+ for ms in ent['object'].material_slots:
+ a_materials.add( ms.material )
+
+ for mdl in a_models:
+ uid = asset_uid(mdl)
+ qc_jobs += [F'{uid}.qc']
+
+ for obj in mdl.objects:
+ for ms in obj.material_slots:
+ a_materials.add( ms.material )
+ if ms.material.cxr_data.shader == 'LightMappedGeneric' or \
+ ms.material.cxr_data.shader == 'WorldVertexTransition':
+
+ errmat = ms.material.name
+ errnam = obj.name
+ print( F"Lightmapped shader {errmat} used on {errnam}")
+ return {'CANCELLED'}
+
+ # Collect images
+ for mat in a_materials:
+ for pair in compile_material(mat):
+ decl = pair[0]
+ pdef = pair[1]
+ prop = pair[2]
+
+ if isinstance(prop,bpy.types.Image):
+ flags = 0
+ if 'flags' in pdef: flags = pdef['flags']
+ if prop not in image_jobs:
+ image_jobs += [(prop,)]
+ prop.cxr_data.flags = flags
+
+ # Create packlist
+ with open( packlist, "w" ) as fp:
+
+ for mat in a_materials:
+ if mat.cxr_data.shader == 'Builtin': continue
+ fp.write(F"{asset_path('materials',mat)}.vmt\n")
+ fp.write(F"{cxr_winepath(asset_full_path('materials',mat))}.vmt\n")
+
+ for img_job in image_jobs:
+ img = img_job[0]
+ fp.write(F"{asset_path('materials',img)}.vtf\n")
+ fp.write(F"{cxr_winepath(asset_full_path('materials',img))}.vtf\n")
+
+ for mdl in a_models:
+ local = asset_path('models',mdl)
+ winep = cxr_winepath(asset_full_path('models',mdl))
+
+ fp.write(F"{local}.vvd\n")
+ fp.write(F"{winep}.vvd\n")
+ fp.write(F"{local}.dx90.vtx\n")
+ fp.write(F"{winep}.dx90.vtx\n")
+ fp.write(F"{local}.mdl\n")
+ fp.write(F"{winep}.mdl\n")
+ fp.write(F"{local}.vvd\n")
+ fp.write(F"{winep}.vvd\n")
+
+ if cxr_modelsrc_vphys(mdl):
+ fp.write(F"{local}.phy\n")
+ fp.write(F"{winep}.phy\n")
+
+ # Convexer jobs
+ static.JOBID = 0
+ static.JOBINFO = []
+
+ if settings.comp_vmf:
+ static.JOBINFO += [{
+ "title": "Convexer",
+ "w": 20,
+ "colour": (1.0,0.3,0.1,1.0),
+ "exec": cxr_export_vmf,
+ "jobs": [(sceneinfo,output_vmf)]
+ }]
+
+ if settings.comp_textures:
+ if len(image_jobs) > 0:
+ static.JOBINFO += [{
+ "title": "Textures",
+ "w": 40,
+ "colour": (0.1,1.0,0.3,1.0),
+ "exec": compile_image,
+ "jobs": image_jobs
+ }]
+
+ game = cxr_winepath( settings.subdir )
+ args = [ \
+ '-game', game, settings.project_name
+ ]
+
+ # FBX stage
+ if settings.comp_models:
+ if len(model_jobs) > 0:
+ static.JOBINFO += [{
+ "title": "Batches",
+ "w": 25,
+ "colour": (0.5,0.5,1.0,1.0),
+ "exec": cxr_export_modelsrc,
+ "jobs": model_jobs
+ }]
+
+ if len(qc_jobs) > 0:
+ static.JOBINFO += [{
+ "title": "StudioMDL",
+ "w": 20,
+ "colour": (0.8,0.1,0.1,1.0),
+ "exec": "studiomdl",
+ "jobs": [[settings[F'exe_studiomdl']] + [\
+ '-nop4', '-game', game, qc] for qc in qc_jobs],
+ "cwd": asset_dir
+ }]
+
+ # VBSP stage
+ if settings.comp_compile:
+ static.JOBINFO += [{
+ "title": "VBSP",
+ "w": 25,
+ "colour": (0.1,0.2,1.0,1.0),
+ "exec": "vbsp",
+ "jobs": [[settings[F'exe_vbsp']] + args],
+ "cwd": directory
+ }]
+
+ static.JOBINFO += [{
+ "title": "VVIS",
+ "w": 25,
+ "colour": (0.9,0.5,0.5,1.0),
+ "exec": "vvis",
+ "jobs": [[settings[F'exe_vvis']] + ['-fast'] + args ],
+ "cwd": directory
+ }]
+
+ vrad_opt = settings.opt_vrad.split()
+ static.JOBINFO += [{
+ "title": "VRAD",
+ "w": 25,
+ "colour": (0.9,0.2,0.3,1.0),
+ "exec": "vrad",
+ "jobs": [[settings[F'exe_vrad']] + vrad_opt + args ],
+ "cwd": directory
+ }]
+
+ static.JOBINFO += [{
+ "title": "CXR",
+ "w": 5,
+ "colour": (0.0,1.0,0.4,1.0),
+ "exec": cxr_patchmap,
+ "jobs": [(bsp_local,bsp_remote)]
+ }]
+
+ if settings.comp_pack:
+ static.JOBINFO += [{
+ "title": "Pack",
+ "w": 5,
+ "colour": (0.2,0.2,0.2,1.0),
+ "exec": "bspzip",
+ "jobs": [[cxr_compiler_path("bspzip"), '-addlist', \
+ cxr_winepath(bsp_remote),
+ cxr_winepath(packlist),
+ cxr_winepath(bsp_packed) ]],
+ "cwd": directory
+ }]
+
+ if len(static.JOBINFO) == 0:
+ return {'CANCELLED'}
+
+ static.USER_EXIT=False
+ static.TIMER=wm.event_timer_add(0.1,window=context.window)
+ wm.modal_handler_add(_)
+
+ cxr_jobs_update_graph( static.JOBINFO )
+ scene_redraw()
+ return {'RUNNING_MODAL'}
+
+class CXR_RESET_HASHES(bpy.types.Operator):
+ bl_idname="convexer.hash_reset"
+ bl_label="Reset asset hashes"
+
+ def execute(_,context):
+ for c in bpy.data.collections:
+ c.cxr_data.last_hash = F"<RESET>{time.time()}"
+ c.cxr_data.asset_id=0
+
+ for t in bpy.data.images:
+ t.cxr_data.last_hash = F"<RESET>{time.time()}"
+ t.cxr_data.asset_id=0
+
+ return {'FINISHED'}
+
+class CXR_COMPILE_MATERIAL(bpy.types.Operator):
+ bl_idname="convexer.matcomp"
+ bl_label="Recompile Material"
+
+ def execute(_,context):
+ active_obj = bpy.context.active_object
+ active_mat = active_obj.active_material
+
+ #TODO: reduce code dupe (L1663)
+ for pair in compile_material(active_mat):
+ decl = pair[0]
+ pdef = pair[1]
+ prop = pair[2]
+
+ if isinstance(prop,bpy.types.Image):
+ flags = 0
+ if 'flags' in pdef: flags = pdef['flags']
+ prop.cxr_data.flags = flags
+
+ compile_image( prop )
+
+ settings = bpy.context.scene.cxr_data
+ with open(F'{settings.subdir}/cfg/convexer_mat_update.cfg','w') as o:
+ o.write(F'mat_reloadmaterial {asset_name(active_mat)}')
+
+ # TODO: Move this
+ with open(F'{settings.subdir}/cfg/convexer.cfg','w') as o:
+ o.write('sv_cheats 1\n')
+ o.write('mp_warmup_pausetimer 1\n')
+ o.write('bot_kick\n')
+ o.write('alias cxr_reload "exec convexer_mat_update"\n')
+
+ return {'FINISHED'}
+
+# Convexer panels
+# ------------------------------------------------------------------------------
+
+# Helper buttons for 3d toolbox view
+#