first windows distributions
[convexer.git] / __init__.py
index 7ba2584dbb5c0482840656d7441536f9f2b90ffb..6b6185cb6a208c2844e42d026110df0cc8997204 100644 (file)
@@ -1,4 +1,12 @@
-# Copyright (C) 2022 Harry Godden (hgn)
+#                              CONVEXER v0.95
+#
+#               A GNU/Linux-first Source1 Hammer replacement
+#                    built with Blender, for mapmakers
+#
+#                  Copyright (C) 2022 Harry Godden (hgn)
+#
+# LICENSE: GPLv3.0, please see COPYING and LICENSE for more information
+#
 
 bl_info = {
    "name":"Convexer",
@@ -20,6 +28,16 @@ from ctypes import *
 from gpu_extras.batch import batch_for_shader
 from bpy.app.handlers import persistent
 
+# Setup platform dependent variables
+
+exec(open(F'{os.path.dirname(__file__)}/platform.py').read())
+if CXR_GNU_LINUX==1:
+   CXR_SHARED_EXT=".so"
+   CXR_EXE_EXT=""
+else:
+   CXR_SHARED_EXT=".dll"
+   CXR_EXE_EXT=".exe"
+
 # GPU and viewport drawing
 # ------------------------------------------------------------------------------
 
@@ -32,6 +50,7 @@ cxr_view_lines = None
 cxr_view_mesh = None
 cxr_jobs_batch = None
 cxr_jobs_inf = []
+cxr_error_inf = None
 
 # Shaders
 cxr_view_shader = gpu.shader.from_builtin('3D_SMOOTH_COLOR')
@@ -62,13 +81,35 @@ void main()
 # Render functions
 #
 def cxr_ui(_,context):
-   global cxr_jobs_batch, cxr_ui_shader, cxr_jobs_inf
+   global cxr_jobs_batch, cxr_ui_shader, cxr_jobs_inf, cxr_error_inf
 
    w = gpu.state.viewport_get()[2]
    cxr_ui_shader.bind()
    cxr_ui_shader.uniform_float( "scale", w )
 
-   if cxr_jobs_batch != None:
+   if cxr_error_inf != None:
+      err_begin = 50
+
+      if isinstance(cxr_error_inf[1],list):
+         err_begin += 20*(len(cxr_error_inf[1])-1)
+
+      blf.position(0,2,err_begin,0)
+      blf.size(0,50,48)
+      blf.color(0, 1.0,0.2,0.2,0.9)
+      blf.draw(0,cxr_error_inf[0])
+      
+      blf.size(0,50,24)
+      blf.color(0, 1.0,1.0,1.0,1.0)
+
+      if isinstance(cxr_error_inf[1],list):
+         for i,inf in enumerate(cxr_error_inf[1]):
+            blf.position(0,2,err_begin-30-i*20,0)
+            blf.draw(0,inf)
+      else:
+         blf.position(0,2,err_begin-30,0)
+         blf.draw(0,cxr_error_inf[1])
+
+   elif cxr_jobs_batch != None:
       gpu.state.blend_set('ALPHA')
       cxr_jobs_batch.draw(cxr_ui_shader)
 
@@ -89,12 +130,6 @@ def cxr_ui(_,context):
          blf.draw(0,ln[:-1])
          py += 16
 
-   #if CXR_PREVIEW_OPERATOR.LASTERR != None:
-   #   blf.position(0,2,80,0)
-   #   blf.size(0,50,48)
-   #   blf.color(0,1.0,0.2,0.2,0.9)
-   #   blf.draw(0,"Invalid geometry")
-
    # Something is off with TIMER,
    # this forces the viewport to redraw before we can continue with our
    # compilation stuff.
@@ -186,10 +221,11 @@ def scene_redraw():
 # Shared libraries
 # ------------------------------------------------------------------------------
 
-# dlclose for reloading modules manually
-libc_dlclose = None
-libc_dlclose = cdll.LoadLibrary(None).dlclose
-libc_dlclose.argtypes = [c_void_p]
+if CXR_GNU_LINUX==1:
+   # dlclose for reloading modules manually
+   libc_dlclose = None
+   libc_dlclose = cdll.LoadLibrary(None).dlclose
+   libc_dlclose.argtypes = [c_void_p]
 
 # wrapper for ctypes binding
 class extern():
@@ -472,6 +508,18 @@ def libcxr_line_callback( p0,p1,colour ):
    cxr_line_colours += [(colour[0],colour[1],colour[2],colour[3])]
    cxr_line_colours += [(colour[0],colour[1],colour[2],colour[3])]
 
+def cxr_reset_all():
+   global cxr_jobs_inf, cxr_jobs_batch, cxr_error_inf, cxr_view_mesh
+   cxr_jobs_inf = None
+   cxr_jobs_batch = None
+   cxr_error_inf = None
+
+   cxr_reset_lines()
+   cxr_batch_lines()
+   cxr_view_mesh = None
+
+   scene_redraw()
+
 # libnbvtf
 # ------------------------------------------------------------------------------
 
@@ -503,12 +551,15 @@ def shared_reload():
 
    # Unload libraries if existing
    def _reload( lib, path ):
-      if lib != None:
-         _handle = lib._handle
-         for i in range(10): libc_dlclose( _handle )
-         lib = None
-         del lib
-      return cdll.LoadLibrary( F'{os.path.dirname(__file__)}/{path}.so' )
+      if CXR_GNU_LINUX==1:
+         if lib != None:
+            _handle = lib._handle
+            for i in range(10): libc_dlclose( _handle )
+            lib = None
+            del lib
+
+      libpath = F'{os.path.dirname(__file__)}/{path}{CXR_SHARED_EXT}'
+      return cdll.LoadLibrary( libpath )
 
    libnbvtf = _reload( libnbvtf, "libnbvtf" )
    libcxr = _reload( libcxr, "libcxr" )
@@ -760,9 +811,85 @@ def asset_full_path(sdir,asset):
    return F"{bpy.context.scene.cxr_data.subdir}/"+\
       F"{asset_path(sdir,asset_uid(asset))}"
 
+# Decomposes mesh, and sets global error information if failed. 
+# - returns None on fail
+# - returns world on success
+def cxr_decompose_globalerr( mesh_src ):
+   global cxr_error_inf
+
+   err = c_int32(0)
+   world = libcxr_decompose.call( mesh_src, pointer(err) )
+
+   if world == None:
+      cxr_view_mesh = None
+      cxr_batch_lines()
+      
+      cxr_error_inf = [\
+          ("No Error", "There is no error?"),\
+          ("Bad input", "Non manifold geometry is present in the input mesh"),\
+          ("Bad result","An invalid manifold was generated, try to simplify"),\
+          ("Bad result","Make sure there is a clear starting point"),\
+          ("Bad result","Implicit vertex was invalid, try to simplify"),\
+          ("Bad input","Non coplanar vertices are in the source mesh"),\
+          ("Bad input","Non convex polygon is in the source mesh"),\
+          ("Bad result","Undefined failure"),\
+          ("Invalid Input", "Undefined failure"),\
+         ][err.value]
+
+      scene_redraw()
+
+   return world
+
 # Entity functions / infos
 # ------------------------
 
+def cxr_collection_purpose(collection):
+   if collection.name.startswith('.'): return None
+   if collection.hide_render: return None
+   if collection.name.startswith('mdl_'): return 'model'
+   return 'group'
+
+def cxr_object_purpose(obj):
+   objpurpose = None
+   group = None
+
+   def _search(collection):
+      nonlocal objpurpose, group, obj
+
+      purpose = cxr_collection_purpose( collection )
+      if purpose == None: return
+      if purpose == 'model':
+         for o in collection.objects:
+            if o == obj:
+               if o.type != 'EMPTY':
+                  objpurpose = 'model'
+                  group = collection
+               return
+         return
+      for o in collection.objects:
+         if o == obj:
+            classname = cxr_classname(o)
+            if classname != None: 
+               objpurpose = 'entity'
+               if o.type == 'MESH':
+                  objpurpose = 'brush_entity'
+               group = collection
+            else: 
+               if o.type == 'MESH':
+                  objpurpose = 'brush'
+                  group = collection
+            return
+      for c in collection.children:
+         _search(c)
+
+   if 'main' in bpy.data.collections:
+      _search( bpy.data.collections['main'] )
+
+   if objpurpose == None and 'skybox' in bpy.data.collections:
+      _search( bpy.data.collections['skybox'] )
+
+   return (group,objpurpose)
+
 def cxr_intrinsic_classname(obj):
    if obj.type == 'LIGHT':
       return {
@@ -968,11 +1095,10 @@ def cxr_scene_collect():
 
    def _collect(collection,transform):
       nonlocal sceneinfo
-
-      if collection.name.startswith('.'): return
-      if collection.hide_render: return
-
-      if collection.name.startswith('mdl_'):
+      
+      purpose = cxr_collection_purpose( collection )
+      if purpose == None: return
+      if purpose == 'model':
          sceneinfo['entities'] += [{
             "object": collection,
             "classname": "prop_static",
@@ -1066,7 +1192,7 @@ def cxr_export_vmf(sceneinfo, output_vmf):
          print( F"{vmfinfo.brush_count} :: {cmd['object'].name}" )
 
          baked = mesh_cxr_format( cmd['object'] )
-         world = libcxr_decompose.call( baked, None )
+         world = cxr_decompose_globalerr( baked )
          
          if world == None:
             return False
@@ -1429,6 +1555,15 @@ class CXR_RELOAD(bpy.types.Operator):
       shared_reload()
       return {'FINISHED'}
 
+# Reset all debugging/ui information
+#
+class CXR_RESET(bpy.types.Operator):
+   bl_idname="convexer.reset"
+   bl_label="Reset Convexer"
+   def execute(_,context):
+      cxr_reset_all()
+      return {'FINISHED'}
+
 # Used for exporting data to use with ASAN builds
 #
 class CXR_DEV_OPERATOR(bpy.types.Operator):
@@ -1447,60 +1582,21 @@ class CXR_PREVIEW_OPERATOR(bpy.types.Operator):
    bl_idname="convexer.preview"
    bl_label="Preview Brushes"
 
-   LASTERR = None
    RUNNING = False
 
    def execute(_,context):
-      return {'FINISHED'}
-
-   def modal(_,context,event):
       global cxr_view_mesh
-      static = _.__class__
-
-      if event.type == 'ESC':
-         cxr_reset_lines()
-         cxr_batch_lines()
-         cxr_view_mesh = None
-         static.RUNNING = False
-
-         scene_redraw()
-         return {'FINISHED'}
-
-      return {'PASS_THROUGH'}
+      global cxr_view_shader, cxr_view_mesh, cxr_error_inf
+      
+      cxr_reset_all()
 
-   def invoke(_,context,event):
-      global cxr_view_shader, cxr_view_mesh
       static = _.__class__
-      static.LASTERR = None
-
-      cxr_reset_lines()
 
       mesh_src = mesh_cxr_format(context.active_object)
-
-      err = c_int32(0)
-      world = libcxr_decompose.call( mesh_src, pointer(err) )
+      world = cxr_decompose_globalerr( mesh_src )
 
       if world == None:
-         cxr_view_mesh = None
-         cxr_batch_lines()
-         scene_redraw()
-
-         static.LASTERR = ["There is no error", \
-               "Non-Manifold",\
-               "Bad-Manifold",\
-               "No-Candidate",\
-               "Internal-Fail",\
-               "Non-Coplanar",\
-               "Non-Convex Polygon",\
-               "Bad Result",\
-               "Invalid-Input"]\
-               [err.value]
-
-         if static.RUNNING:
-            return {'CANCELLED'}
-         else:
-            context.window_manager.modal_handler_add(_)
-            return {'RUNNING_MODAL'}
+         return {'FINISHED'}
       
       # Generate preview using cxr
       #
@@ -1527,15 +1623,8 @@ class CXR_PREVIEW_OPERATOR(bpy.types.Operator):
       libcxr_free_world.call( world )
       cxr_batch_lines()
       scene_redraw()
-
-      # Allow user to spam the operator
-      if static.RUNNING:
-         return {'CANCELLED'}
-
-      if not static.RUNNING:
-         static.RUNNING = True
-         context.window_manager.modal_handler_add(_)
-         return {'RUNNING_MODAL'}
+      
+      return {'FINISHED'}
 
 # Search for VMF compiler executables in subdirectory
 #
@@ -1563,8 +1652,21 @@ def cxr_compiler_path( compiler ):
    if os.path.exists( path ): return path
    else: return None
 
+# Compatibility layer
+#
+def cxr_temp_file( fn ):
+   if CXR_GNU_LINUX == 1:
+      return F"/tmp/fn"
+   else:
+      filepath = bpy.data.filepath
+      directory = os.path.dirname(filepath)
+      return F"{directory}/{fn}.txt"
+
 def cxr_winepath( path ):
-   return 'z:'+path.replace('/','\\')
+   if CXR_GNU_LINUX == 1:
+      return 'z:'+path.replace('/','\\')
+   else:
+      return path
 
 # Main compile function
 #
@@ -1586,7 +1688,7 @@ class CXR_COMPILER_CHAIN(bpy.types.Operator):
    JOBSYS = None
 
    def cancel(_,context):
-      global cxr_jobs_batch
+      #global cxr_jobs_batch
       static = _.__class__
       wm = context.window_manager
       
@@ -1600,7 +1702,7 @@ class CXR_COMPILER_CHAIN(bpy.types.Operator):
       
       static.FILE.close()
 
-      cxr_jobs_batch = None
+      #cxr_jobs_batch = None
       scene_redraw()
       return {'FINISHED'}
 
@@ -1608,7 +1710,7 @@ class CXR_COMPILER_CHAIN(bpy.types.Operator):
       static = _.__class__
 
       if ev.type == 'TIMER':
-         global cxr_jobs_batch
+         global cxr_jobs_batch, cxr_error_inf
 
          if static.WAIT_REDRAW:
             scene_redraw()
@@ -1622,15 +1724,16 @@ class CXR_COMPILER_CHAIN(bpy.types.Operator):
          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()
+            # 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(cxr_temp_file("convexer_compile_log.txt"),"r") as log:
+               static.LOG = log.readlines()
+            if status == None:
                return {'PASS_THROUGH'}
             else:
                #for l in static.SUBPROC.stdout:
@@ -1639,6 +1742,10 @@ class CXR_COMPILER_CHAIN(bpy.types.Operator):
 
                if status != 0:
                   print(F'Compiler () error: {status}')
+
+                  jobn = static.JOBSYS['jobs'][static.JOBID]
+                  cxr_error_inf = ( F"{static.JOBSYS['title']} error {status}", jobn )
+
                   return _.cancel(context)
 
                static.JOBSYS['jobs'][static.JOBID] = None
@@ -1675,9 +1782,8 @@ class CXR_COMPILER_CHAIN(bpy.types.Operator):
 
          # All completed
          print( "All jobs completed!" )
-         cxr_jobs_batch = None
-
-         scene_redraw()
+         #cxr_jobs_batch = None
+         #scene_redraw()
          return _.cancel(context)
       
       return {'PASS_THROUGH'}
@@ -1692,6 +1798,7 @@ class CXR_COMPILER_CHAIN(bpy.types.Operator):
          return {'RUNNING_MODAL'}
 
       print("Launching compiler toolchain")
+      cxr_reset_all()
 
       # Run static compilation units now (collect, vmt..)
       filepath = bpy.data.filepath
@@ -1712,7 +1819,7 @@ class CXR_COMPILER_CHAIN(bpy.types.Operator):
       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.FILE = open(cxr_temp_file("convexer_compile_log.txt"),"w")
       static.LOG = []
 
       sceneinfo = cxr_scene_collect()
@@ -1823,7 +1930,7 @@ class CXR_COMPILER_CHAIN(bpy.types.Operator):
          static.JOBINFO += [{ 
             "title": "Convexer",
             "w": 20,
-            "colour": (1.0,0.3,0.1,1.0),
+            "colour": (0.863, 0.078, 0.235,1.0),
             "exec": cxr_export_vmf,
             "jobs": [(sceneinfo,output_vmf)]
          }]
@@ -1833,7 +1940,7 @@ class CXR_COMPILER_CHAIN(bpy.types.Operator):
             static.JOBINFO += [{
                "title": "Textures",
                "w": 40,
-               "colour": (0.1,1.0,0.3,1.0),
+               "colour": (1.000, 0.271, 0.000,1.0),
                "exec": compile_image,
                "jobs": image_jobs
             }]
@@ -1849,7 +1956,7 @@ class CXR_COMPILER_CHAIN(bpy.types.Operator):
             static.JOBINFO += [{
                "title": "Batches",
                "w": 25,
-               "colour": (0.5,0.5,1.0,1.0),
+               "colour": (1.000, 0.647, 0.000,1.0),
                "exec": cxr_export_modelsrc,
                "jobs": model_jobs
             }]
@@ -1858,7 +1965,7 @@ class CXR_COMPILER_CHAIN(bpy.types.Operator):
             static.JOBINFO += [{
                "title": "StudioMDL",
                "w": 20,
-               "colour": (0.8,0.1,0.1,1.0),
+               "colour": (1.000, 0.843, 0.000, 1.0),
                "exec": "studiomdl",
                "jobs": [[settings[F'exe_studiomdl']] + [\
                      '-nop4', '-game', game, qc] for qc in qc_jobs],
@@ -1872,7 +1979,7 @@ class CXR_COMPILER_CHAIN(bpy.types.Operator):
             static.JOBINFO += [{
                "title": "VBSP",
                "w": 25,
-               "colour": (0.1,0.2,1.0,1.0),
+               "colour": (0.678, 1.000, 0.184,1.0),
                "exec": "vbsp",
                "jobs": [[settings[F'exe_vbsp']] + vbsp_opt + args],
                "cwd": directory
@@ -1883,7 +1990,7 @@ class CXR_COMPILER_CHAIN(bpy.types.Operator):
             static.JOBINFO += [{
                "title": "VVIS",
                "w": 25,
-               "colour": (0.9,0.5,0.5,1.0),
+               "colour": (0.000, 1.000, 0.498,1.0),
                "exec": "vvis",
                "jobs": [[settings[F'exe_vvis']] + vvis_opt + args ],
                "cwd": directory
@@ -1894,7 +2001,7 @@ class CXR_COMPILER_CHAIN(bpy.types.Operator):
             static.JOBINFO += [{
                "title": "VRAD",
                "w": 25,
-               "colour": (0.9,0.2,0.3,1.0),
+               "colour": (0.125, 0.698, 0.667,1.0),
                "exec": "vrad",
                "jobs": [[settings[F'exe_vrad']] + vrad_opt + args ],
                "cwd": directory
@@ -1903,7 +2010,7 @@ class CXR_COMPILER_CHAIN(bpy.types.Operator):
          static.JOBINFO += [{
             "title": "CXR",
             "w": 5,
-            "colour": (0.0,1.0,0.4,1.0),
+            "colour": (0.118, 0.565, 1.000,1.0),
             "exec": cxr_patchmap,
             "jobs": [(bsp_local,bsp_remote)]
          }]
@@ -1912,7 +2019,7 @@ class CXR_COMPILER_CHAIN(bpy.types.Operator):
          static.JOBINFO += [{
             "title": "Pack",
             "w": 5,
-            "colour": (0.2,0.2,0.2,1.0),
+            "colour": (0.541, 0.169, 0.886,1.0),
             "exec": "bspzip",
             "jobs": [[cxr_compiler_path("bspzip"), '-addlist', \
                   cxr_winepath(bsp_remote), 
@@ -1993,19 +2100,34 @@ class CXR_VIEW3D( bpy.types.Panel ):
    bl_region_type = 'UI'
    bl_category = "Convexer"
 
-   @classmethod
-   def poll(cls, context):
-      return (context.object is not None)
-
    def draw(_, context):
       layout = _.layout
+
+      active_object = context.object
+      if active_object == None: return 
+
+      purpose = cxr_object_purpose( active_object )
+
+      if purpose[0] == None or purpose[1] == None:
+         usage_str = "No purpose"
+      else:
+         if purpose[1] == 'model':
+            usage_str = F'mesh in {asset_name( purpose[0] )}.mdl'
+         else:
+            usage_str = F'{purpose[1]} in {purpose[0].name}'
+
+      layout.label(text=F"Currently editing:")
+      box = layout.box()
+      box.label(text=usage_str)
+      
+      if purpose[1] == 'brush' or purpose[1] == 'brush_entity':
+         row = layout.row()
+         row.scale_y = 2
+         row.operator("convexer.preview")
+
       row = layout.row()
       row.scale_y = 2
-      row.operator("convexer.preview")
-
-      if CXR_PREVIEW_OPERATOR.LASTERR != None:
-         box = layout.box()
-         box.label(text=CXR_PREVIEW_OPERATOR.LASTERR, icon='ERROR')
+      row.operator("convexer.reset")
 
 # Main scene properties interface, where all the settings go
 #
@@ -2017,14 +2139,13 @@ class CXR_INTERFACE(bpy.types.Panel):
    bl_context="scene"
 
    def draw(_,context):
-      _.layout.operator("convexer.reload")
-      _.layout.operator("convexer.dev_test")
-      _.layout.operator("convexer.preview")
-      _.layout.operator("convexer.hash_reset")
+      if CXR_GNU_LINUX==1:
+         _.layout.operator("convexer.reload")
+         _.layout.operator("convexer.dev_test")
 
+      _.layout.operator("convexer.hash_reset")
       settings = context.scene.cxr_data
 
-      _.layout.prop(settings, "debug")
       _.layout.prop(settings, "scale_factor")
       _.layout.prop(settings, "skybox_scale_factor")
       _.layout.prop(settings, "skyname" )
@@ -2062,6 +2183,11 @@ class CXR_INTERFACE(bpy.types.Panel):
       row.scale_y = 3
       row.operator("convexer.chain", text=text)
 
+      row = box.row()
+      row.scale_y = 2
+      row.operator("convexer.reset")
+      if CXR_COMPILER_CHAIN.TIMER != None:
+         row.enabled = False
 
 class CXR_MATERIAL_PANEL(bpy.types.Panel):
    bl_label="VMT Properties"
@@ -2379,7 +2505,7 @@ class CXR_MODEL_SETTINGS(bpy.types.PropertyGroup):
 
 class CXR_SCENE_SETTINGS(bpy.types.PropertyGroup):
    project_name: bpy.props.StringProperty( name="Project Name" )
-   subdir: bpy.props.StringProperty( name="Subdirectory" )
+   subdir: bpy.props.StringProperty( name="../csgo/ folder" )
    
    exe_studiomdl: bpy.props.StringProperty( name="studiomdl" )
    exe_vbsp: bpy.props.StringProperty( name="vbsp" )
@@ -2390,7 +2516,6 @@ class CXR_SCENE_SETTINGS(bpy.types.PropertyGroup):
    opt_vrad: bpy.props.StringProperty( name="args", \
          default="-reflectivityScale 0.35 -aoscale 1.4 -final -textureshadows -hdr -StaticPropLighting -StaticPropPolys" )
 
-   debug: bpy.props.BoolProperty(name="Debug",default=False)
    scale_factor: bpy.props.FloatProperty( name="VMF Scale factor", \
          default=32.0,min=1.0)
    skybox_scale_factor: bpy.props.FloatProperty( name="Sky Scale factor", \
@@ -2417,7 +2542,7 @@ classes = [ CXR_RELOAD, CXR_DEV_OPERATOR, CXR_INTERFACE, \
             CXR_LIGHT_SETTINGS, CXR_SCENE_SETTINGS, CXR_DETECT_COMPILERS,\
             CXR_ENTITY_PANEL, CXR_LIGHT_PANEL, CXR_PREVIEW_OPERATOR,\
             CXR_VIEW3D, CXR_COMPILER_CHAIN, CXR_RESET_HASHES,\
-            CXR_COMPILE_MATERIAL, CXR_COLLECTION_PANEL ]
+            CXR_COMPILE_MATERIAL, CXR_COLLECTION_PANEL, CXR_RESET ]
 
 vmt_param_dynamic_class = None