|
| 1 | +import bpy |
| 2 | +import bmesh |
| 3 | +from mathutils import Vector |
| 4 | +from os import path, remove |
| 5 | +import sys |
| 6 | + |
| 7 | + |
| 8 | +HELP_MESSAGE = 'Usage: blender -b -P BlenderSup.py -- [OPTION] ...\n\ |
| 9 | +Mandatory arguments are:\n\ |
| 10 | +\t-t, --text [VALUE]\t\tText to generate from\n\ |
| 11 | +Optionary arguments are:\n\ |
| 12 | +\t-e, --extrusion [VALUE]\t\tHeight of text\n\ |
| 13 | +\t-b, --base [VALUE]\t\tHeight of base\n\ |
| 14 | +\t-p, --padding [VALUE]\t\tExtra padding around text\n\ |
| 15 | +\t-l, --location [VALUE]\t\tPath to save to\n\ |
| 16 | +BlenderSup allows you to generate a TextObj.obj\ |
| 17 | +file from the given arguments' |
| 18 | + |
| 19 | + |
| 20 | +class Blender(object): |
| 21 | + """A blender static class |
| 22 | +
|
| 23 | + Unifies and simplifies common simple tasks required for the script to work |
| 24 | +
|
| 25 | + """ |
| 26 | + |
| 27 | + @staticmethod |
| 28 | + def removeStartingElements(): |
| 29 | + """Removes the starting elements in the blender scene""" |
| 30 | + |
| 31 | + bpy.ops.object.select_all(action='SELECT') |
| 32 | + bpy.ops.object.delete() |
| 33 | + |
| 34 | + @staticmethod |
| 35 | + def deselectAll(): |
| 36 | + """Deselects all objects""" |
| 37 | + |
| 38 | + for obj in bpy.data.objects: |
| 39 | + obj.select = False |
| 40 | + |
| 41 | + @staticmethod |
| 42 | + def join(objectName1, objectName2): |
| 43 | + """Joins 2 objects |
| 44 | +
|
| 45 | + Args: |
| 46 | + [objectName1] (str): Object name to join to |
| 47 | + [objectName2] (str): Object name to join to [objectName1] |
| 48 | +
|
| 49 | + """ |
| 50 | + |
| 51 | + bpy.data.objects[objectName1].select = True |
| 52 | + bpy.data.objects[objectName2].select = True |
| 53 | + bpy.context.scene.objects.active = bpy.data.objects[objectName1] |
| 54 | + bpy.ops.object.join() |
| 55 | + Blender.deselectAll() |
| 56 | + |
| 57 | + |
| 58 | +class BlenderText(object): |
| 59 | + """A Blender text textcurve/mesh object |
| 60 | +
|
| 61 | + Allows easy creation of blender text objects |
| 62 | +
|
| 63 | + """ |
| 64 | + |
| 65 | + def __init__(self, text=None, typeT='mesh', meshName="WordMesh"): |
| 66 | + """The BlenderText initializer |
| 67 | +
|
| 68 | + Args: |
| 69 | + [text] (str): Text to write |
| 70 | + [typeT] (str): ['mesh'/'curve'/None] What should be created out of |
| 71 | + the text |
| 72 | +
|
| 73 | + """ |
| 74 | + |
| 75 | + self.text = text |
| 76 | + |
| 77 | + if typeT is 'mesh': |
| 78 | + self.toCurve() |
| 79 | + self.toMesh(meshName) |
| 80 | + elif typeT is 'curve': |
| 81 | + self.toCurve() |
| 82 | + |
| 83 | + @property |
| 84 | + def text(self): |
| 85 | + """The text to write""" |
| 86 | + |
| 87 | + return self.__text |
| 88 | + |
| 89 | + @text.setter |
| 90 | + def text(self, text): |
| 91 | + self.__text = text |
| 92 | + |
| 93 | + @property |
| 94 | + def dimensions(self): |
| 95 | + """Dimensions of the mesh""" |
| 96 | + if self.textMesh: |
| 97 | + return self.textMesh.dimensions |
| 98 | + elif self.textCurve: |
| 99 | + return self.textCurve.dimensions |
| 100 | + else: |
| 101 | + raise Exception('Object not yet created') |
| 102 | + |
| 103 | + @property |
| 104 | + def location(self): |
| 105 | + """Location of the mesh""" |
| 106 | + |
| 107 | + if self.textMesh: |
| 108 | + return self.textMesh.location |
| 109 | + elif self.textCurve: |
| 110 | + return self.textCurve.location |
| 111 | + else: |
| 112 | + raise Exception('Object not yet created') |
| 113 | + |
| 114 | + @location.setter |
| 115 | + def location(self, loc): |
| 116 | + |
| 117 | + self.textMesh.location[0] = loc[0] |
| 118 | + self.textMesh.location[1] = loc[1] |
| 119 | + self.textMesh.location[2] = loc[2] |
| 120 | + |
| 121 | + def centerOrigin(self): |
| 122 | + """Centers the origin to the geometry""" |
| 123 | + |
| 124 | + bpy.ops.object.select_all(action='SELECT') |
| 125 | + bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY') |
| 126 | + |
| 127 | + def toCurve(self): |
| 128 | + """Creates a textCurve out of self.text""" |
| 129 | + |
| 130 | + if not self.text: |
| 131 | + raise TypeError('None value given') |
| 132 | + bpy.ops.object.text_add() |
| 133 | + self.textCurve = bpy.context.object |
| 134 | + self.textCurve.data.body = self.text |
| 135 | + |
| 136 | + self.centerOrigin() |
| 137 | + |
| 138 | + def toMesh(self, meshName='WordMesh'): |
| 139 | + """Creates a mesh out of self.textCurve and centers the origin to the geometry |
| 140 | +
|
| 141 | + Note: |
| 142 | + Will raise TypeError if self.text is not set |
| 143 | +
|
| 144 | + """ |
| 145 | + |
| 146 | + if not self.text: |
| 147 | + raise TypeError('None value given') |
| 148 | + if not self.textCurve: |
| 149 | + self.toCurve() |
| 150 | + |
| 151 | + context = bpy.context |
| 152 | + scene = context.scene |
| 153 | + |
| 154 | + textMesh = self.textCurve.to_mesh(scene, False, 'PREVIEW') |
| 155 | + self.textMesh = bpy.data.objects.new(meshName, textMesh) |
| 156 | + scene.objects.link(self.textMesh) |
| 157 | + self.textMesh.matrix_world = self.textCurve.matrix_world |
| 158 | + scene.objects.unlink(self.textCurve) |
| 159 | + |
| 160 | + def extrude(self, amount): |
| 161 | + """Extrudes the self.textMesh |
| 162 | +
|
| 163 | + Args: |
| 164 | + amount (num): Amount to extrude |
| 165 | + """ |
| 166 | + |
| 167 | + me = self.textMesh.data |
| 168 | + bm = bmesh.new() |
| 169 | + bm.from_mesh(me) |
| 170 | + |
| 171 | + faces = bm.faces[:] |
| 172 | + |
| 173 | + for face in faces: |
| 174 | + r = bmesh.ops.extrude_discrete_faces(bm, faces=[face]) |
| 175 | + bmesh.ops.translate(bm, vec=Vector((0, 0, amount)), |
| 176 | + verts=r['faces'][0].verts) |
| 177 | + |
| 178 | + bm.to_mesh(me) |
| 179 | + me.update() |
| 180 | + |
| 181 | + |
| 182 | +class BlenderCube(object): |
| 183 | + """A Blender text textcurve/mesh object |
| 184 | +
|
| 185 | + Allows easy creation of blender cubes |
| 186 | +
|
| 187 | + """ |
| 188 | + |
| 189 | + def __init__(self, name='Cube', |
| 190 | + x=0, y=0, z=0, width=0, length=0, height=0): |
| 191 | + """The BlenderCube initializer |
| 192 | +
|
| 193 | + Args: |
| 194 | + [name] (str): Name of the object |
| 195 | + [x] (num): Center location on the X axis |
| 196 | + [y] (num): Center location on the Y axis |
| 197 | + [z] (num): Center location on the Z axis |
| 198 | + [width] (num): X axis dimension |
| 199 | + [length] (num): Y axis dimension |
| 200 | + [height] (num): Z axis dimension |
| 201 | + """ |
| 202 | + |
| 203 | + bpy.ops.mesh.primitive_cube_add(location=(x, y, z)) |
| 204 | + bpy.context.object.name = name |
| 205 | + bpy.context.object.dimensions = width, length, height |
| 206 | + Blender.deselectAll() |
| 207 | + |
| 208 | + |
| 209 | +argv = sys.argv |
| 210 | +argv = argv[argv.index('--') + 1:] |
| 211 | + |
| 212 | +R_TEXT_TO_SET = "" |
| 213 | +R_EXTRUSION_VALUE = 0 |
| 214 | +R_BASE_HEIGHT = 0 |
| 215 | +R_PADDING = 0 |
| 216 | +R_LOCATION = path.join(path.expanduser('~'), 'Desktop', 'TextObject.obj') |
| 217 | + |
| 218 | + |
| 219 | +def createMesh(): |
| 220 | + Blender.removeStartingElements() |
| 221 | + |
| 222 | + text = BlenderText(R_TEXT_TO_SET, 'mesh', 'TextMesh') |
| 223 | + text.location = 0, 0, 0 |
| 224 | + text.extrude(R_EXTRUSION_VALUE) |
| 225 | + |
| 226 | + BlenderCube('Base', 0, 0, 0 - R_BASE_HEIGHT / 2, |
| 227 | + text.dimensions[0] + R_PADDING * 2, |
| 228 | + text.dimensions[1] + R_PADDING * 2, |
| 229 | + R_BASE_HEIGHT) |
| 230 | + |
| 231 | + Blender.join('TextMesh', 'Base') |
| 232 | + |
| 233 | + bpy.ops.export_scene.obj(filepath=R_LOCATION) |
| 234 | + remove(path.join(path.expanduser('~'), 'Desktop', 'TextObject.mtl')) |
| 235 | + |
| 236 | + |
| 237 | +if '--help' in argv: |
| 238 | + print(HELP_MESSAGE) |
| 239 | +else: |
| 240 | + if '-t' in argv: |
| 241 | + R_TEXT_TO_SET = argv[argv.index('-t') + 1] |
| 242 | + elif '--text' in argv: |
| 243 | + R_TEXT_TO_SET = argv[argv.index('--text') + 1] |
| 244 | + if '-e' in argv: |
| 245 | + R_EXTRUSION_VALUE = float(argv[argv.index('-e') + 1]) |
| 246 | + elif '--extrusion' in argv: |
| 247 | + R_EXTRUSION_VALUE = float(argv[argv.index('--extrusion') + 1]) |
| 248 | + if '-b' in argv: |
| 249 | + R_BASE_HEIGHT = float(argv[argv.index('-b') + 1]) |
| 250 | + elif '--base' in argv: |
| 251 | + R_BASE_HEIGHT = float(argv[argv.index('--base') + 1]) |
| 252 | + if '-p' in argv: |
| 253 | + R_PADDING = float(argv[argv.index('-p') + 1]) |
| 254 | + elif '--padding' in argv: |
| 255 | + R_PADDING = float(argv[argv.index('--padding') + 1]) |
| 256 | + if '-l' in argv: |
| 257 | + R_LOCATION = argv[argv.index('-l') + 1] |
| 258 | + elif '--location' in argv: |
| 259 | + R_LOCATION = argv[argv.index('--location') + 1] |
| 260 | + |
| 261 | + createMesh() |
0 commit comments