├── .gitignore ├── LICENSE ├── README.md └── __init__.py /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ASE Export Script 2 | 3 | This is a fork from the [original project](http://code.google.com/p/ase-export-vmc/) by MCampagnini. The script has been upgraded to support Blender 2.78a and Blender 2.80 (check the Releases). 4 | 5 | This addon has been superceded by [io_scene_ase](https://github.com/DarklightGames/io_scene_ase). 6 | 7 | ## Videos 8 | * Blender 2.63a and ASE 2.01 (http://www.youtube.com/watch?v=63Wz0FoK2U0) 9 | * Blender 2.59 old version (http://www.youtube.com/watch?v=ZPQW5x1rhAY&feature=channel&list=UL) 10 | 11 | ## Installation 12 | * Download and extract the ASE Export Script. 13 | * In Blender, File / User Preferences. 14 | * Go to the Add-On Tab. 15 | * Install Add-On, select the extracted python script. 16 | * Click Save as Default to enable the script every time Blender loads (optional). 17 | 18 | You will find a new export option in File / Export called Ascii Scene Export (.ase). If you do not see the script in the export menu after following the installation instructions, open the Add-On tab and find the script under Import-Export: ASCII Scene Exporter. Be sure that it is enabled by checking the box next to the add-on name. Contact me on my website if you still have problems exporting. 19 | 20 | ## Mesh Requirements 21 | * All non-collision geometries must be UV unwrapped. 22 | * All non-collision meshes must have a material applied. 23 | 24 | ## Optional 25 | * Mesh may have more than one material 26 | * Mesh may have more than one UV texture and/or a lightmap texture. 27 | * Mesh may be assigned as a collision object. 28 | * Mesh may have more than one collision object to determine collision borders. 29 | * Mesh may be assigned smoothing groups. 30 | 31 | ## Collision Objects 32 | Assign a mesh as a collision object by prefixing each object name with UCX_NAME, where NAME can be anything you would like (case-insensitive). 33 | 34 | ## Smoothing Groups 35 | Assign smoothing groups by edge selecting the border of the faces you want assigned to a smoothing group, press ctrl+e and mark sharp. Any face group separated with sharp edges will be assigned a smoothing group. 36 | 37 | ## Vertex Painting 38 | Apply vertex painting in Blender as normal, be sure that you have at least two uv texture slots. This is not a technical limitation, but due to time constraints I left the vertex painting code inside of a conditional that requires two uv textures. In order to view your vertex painting in UDK you will have to import and set up your materials correctly within UDK. 39 | -------------------------------------------------------------------------------- /__init__.py: -------------------------------------------------------------------------------- 1 | ## ***** BEGIN GPL LICENSE BLOCK ***** 2 | # 3 | # This program is free software; you can redistribute it and/or 4 | # modify it under the terms of the GNU General Public License 5 | # as published by the Free Software Foundation; either version 2 6 | # of the License, or (at your option) any later version. 7 | # 8 | # This program is distributed in the hope that it will be useful, 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | # GNU General Public License for more details. 12 | # 13 | # You should have received a copy of the GNU General Public License 14 | # along with this program; if not, write to the Free Software Foundation, 15 | # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 16 | # 17 | # ***** END GPL LICENCE BLOCK ***** 18 | # 19 | # 20 | # REQUIRED OPTIONS - 21 | # - Make Normals Consistent 22 | # - Remove Doubles 23 | # ********************************** 24 | bl_info = { 25 | "name": "ASCII Scene Exporter", 26 | "author": "Richard Bartlett, MCampagnini", 27 | "version": ( 2, 80, 0 ), 28 | "blender": ( 2, 80, 0 ), 29 | "api": 36079, 30 | "location": "File > Export > ASCII Scene Export(.ase)", 31 | "description": "ASCII Scene Export(.ase)", 32 | "warning": "", 33 | "wiki_url": "https://github.com/DarklightGames/io_export_ase", 34 | "tracker_url": "https://github.com/DarklightGames/io_export_ase/issues", 35 | "category": "Import-Export" 36 | } 37 | 38 | """ 39 | -- This script is intended to export in the ASE file format for STATIC MESHES ONLY. 40 | -- This script WILL NOT export skeletal meshes, these should be exported using a 41 | -- different file format. 42 | 43 | -- More Information at http://code.google.com/p/ase-export-vmc/ 44 | -- UDK Thread at http://forums.epicgames.com/threads/776986-Blender-2-57-ASE-export 45 | """ 46 | 47 | import os 48 | import bpy 49 | import math 50 | import time 51 | from bpy_extras import node_shader_utils 52 | 53 | # settings 54 | aseFloat = lambda x: '''{0: 0.4f}'''.format( x ) 55 | optionScale = 16.0 56 | optionSubmaterials = False 57 | optionSmoothingGroups = True 58 | optionAllowMultiMats = True 59 | 60 | # ASE components 61 | aseHeader = '' 62 | aseScene = '' 63 | aseMaterials = '' 64 | aseGeometry = '' 65 | 66 | # Other 67 | matList = [] 68 | numMats = 0 69 | currentMatId = 0 70 | 71 | #== Error ================================================================== 72 | class Error( Exception ): 73 | 74 | def __init__( self, message ): 75 | self.message = message 76 | print( '\n\n' + message + '\n\n' ) 77 | 78 | #== Header ================================================================= 79 | class cHeader: 80 | def __init__( self ): 81 | self.comment = "Ascii Scene Exporter v2.51" 82 | 83 | def __repr__( self ): 84 | return '''*3DSMAX_ASCIIEXPORT\t200\n*COMMENT "{0}"\n'''.format( self.comment ) 85 | 86 | #== Scene ================================================================== 87 | class cScene: 88 | def __init__( self ): 89 | self.filename = bpy.data.filepath 90 | self.firstframe = 0 91 | self.lastframe = 100 92 | self.framespeed = 30 93 | self.ticksperframe = 160 94 | self.backgroundstatic = ''.join( [aseFloat( x ) for x in [0.0, 0.0, 0.0]] ) 95 | self.ambientstatic = ''.join( [aseFloat( x ) for x in [0.0, 0.0, 0.0]] ) 96 | 97 | def __repr__( self ): 98 | return '''*SCENE {{\n\t*SCENE_FILENAME "{0}"\ 99 | \n\t*SCENE_FIRSTFRAME {1}\ 100 | \n\t*SCENE_LASTFRAME {2}\ 101 | \n\t*SCENE_FRAMESPEED {3}\ 102 | \n\t*SCENE_TICKSPERFxRAME {4}\ 103 | \n\t*SCENE_BACKGROUND_STATIC {5}\ 104 | \n\t*SCENE_AMBIENT_STATIC {6}\ 105 | \n}}\n'''.format( self.filename, self.firstframe, self.lastframe, self.framespeed, self.ticksperframe, self.backgroundstatic, self.ambientstatic ) 106 | 107 | #== Materials ============================================================== 108 | class cMaterials: 109 | def __init__( self ): 110 | global optionSubmaterials 111 | global matList 112 | global numMats 113 | 114 | self.material_list = [] 115 | 116 | # Get all of the materials used by non-collision object meshes 117 | for object in bpy.context.selected_objects: 118 | if collisionObject( object ) == True: 119 | continue 120 | elif object.type != 'MESH': 121 | continue 122 | else: 123 | print( object.name + ': Constructing Materials' ) 124 | for slot in object.material_slots: 125 | # if the material is not in the material_list, add it 126 | if self.material_list.count( slot.material ) == 0: 127 | self.material_list.append( slot.material ) 128 | mat_wrap = node_shader_utils.PrincipledBSDFWrapper(slot.material) if slot.material else None 129 | # image = mat_wrap.base_color_texture.image 130 | matList.append( slot.material.name ) 131 | 132 | self.material_count = len( self.material_list ) 133 | numMats = self.material_count 134 | 135 | # Raise an error if there are no materials found 136 | if self.material_count == 0: 137 | raise Error( 'Mesh must have at least one applied material' ) 138 | else: 139 | if ( optionSubmaterials ): 140 | self.dump = cSubMaterials( self.material_list ) 141 | else: 142 | self.dump = cMultiMaterials( self.material_list ) 143 | 144 | def __repr__( self ): 145 | return str( self.dump ) 146 | class cMultiMaterials: 147 | def __init__( self, material_list ): 148 | self.numMtls = len( material_list ) 149 | # Initialize material information 150 | self.dump = '''*MATERIAL_LIST {{\ 151 | \n\t*MATERIAL_COUNT {0}\ 152 | '''.format( str( self.numMtls ) ) 153 | 154 | for index, slot in enumerate( material_list ): 155 | self.dump += '''\n\t*MATERIAL {0} {{\ 156 | {1}\ 157 | \n\t}}'''.format( index, cMaterial( slot ) ) 158 | 159 | self.dump += '\n}' 160 | 161 | def __repr__( self ): 162 | return self.dump 163 | class cSubMaterials: 164 | def __init__( self, material_list ): 165 | slot = material_list[0] 166 | # Initialize material information 167 | self.dump = '''*MATERIAL_LIST {\ 168 | \n\t*MATERIAL_COUNT 1\ 169 | \n\t*MATERIAL 0 {\ 170 | ''' 171 | self.matDump = '' 172 | self.name = material_list[0].name 173 | self.numSubMtls = len( material_list ) 174 | self.diffusemap = cDiffusemap( None ) # /*slot.texture_slots[0]*/ 175 | if ( self.numSubMtls > 1 ): 176 | self.matClass = 'Multi/Sub-Object' 177 | self.diffuseDump = '' 178 | else: 179 | self.matClass = 'Standard' 180 | self.numSubMtls = 0 181 | self.diffuseDump = self.diffdump() 182 | self.ambient = ''.join( [aseFloat( x ) for x in [0.0, 0.0, 0.0]] ) 183 | self.diffuse = ''.join( [aseFloat( x ) for x in slot.diffuse_color] ) 184 | self.specular = ''.join( [aseFloat( x ) for x in slot.specular_color] ) 185 | self.shine = aseFloat( 0 ) 186 | self.shinestrength = aseFloat( 0 ) 187 | self.transparency = aseFloat( 0 ) 188 | self.wiresize = aseFloat( 1.0 ) 189 | self.shading = 'WHO CARES' 190 | self.xpfalloff = aseFloat( 0.0 ) 191 | self.xptype = 'Filter' 192 | self.falloff = 'In' 193 | self.soften = False 194 | self.submtls = [] 195 | self.selfillum = aseFloat( 0 ) 196 | 197 | if ( len( material_list ) > 1 ): 198 | # Build SubMaterials 199 | for index, slot in enumerate( material_list ): 200 | self.matDump += '''\n\t\t*SUBMATERIAL {0} {{\ 201 | {1}\ 202 | \n\t\t}}'''.format( index, cMaterial( slot ) ) 203 | 204 | self.dump += '''\n\t\t*MATERIAL_NAME "{0}"\ 205 | \n\t\t*MATERIAL_CLASS "{1}"\ 206 | \n\t\t*MATERIAL_AMBIENT {2}\ 207 | \n\t\t*MATERIAL_DIFFUSE {3}\ 208 | \n\t\t*MATERIAL_SPECULAR {4}\ 209 | \n\t\t*MATERIAL_SHINE {5}\ 210 | \n\t\t*MATERIAL_SHINESTRENGTH {6}\ 211 | \n\t\t*MATERIAL_TRANSPARENCY {7}\ 212 | \n\t\t*MATERIAL_WIRESIZE {8}\ 213 | \n\t\t*MATERIAL_SHADING {9}\ 214 | \n\t\t*MATERIAL_XP_FALLOFF {10}\ 215 | \n\t\t*MATERIAL_SELFILLUM {11}\ 216 | \n\t\t*MATERIAL_FALLOFF {12}\ 217 | \n\t\t*MATERIAL_XP_TYPE {13}\ 218 | {14}\ 219 | \n\t\t*NUMSUBMTLS {15}\ 220 | {16}'''.format( self.name, self.matClass, self.ambient, self.diffuse, self.specular, self.shine, self.shinestrength, self.transparency, self.wiresize, self.shading, self.xpfalloff, self.selfillum, self.falloff, self.xptype, self.diffuseDump, self.numSubMtls, self.matDump ) 221 | 222 | 223 | self.dump += '\t\n}\n}' 224 | 225 | def diffdump( self ): 226 | for x in [self.diffusemap]: 227 | return x 228 | 229 | def __repr__( self ): 230 | return self.dump 231 | class cMaterial: 232 | def __init__( self, slot ): 233 | self.dump = '' 234 | self.name = slot.name 235 | self.matClass = 'Standard' 236 | self.ambient = ''.join( [aseFloat( x ) for x in [0.0, 0.0, 0.0]] ) 237 | self.diffuse = ''.join( [aseFloat( x ) for x in slot.diffuse_color] ) 238 | self.specular = ''.join( [aseFloat( x ) for x in slot.specular_color] ) 239 | self.shine = aseFloat( 0.0 ) # slot.specular_hardness / 511 240 | self.shinestrength = aseFloat( 0.0 ) # slot.specular_intensity 241 | self.transparency = aseFloat( 0.0 ) # slot.translucency * slot.alpha 242 | self.wiresize = aseFloat( 1.0 ) 243 | 244 | # Material Definition 245 | self.shading = 'WHO CARES' # str( slot.specular_shader ).capitalize() 246 | self.xpfalloff = aseFloat( 0.0 ) 247 | self.xptype = 'Filter' 248 | self.falloff = 'In' 249 | self.soften = False 250 | self.diffusemap = cDiffusemap( None ) # slot.texture_slots[0] 251 | self.submtls = [] 252 | self.selfillum = aseFloat( 0.0 ) 253 | self.dump = '''\n\t\t*MATERIAL_NAME "{0}"\ 254 | \n\t\t*MATERIAL_CLASS "{1}"\ 255 | \n\t\t*MATERIAL_AMBIENT {2}\ 256 | \n\t\t*MATERIAL_DIFFUSE {3}\ 257 | \n\t\t*MATERIAL_SPECULAR {4}\ 258 | \n\t\t*MATERIAL_SHINE {5}\ 259 | \n\t\t*MATERIAL_SHINESTRENGTH {6}\ 260 | \n\t\t*MATERIAL_TRANSPARENCY {7}\ 261 | \n\t\t*MATERIAL_WIRESIZE {8}\ 262 | \n\t\t*MATERIAL_SHADING {9}\ 263 | \n\t\t*MATERIAL_XP_FALLOFF {10}\ 264 | \n\t\t*MATERIAL_SELFILLUM {11}\ 265 | \n\t\t*MATERIAL_FALLOFF {12}\ 266 | \n\t\t*MATERIAL_XP_TYPE {13}\ 267 | {14}\ 268 | '''.format( self.name, self.matClass, self.ambient, self.diffuse, self.specular, self.shine, self.shinestrength, self.transparency, self.wiresize, self.shading, self.xpfalloff, self.selfillum, self.falloff, self.xptype, self.diffdump() ) 269 | 270 | def diffdump( self ): 271 | for x in [self.diffusemap]: 272 | return x 273 | 274 | def __repr__( self ): 275 | return self.dump 276 | class cDiffusemap: 277 | def __init__( self, slot ): 278 | import os 279 | self.dump = '' 280 | if slot is None: 281 | self.name = 'default' 282 | self.mapclass = 'Bitmap' 283 | self.bitmap = 'None' 284 | else: 285 | self.name = slot.name 286 | if slot.texture.type == 'IMAGE': 287 | self.mapclass = 'Bitmap' 288 | self.bitmap = slot.texture.image.filepath 289 | if slot.texture.image.has_data: 290 | pass 291 | else: 292 | self.bitmap = '\\\\base\\' + self.bitmap.replace( '/', '\\' ) 293 | else: 294 | self.mapclass = 'Bitmap' 295 | self.bitmap = 'None' 296 | self.subno = 1 297 | self.amount = aseFloat( 1.0 ) 298 | self.type = 'Screen' 299 | self.uoffset = aseFloat( 0.0 ) 300 | self.voffset = aseFloat( 0.0 ) 301 | self.utiling = aseFloat( 1.0 ) 302 | self.vtiling = aseFloat( 1.0 ) 303 | self.angle = aseFloat( 0.0 ) 304 | self.blur = aseFloat( 1.0 ) 305 | self.bluroffset = aseFloat( 0.0 ) 306 | self.noiseamt = aseFloat( 1.0 ) 307 | self.noisesize = aseFloat( 1.0 ) 308 | self.noiselevel = 1 309 | self.noisephase = aseFloat( 0.0 ) 310 | self.bitmapfilter = 'Pyramidal' 311 | 312 | self.dump = '''\n\t\t*MAP_DIFFUSE {{\ 313 | \n\t\t\t*MAP_NAME "{0}"\ 314 | \n\t\t\t*MAP_CLASS "{1}"\ 315 | \n\t\t\t*MAP_SUBNO {2}\ 316 | \n\t\t\t*MAP_AMOUNT {3}\ 317 | \n\t\t\t*BITMAP "{4}"\ 318 | \n\t\t\t*MAP_TYPE {5}\ 319 | \n\t\t\t*UVW_U_OFFSET {6}\ 320 | \n\t\t\t*UVW_V_OFFSET {7}\ 321 | \n\t\t\t*UVW_U_TILING {8}\ 322 | \n\t\t\t*UVW_V_TILING {9}\ 323 | \n\t\t\t*UVW_ANGLE {10}\ 324 | \n\t\t\t*UVW_BLUR {11}\ 325 | \n\t\t\t*UVW_BLUR_OFFSET {12}\ 326 | \n\t\t\t*UVW_NOUSE_AMT {13}\ 327 | \n\t\t\t*UVW_NOISE_SIZE {14}\ 328 | \n\t\t\t*UVW_NOISE_LEVEL {15}\ 329 | \n\t\t\t*UVW_NOISE_PHASE {16}\ 330 | \n\t\t\t*BITMAP_FILTER {17}\ 331 | \n\t\t}}\ 332 | '''.format( self.name, self.mapclass, self.subno, self.amount, self.bitmap, self.type, self.uoffset, self.voffset, self.utiling, self.vtiling, self.angle, self.blur, self.bluroffset, self.noiseamt, self.noisesize, self.noiselevel, self.noisephase, self.bitmapfilter ) 333 | 334 | def __repr__( self ): 335 | return self.dump 336 | 337 | #== Geometry =============================================================== 338 | class cGeomObject: 339 | def __init__( self, object ): 340 | print( object.name + ": Constructing Geometry" ) 341 | global optionAllowMultiMats 342 | global currentMatId 343 | 344 | self.name = object.name 345 | self.prop_motionblur = 0 346 | self.prop_castshadow = 1 347 | self.prop_recvshadow = 1 348 | 349 | if optionAllowMultiMats: 350 | self.material_ref = 0 351 | else: 352 | self.material_ref = currentMatId 353 | 354 | self.nodetm = cNodeTM( object ) 355 | self.mesh = cMesh( object ) 356 | 357 | self.dump = '''\n*GEOMOBJECT {{\n\t*NODE_NAME "{0}"\n{1}\n{2}\n\t*PROP_MOTIONBLUR {3}\n\t*PROP_CASTSHADOW {4}\n\t*PROP_RECVSHADOW {5}\n\t*MATERIAL_REF {6}\n}}'''.format( self.name, self.nodetm, self.mesh, self.prop_motionblur, self.prop_castshadow, self.prop_recvshadow, self.material_ref ) 358 | 359 | def __repr__( self ): 360 | return self.dump 361 | class cNodeTM: 362 | def __init__( self, object ): 363 | self.name = object.name 364 | self.inherit_pos = '0 0 0' 365 | self.inherit_rot = '0 0 0' 366 | self.inherit_scl = '0 0 0' 367 | self.tm_row0 = '1.0000 0.0000 0.0000' 368 | self.tm_row1 = '0.0000 1.0000 0.0000' 369 | self.tm_row2 = '0.0000 0.0000 1.0000' 370 | self.tm_row3 = '0.0000 0.0000 0.0000' 371 | self.tm_pos = '0.0000 0.0000 0.0000' 372 | self.tm_rotaxis = '0.0000 0.0000 0.0000' 373 | self.tm_rotangle = '0.0000' 374 | self.tm_scale = '1.0000 1.0000 1.0000' 375 | self.tm_scaleaxis = '0.0000 0.0000 0.0000' 376 | self.tm_scaleaxisang = '0.0000' 377 | 378 | self.dump = '''\t*NODE_TM {{\ 379 | \n\t\t*NODE_NAME "{0}"\ 380 | \n\t\t*INHERIT_POS {1}\ 381 | \n\t\t*INHERIT_ROT {2}\ 382 | \n\t\t*INHERIT_SCL {3}\ 383 | \n\t\t*TM_ROW0 {4}\ 384 | \n\t\t*TM_ROW1 {5}\ 385 | \n\t\t*TM_ROW2 {6}\ 386 | \n\t\t*TM_ROW3 {7}\ 387 | \n\t\t*TM_POS {8}\ 388 | \n\t\t*TM_ROTAXIS {9}\ 389 | \n\t\t*TM_ROTANGLE {10}\ 390 | \n\t\t*TM_SCALE {11}\ 391 | \n\t\t*TM_SCALEAXIS {12}\ 392 | \n\t\t*TM_SCALEAXISANG {13}\ 393 | \n\t}}'''.format( self.name, self.inherit_pos, self.inherit_rot, self.inherit_scl, self.tm_row0, self.tm_row1, self.tm_row2, self.tm_row3, self.tm_pos, self.tm_rotaxis, self.tm_rotangle, self.tm_scale, self.tm_scaleaxis, self.tm_scaleaxisang ) 394 | 395 | def __repr__( self ): 396 | return self.dump 397 | class cMesh: 398 | def __init__( self, object ): 399 | bpy.ops.mesh.reveal 400 | 401 | if collisionObject( object ) == False: 402 | object.data.uv_layers.active_index = 0 403 | object.data.uv_layer_stencil_index = 0 404 | self.tvertlist = cTVertlist( object ) 405 | self.numtvertex = self.tvertlist.length 406 | self.numtvfaces = int(len( object.data.uv_layer_stencil.data ) / 3) 407 | self.tfacelist = cTFacelist( self.numtvfaces ) 408 | self.uvmapchannels = self.uvdump( object ) 409 | 410 | # OUTPUT 411 | self.tvertlist_str = '\n\t\t*MESH_TVERTLIST ' + str( self.tvertlist ) 412 | self.numtvertex_str = '\n\t\t*MESH_NUMTVERTEX ' + str( self.numtvertex ) 413 | self.numtvfaces_str = '\n\t\t*MESH_NUMTVFACES ' + str( self.numtvfaces ) 414 | self.tfacelist_str = '\n\t\t*MESH_TFACELIST ' + str( self.tfacelist ) 415 | 416 | else: 417 | self.tvertlist_str = '' 418 | self.numtvertex_str = '' 419 | self.numtvfaces_str = '' 420 | self.tfacelist_str = '' 421 | self.uvmapchannels = '' 422 | 423 | self.timevalue = '0' 424 | self.numvertex = len( object.data.vertices ) 425 | self.numfaces = len( object.data.polygons ) 426 | self.vertlist = cVertlist( object ) 427 | self.facelist = cFacelist( object ) 428 | 429 | # Vertex Paint 430 | if len( object.data.vertex_colors ) > 0: 431 | self.cvertlist = cCVertlist( object ) 432 | self.numcvertex = self.cvertlist.length 433 | self.numcvfaces = len( object.data.vertex_colors.data.polygons ) 434 | self.cfacelist = cCFacelist( self.numcvfaces ) 435 | # change them into strings now 436 | self.cvertlist = '\n{0}'.format( self.cvertlist ) 437 | self.numcvertex = '\n\t\t*MESH_NUMCVERTEX {0}'.format( self.numcvertex ) 438 | self.numcvfaces = '\n\t\t*MESH_NUMCVFACES {0}'.format( self.numcvfaces ) 439 | self.cfacelist = '\n{0}'.format( self.cfacelist ) 440 | else: 441 | self.cvertlist = '' 442 | self.numcvertex = '' 443 | self.numcvfaces = '' 444 | self.cfacelist = '' 445 | 446 | self.normals = cNormallist( object ) 447 | 448 | # get uv layer names for specified object 449 | def getUVLayerNames( self, object ): 450 | self.uvLayerNames = [] 451 | obj = object.data 452 | for uv in obj.uv_layers.keys(): 453 | self.uvLayerNames.append( str( uv ) ) 454 | 455 | def uvdump( self, object ): 456 | self.mappingchannels = '' 457 | # if there is more than 1 uv layer 458 | if collisionObject( object ) == False: 459 | self.getUVLayerNames( object ) 460 | # if len( self.uvLayerNames ) > 1: 461 | # save uv actives 462 | active_uv = object.data.uv_layers.active_index 463 | obj = object.data 464 | activeUV = 0 465 | for uvname in self.uvLayerNames: 466 | if activeUV == 0: 467 | activeUV += 1 468 | continue 469 | obj.uv_layers.active_index = activeUV 470 | obj.uv_layer_stencil_index = activeUV 471 | self.uvm_tvertlist = cTVertlist( object ) 472 | self.uvm_numtvertex = self.uvm_tvertlist.length 473 | self.uvm_numtvfaces = int(len( object.data.uv_layer_stencil.data ) / 3) 474 | self.uvm_tfacelist = cTFacelist( self.uvm_numtvfaces ) 475 | 476 | # if len(object.data.vertex_colors) > 0: 477 | # self.uvm_cvertlist = cCVertlist(object) 478 | # self.uvm_numcvertex = self.uvm_cvertlist.length 479 | # self.uvm_numcvfaces = len(object.data.vertex_colors[0].data) 480 | # self.uvm_cfacelist = cCFacelist(self.uvm_numcvfaces) 481 | 482 | # self.uvm_cvertlist = '\n{0}'.format(self.uvm_cvertlist) 483 | # self.uvm_numcvertex = '\n\t\t*MESH_NUMCVERTEX {0}'.format(self.uvm_numcvertex) 484 | # self.uvm_numcvfaces = '\n\t\t*MESH_NUMCVFACES {0}'.format(self.uvm_numcvfaces) 485 | # self.uvm_cfacelist = '\n{0}'.format(self.uvm_cfacelist) 486 | # else: 487 | self.uvm_numcvertex = '' 488 | self.uvm_numcvfaces = '' 489 | self.uvm_cvertlist = '' 490 | self.uvm_cfacelist = '' 491 | 492 | # print extra mapping channels 493 | self.mappingchannels += '''\n\t\t*MESH_MAPPINGCHANNEL {0} {{\n\t\t\t*MESH_NUMTVERTEX {1}\n\t\t\t*MESH_TVERTLIST {2}\n\t\t*MESH_NUMTVFACES {3}\n\t\t*MESH_TFACELIST {4}{5}{6}{7}{8}\n\t\t}}'''.format( str( activeUV + 1 ), self.uvm_numtvertex, self.uvm_tvertlist, self.uvm_numtvfaces, self.uvm_tfacelist, self.uvm_numcvertex, self.uvm_cvertlist, self.uvm_numcvfaces, self.uvm_cfacelist ) 494 | activeUV = activeUV + 1 495 | 496 | # restore uv actives 497 | object.data.uv_layers.active_index = active_uv 498 | 499 | return self.mappingchannels 500 | 501 | # UV textures go AFTER MESH_FACE_LIST 502 | # MESH_NUMTVERTEX, MESH_TVERTLIST, MESH_NUMTVFACES, MESH_TFACELIST 503 | def __repr__( self ): 504 | temp = '''\t*MESH {{\n\t\t*TIMEVALUE {0}\n\t\t*MESH_NUMVERTEX {1}\n\t\t*MESH_NUMFACES {2}\n\t\t*MESH_VERTEX_LIST {3}\n\t\t*MESH_FACE_LIST {4}{5}{6}{7}{8}{9}{10}{11}{12}{13}\n{14}\n\t}}'''.format( self.timevalue, self.numvertex, self.numfaces, self.vertlist, self.facelist, self.numtvertex_str, self.tvertlist_str, self.numtvfaces_str, self.tfacelist_str, self.numcvertex, self.cvertlist, self.numcvfaces, self.cfacelist, self.uvmapchannels, self.normals ) 505 | return temp 506 | class cVertlist: 507 | def __init__( self, object ): 508 | self.vertlist = [] 509 | for data in object.data.vertices: 510 | temp = cVert( data.index, data.co.to_tuple( 4 ) ) 511 | self.vertlist.append( temp ) 512 | 513 | def dump( self ): 514 | temp = '' 515 | for x in self.vertlist: 516 | temp += str( x ) 517 | return temp 518 | 519 | def __repr__( self ): 520 | return '''{{\n{0}\t\t}}'''.format( self.dump() ) 521 | class cVert: 522 | def __init__( self, index, coord ): 523 | global optionScale 524 | 525 | self.index = index 526 | self.x = aseFloat( coord[0] * optionScale ) 527 | self.y = aseFloat( coord[1] * optionScale ) 528 | self.z = aseFloat( coord[2] * optionScale ) 529 | 530 | def __repr__( self ): 531 | return '''\t\t\t*MESH_VERTEX {0} {1} {2} {3}\n'''.format( self.index, self.x, self.y, self.z ) 532 | class cFacelist: 533 | def __init__( self, object ): 534 | global optionAllowMultiMats 535 | global matList 536 | global numMats 537 | global currentMatId 538 | 539 | self.facelist = [] 540 | sgID = 0 541 | 542 | # Define smoothing groups (if enabled) 543 | if ( collisionObject( object ) == False ): 544 | if ( optionSmoothingGroups ): 545 | self.smoothing_groups = defineSmoothing( self, object ) 546 | else: 547 | self.smoothing_groups = '' 548 | 549 | for face in object.data.polygons: 550 | if optionAllowMultiMats: 551 | if ( collisionObject( object ) == False ): 552 | self.matid = matList.index( object.material_slots[face.material_index].material.name ) 553 | else: 554 | self.matid = 0 555 | else: 556 | self.matid = currentMatId 557 | if ( collisionObject( object ) == False ): 558 | if ( optionSmoothingGroups ): 559 | for group in self.smoothing_groups: 560 | if group.count( face.index ) == 0: 561 | continue 562 | else: 563 | #TODO: Compress sg's 564 | index = self.smoothing_groups.index( group ) 565 | sgID = index % 32 566 | 567 | temp = '''\t\t\t*MESH_FACE {0}: A: {1} B: {2} C: {3} AB: 0 BC: 0 CA: 0 *MESH_SMOOTHING {4} *MESH_MTLID {5}\n'''.format( face.index, face.vertices[0], face.vertices[1], face.vertices[2], sgID, self.matid ) 568 | self.facelist.append( temp ) 569 | 570 | if currentMatId < numMats - 1: 571 | currentMatId += 1 572 | else: 573 | currentMatId = 0 574 | 575 | def dump( self ): 576 | temp = '' 577 | for x in self.facelist: 578 | temp = temp + str( x ) 579 | return temp 580 | 581 | def __repr__( self ): 582 | return '''{{\n{0}\t\t}}'''.format( self.dump() ) 583 | class cTVertlist: 584 | def __init__( self, object ): 585 | self.vertlist = [] 586 | 587 | # update tessface 588 | mesh = bpy.context.object.data 589 | mesh.update() 590 | mesh.calc_loop_triangles() 591 | 592 | for index, face in enumerate( object.data.loop_triangles ): 593 | if len( object.data.uv_layers ) == 0: 594 | raise Error( "Error: No UV texture data for " + object.name ) 595 | else: 596 | temp = cTVert( ( index * 3 ) + 0, object.data.uv_layers[object.data.uv_layers.active_index].data[face.index * 3 + 0].uv ) 597 | self.vertlist.append( temp ) 598 | temp = cTVert( ( index * 3 ) + 1, object.data.uv_layers[object.data.uv_layers.active_index].data[face.index * 3 + 1].uv ) 599 | self.vertlist.append( temp ) 600 | temp = cTVert( ( index * 3 ) + 2, object.data.uv_layers[object.data.uv_layers.active_index].data[face.index * 3 + 2].uv ) 601 | self.vertlist.append( temp ) 602 | 603 | self.length = len( self.vertlist ) 604 | 605 | def dump( self ): 606 | temp = '' 607 | for x in self.vertlist: 608 | temp += str( x ) 609 | return temp 610 | 611 | def __repr__( self ): 612 | return '''{{\n{0}\t\t}}'''.format( self.dump() ) 613 | class cTVert: 614 | def __init__( self, index, coord ): 615 | self.index = index 616 | self.u = aseFloat( coord[0] ) 617 | self.v = aseFloat( coord[1] ) 618 | 619 | def __repr__( self ): 620 | return '''\t\t\t*MESH_TVERT {0} {1} {2} 0.0000\n'''.format( self.index, self.u, self.v ) 621 | class cTFacelist: 622 | def __init__( self, facecount ): 623 | self.facelist = [] 624 | for data in range( facecount ): 625 | temp = cTFace( data ) 626 | self.facelist.append( temp ) 627 | 628 | def dump( self ): 629 | temp = '' 630 | for x in self.facelist: 631 | temp = temp + str( x ) 632 | return temp 633 | 634 | def __repr__( self ): 635 | return '''{{\n{0}\t\t}}'''.format( self.dump() ) 636 | class cTFace: 637 | def __init__( self, x ): 638 | self.index = x 639 | self.vertices = [] 640 | self.vertices.append( x * 3 ) 641 | self.vertices.append( ( x * 3 ) + 1 ) 642 | self.vertices.append( ( x * 3 ) + 2 ) 643 | 644 | def __repr__( self ): 645 | return '''\t\t\t*MESH_TFACE {0} {1} {2} {3}\n'''.format( self.index, self.vertices[0], self.vertices[1], self.vertices[2] ) 646 | class cCVertlist: 647 | def __init__( self, object ): 648 | self.vertlist = [] 649 | self.index = 0 650 | 651 | # Blender 2.63+ 652 | bpy.ops.object.mode_set( mode = 'OBJECT' ) 653 | object.data.calc_loop_triangles() 654 | 655 | for face in object.data.loop_triangles: 656 | temp = object.data.vertex_colors[0].data[face.index * 3 + 0].color 657 | self.vertlist.append( cCVert( self.index, temp ) ) 658 | temp = object.data.vertex_colors[0].data[face.index * 3 + 1].color 659 | self.vertlist.append( cCVert( self.index + 1, temp ) ) 660 | temp = object.data.vertex_colors[0].data[face.index * 3 + 2].color 661 | self.vertlist.append( cCVert( self.index + 2, temp ) ) 662 | self.index += 3 663 | 664 | self.length = len( self.vertlist ) 665 | 666 | def dump( self ): 667 | temp = '' 668 | for x in self.vertlist: 669 | temp = temp + str( x ) 670 | return temp 671 | 672 | def __repr__( self ): 673 | return '''\t\t*MESH_CVERTLIST {{\n{0}\t\t}}'''.format( self.dump() ) 674 | class cCVert: 675 | def __init__( self, index, temp ): 676 | self.index = index 677 | self.r = aseFloat( float( temp[0] ) ) 678 | self.g = aseFloat( float( temp[1] ) ) 679 | self.b = aseFloat( float( temp[2] ) ) 680 | 681 | def __repr__( self ): 682 | return '''\t\t\t*MESH_VERTCOL {0} {1} {2} {3}\n'''.format( self.index, self.r, self.g, self.b ) 683 | class cCFacelist: 684 | def __init__( self, facecount ): 685 | temp = [0 for x in range( facecount )] 686 | self.facelist = [] 687 | for index, data in enumerate( temp ): 688 | self.facelist.append( cCFace( index, data ) ) 689 | 690 | def dump( self ): 691 | temp = '' 692 | for x in self.facelist: 693 | temp = temp + str( x ) 694 | return temp 695 | 696 | def __repr__( self ): 697 | return '''\t\t*MESH_CFACELIST {{\n{0}\t\t}}'''.format( self.dump() ) 698 | class cCFace: 699 | def __init__( self, index, data ): 700 | self.index = index 701 | self.vertices = [] 702 | self.vertices.append( index * 3 ) 703 | self.vertices.append( ( index * 3 ) + 1 ) 704 | self.vertices.append( ( index * 3 ) + 2 ) 705 | 706 | def __repr__( self ): 707 | return '''\t\t\t*MESH_CFACE {0} {1} {2} {3}\n'''.format( self.index, self.vertices[0], self.vertices[1], self.vertices[2] ) 708 | class cNormallist: 709 | def __init__( self, object ): 710 | self.normallist = [] 711 | for face in object.data.polygons: 712 | self.normallist.append( cNormal( face, object ) ) 713 | 714 | def dump( self ): 715 | temp = '' 716 | for x in self.normallist: 717 | temp = temp + str( x ) 718 | return temp 719 | 720 | def __repr__( self ): 721 | return '''\t\t*MESH_NORMALS {{\n{0}\t\t}}'''.format( self.dump() ) 722 | class cNormal: 723 | def __init__( self, face, object ): 724 | self.faceindex = face.index 725 | self.facenormal = [aseFloat( x ) for x in face.normal.to_tuple( 4 )] 726 | self.vertnormals = [] 727 | for x in face.vertices: 728 | self.vertnormals.append( [x, [aseFloat( y ) for y in object.data.vertices[x].normal.to_tuple( 4 )]] ) 729 | 730 | def __repr__( self ): 731 | return '''\t\t\t*MESH_FACENORMAL {0} {1} {2} {3}\n\t\t\t\t*MESH_VERTEXNORMAL {4} {5} {6} {7}\n\t\t\t\t*MESH_VERTEXNORMAL {8} {9} {10} {11}\n\t\t\t\t*MESH_VERTEXNORMAL {12} {13} {14} {15}\n'''.format( self.faceindex, self.facenormal[0], self.facenormal[1], self.facenormal[2], self.vertnormals[0][0], self.vertnormals[0][1][0], self.vertnormals[0][1][1], self.vertnormals[0][1][2], self.vertnormals[1][0], self.vertnormals[1][1][0], self.vertnormals[1][1][1], self.vertnormals[1][1][2], self.vertnormals[2][0], self.vertnormals[2][1][0], self.vertnormals[2][1][1], self.vertnormals[2][1][2] ) 732 | 733 | #== Smoothing Groups and Helper Methods ================================= 734 | def defineSmoothing( self, object ): 735 | print( object.name + ": Constructing Smoothing Groups" ) 736 | 737 | seam_edge_list = [] 738 | sharp_edge_list = [] 739 | 740 | _mode = bpy.context.active_object.mode 741 | bpy.ops.object.mode_set( mode = 'EDIT' ) 742 | bpy.ops.mesh.select_all( action = 'DESELECT' ) 743 | setSelMode( 'EDGE' ) 744 | 745 | # Get seams and clear them 746 | bpy.ops.object.mode_set( mode = 'OBJECT' ) 747 | for edge in object.data.edges: 748 | if edge.use_seam: 749 | seam_edge_list.append( edge.index ) 750 | edge.select = True 751 | 752 | bpy.ops.object.mode_set( mode = 'EDIT' ) 753 | bpy.ops.mesh.select_all( action = 'SELECT' ) 754 | bpy.ops.mesh.mark_seam( clear = True ) 755 | 756 | # Get sharp edges, convert them to seams 757 | bpy.ops.mesh.select_all( action = 'DESELECT' ) 758 | bpy.ops.object.mode_set( mode = 'OBJECT' ) 759 | for edge in object.data.edges: 760 | if edge.use_edge_sharp: 761 | sharp_edge_list.append( edge ) 762 | edge.select = True 763 | 764 | bpy.ops.object.mode_set( mode = 'EDIT' ) 765 | bpy.ops.mesh.mark_seam() 766 | 767 | bpy.ops.mesh.select_all( action = 'DESELECT' ) 768 | 769 | smoothing_groups = [] 770 | face_list = [] 771 | 772 | mode = getSelMode( self, False ) 773 | setSelMode( 'FACE' ) 774 | 775 | for face in object.data.polygons: 776 | face_list.append( face.index ) 777 | 778 | while len( face_list ) > 0: 779 | bpy.ops.object.mode_set( mode = 'OBJECT' ) 780 | object.data.polygons[face_list[0]].select = True 781 | bpy.ops.object.mode_set( mode = 'EDIT' ) 782 | bpy.ops.mesh.select_linked( delimit = {'SEAM'} ) 783 | 784 | # TODO - update when API is updated 785 | selected_faces = getSelectedFaces( self, True ) 786 | smoothing_groups.append( selected_faces ) 787 | for face_index in selected_faces: 788 | if face_list.count( face_index ) > 0: 789 | face_list.remove( face_index ) 790 | bpy.ops.mesh.select_all( action = 'DESELECT' ) 791 | 792 | setSelMode( mode, False ) 793 | 794 | # Clear seams created by sharp edges 795 | bpy.ops.object.mode_set( mode = 'OBJECT' ) 796 | for edge in object.data.edges: 797 | if edge.use_seam: 798 | edge.select = True 799 | 800 | bpy.ops.object.mode_set( mode = 'EDIT' ) 801 | bpy.ops.mesh.mark_seam( clear = True ) 802 | 803 | bpy.ops.mesh.select_all( action = 'DESELECT' ) 804 | # Restore original uv seams 805 | bpy.ops.object.mode_set( mode = 'OBJECT' ) 806 | for edge_index in seam_edge_list: 807 | object.data.edges[edge_index].select = True 808 | 809 | bpy.ops.object.mode_set( mode = 'EDIT' ) 810 | bpy.ops.mesh.mark_seam() 811 | 812 | print( '\t' + str( len( smoothing_groups ) ) + ' smoothing groups found.' ) 813 | return smoothing_groups 814 | 815 | #=========================================================================== 816 | # // General Helpers 817 | #=========================================================================== 818 | 819 | # Check if the mesh is a collider 820 | # Return True if collision model, else: false 821 | def collisionObject( object ): 822 | collisionPrefixes = ['UCX_', 'UBX_', 'USX_', 'MCDCX_'] 823 | for prefix in collisionPrefixes: 824 | if object.name.find( str( prefix ) ) >= 0: 825 | return True 826 | return False 827 | 828 | # Set the selection mode 829 | def setSelMode( mode, default = True ): 830 | if default: 831 | if mode == 'VERT': 832 | bpy.context.tool_settings.mesh_select_mode = [True, False, False] 833 | elif mode == 'EDGE': 834 | bpy.context.tool_settings.mesh_select_mode = [False, True, False] 835 | elif mode == 'FACE': 836 | bpy.context.tool_settings.mesh_select_mode = [False, False, True] 837 | else: 838 | return False 839 | else: 840 | bpy.context.tool_settings.mesh_select_mode = mode 841 | return True 842 | def getSelMode( self, default = True ): 843 | if default: 844 | if bpy.context.tool_settings.mesh_select_mode[0] == True: 845 | return 'VERT' 846 | elif bpy.context.tool_settings.mesh_select_mode[1] == True: 847 | return 'EDGE' 848 | elif bpy.context.tool_settings.mesh_select_mode[2] == True: 849 | return 'FACE' 850 | return False 851 | else: 852 | mode = [] 853 | for value in bpy.context.tool_settings.mesh_select_mode: 854 | mode.append( value ) 855 | 856 | return mode 857 | def getSelectedFaces( self, index = False ): 858 | selected_faces = [] 859 | # Update mesh data 860 | bpy.ops.object.editmode_toggle() 861 | bpy.ops.object.editmode_toggle() 862 | 863 | _mode = bpy.context.active_object.mode 864 | bpy.ops.object.mode_set( mode = 'EDIT' ) 865 | 866 | object = bpy.context.active_object 867 | for face in object.data.polygons: 868 | if face.select == True: 869 | if index == False: 870 | selected_faces.append( face ) 871 | else: 872 | selected_faces.append( face.index ) 873 | 874 | bpy.ops.object.mode_set( mode = _mode ) 875 | 876 | return selected_faces 877 | 878 | #== Core =================================================================== 879 | 880 | from bpy_extras.io_utils import ExportHelper 881 | from bpy.props import StringProperty, BoolProperty, FloatProperty 882 | 883 | class ExportAse( bpy.types.Operator, ExportHelper ): 884 | '''Load an Ascii Scene Export File''' 885 | bl_idname = "export.ase" 886 | bl_label = "Export" 887 | __doc__ = "Ascii Scene Exporter (.ase)" 888 | filename_ext = ".ase" 889 | filter_glob : StringProperty( default = "*.ase", options = {'HIDDEN'} ) 890 | 891 | filepath : StringProperty( 892 | name = "File Path", 893 | description = "File path used for exporting the ASE file", 894 | maxlen = 1024, 895 | default = "" ) 896 | 897 | option_triangulate : BoolProperty( 898 | name = "Triangulate", 899 | description = "Triangulates all exportable objects", 900 | default = True ) 901 | 902 | option_normals : BoolProperty( 903 | name = "Recalculate Normals", 904 | description = "Recalculate normals before exporting", 905 | default = True ) 906 | 907 | option_remove_doubles : BoolProperty( 908 | name = "Remove Doubles", 909 | description = "Remove any duplicate vertices before exporting", 910 | default = True ) 911 | 912 | option_apply_scale : BoolProperty( 913 | name = "Scale", 914 | description = "Apply scale transformation", 915 | default = True ) 916 | 917 | option_apply_location : BoolProperty( 918 | name = "Location", 919 | description = "Apply location transformation", 920 | default = True ) 921 | 922 | option_apply_rotation : BoolProperty( 923 | name = "Rotation", 924 | description = "Apply rotation transformation", 925 | default = True ) 926 | 927 | option_smoothinggroups : BoolProperty( 928 | name = "Smoothing Groups", 929 | description = "Construct hard edge islands as smoothing groups", 930 | default = True ) 931 | 932 | option_separate : BoolProperty( 933 | name = "Separate", 934 | description = "A separate ASE file for every selected object", 935 | default = False ) 936 | 937 | option_submaterials : BoolProperty( 938 | name = "Use Submaterials (UDK)", 939 | description = "Export a single material with multiple sub materials", 940 | default = True ) 941 | 942 | option_allowmultimats : BoolProperty( 943 | name = "Allow Multiple Materials (UDK)", 944 | description = "Allow multiple materials per geometry object", 945 | default = True ) 946 | 947 | option_scale : FloatProperty( 948 | name = "Scale", 949 | description = "Object scaling factor (default: 1.0)", 950 | min = 0.01, 951 | max = 1000.0, 952 | soft_min = 0.01, 953 | soft_max = 1000.0, 954 | default = 16.0 ) 955 | 956 | def draw( self, context ): 957 | layout = self.layout 958 | 959 | box = layout.box() 960 | box.label(text='Essentials:' ) 961 | box.prop( self, 'option_triangulate' ) 962 | box.prop( self, 'option_normals' ) 963 | box.prop( self, 'option_remove_doubles' ) 964 | box.label( text="Transformations:" ) 965 | box.prop( self, 'option_apply_scale' ) 966 | box.prop( self, 'option_apply_rotation' ) 967 | box.prop( self, 'option_apply_location' ) 968 | box.label( text="Materials:" ) 969 | box.prop( self, 'option_submaterials' ) 970 | box.prop( self, 'option_allowmultimats' ) 971 | box.label( text="Advanced:" ) 972 | box.prop( self, 'option_scale' ) 973 | box.prop( self, 'option_smoothinggroups' ) 974 | 975 | @classmethod 976 | def poll( cls, context ): 977 | active = context.active_object 978 | selected = context.selected_objects 979 | camera = context.scene.camera 980 | ok = selected or camera 981 | return ok 982 | 983 | def writeASE( self, filename, data ): 984 | print( '\nWriting', filename ) 985 | try: 986 | file = open( filename, 'w' ) 987 | except IOError: 988 | print( 'Error: The file could not be written to. Aborting.' ) 989 | else: 990 | file.write( data ) 991 | file.close() 992 | 993 | def execute( self, context ): 994 | start = time.clock() 995 | 996 | global optionScale 997 | global optionSubmaterials 998 | global optionSmoothingGroups 999 | global optionAllowMultiMats 1000 | 1001 | global aseHeader 1002 | global aseScene 1003 | global aseMaterials 1004 | global aseGeometry 1005 | 1006 | global currentMatId 1007 | global numMats 1008 | global matList 1009 | 1010 | # Set globals and reinitialize ase components 1011 | aseHeader = '' 1012 | aseScene = '' 1013 | aseMaterials = '' 1014 | aseGeometry = '' 1015 | 1016 | optionScale = self.option_scale 1017 | optionSubmaterials = self.option_submaterials 1018 | optionSmoothingGroups = self.option_smoothinggroups 1019 | optionAllowMultiMats = self.option_allowmultimats 1020 | 1021 | matList = [] 1022 | currentMatId = 0 1023 | numMats = 0 1024 | 1025 | # Build ASE Header, Scene 1026 | print( '\nAscii Scene Export by MCampagnini\n' ) 1027 | print( 'Objects selected: ' + str( len( bpy.context.selected_objects ) ) ) 1028 | aseHeader = str( cHeader() ) 1029 | aseScene = str( cScene() ) 1030 | aseMaterials = str( cMaterials() ) 1031 | 1032 | # Apply applicable options 1033 | for object in bpy.context.selected_objects: 1034 | if object.type == 'MESH': 1035 | bpy.context.view_layer.objects.active = object 1036 | # object.select = True 1037 | 1038 | # Options 1039 | bpy.ops.object.mode_set( mode = 'EDIT' ) 1040 | if self.option_remove_doubles: 1041 | bpy.ops.object.mode_set( mode = 'EDIT' ) 1042 | bpy.ops.mesh.select_all( action = 'SELECT' ) 1043 | bpy.ops.mesh.remove_doubles() 1044 | if self.option_triangulate: 1045 | print( object.name + ': Converting to triangles' ) 1046 | bpy.ops.mesh.select_all( action = 'SELECT' ) 1047 | bpy.ops.mesh.quads_convert_to_tris() 1048 | if self.option_normals: 1049 | print( object.name + ': Recalculating normals' ) 1050 | bpy.ops.object.mode_set( mode = 'EDIT' ) 1051 | bpy.ops.mesh.select_all( action = 'SELECT' ) 1052 | bpy.ops.mesh.normals_make_consistent() 1053 | 1054 | # Transformations 1055 | bpy.ops.object.mode_set( mode = 'OBJECT' ) 1056 | bpy.ops.object.transform_apply( location = self.option_apply_location, rotation = self.option_apply_rotation, scale = self.option_apply_scale ) 1057 | 1058 | #Construct ASE Geometry Nodes 1059 | aseGeometry += str( cGeomObject( object ) ) 1060 | 1061 | else: 1062 | continue 1063 | 1064 | aseModel = '' 1065 | aseModel += aseHeader 1066 | aseModel += aseScene 1067 | aseModel += aseMaterials 1068 | aseModel += aseGeometry 1069 | 1070 | # Write the ASE file 1071 | self.writeASE( self.filepath, aseModel ) 1072 | 1073 | lapse = ( time.clock() - start ) 1074 | print( 'Completed in ' + str( lapse ) + ' seconds' ) 1075 | 1076 | return {'FINISHED'} 1077 | 1078 | def menu_func( self, context ): 1079 | self.layout.operator( ExportAse.bl_idname, text = "ASCII Scene Exporter (.ase)" ) 1080 | 1081 | def register(): 1082 | bpy.utils.register_class( ExportAse ) 1083 | bpy.types.TOPBAR_MT_file_export.append( menu_func ) 1084 | 1085 | def unregister(): 1086 | bpy.utils.unregister_class( ExportAse ) 1087 | bpy.types.TOPBAR_MT_file_export.remove( menu_func ) 1088 | 1089 | if __name__ == "__main__": 1090 | register() 1091 | --------------------------------------------------------------------------------