Best Python code snippet using slash
face_importer.py
Source:face_importer.py
...28 global SELECTED_OBJECT_COLLECTION_NAME29 global FACE_BLEND_FILE30 if SELECTED_OBJECT_FOUND == 0:31 return32 toolbox.deselect_all(True)33 34 35 36 37 section = "\\Collection\\"38 colname = "2DFace"39 filepath = FACE_BLEND_FILE + section + colname40 directory = FACE_BLEND_FILE + section41 filename = colname42 #path = FACE_BLEND_FILE + "/Collection/"43 #object_name = "2DFace"44 #bpy.ops.wm.append(filename=object_name, directory=path)45 46 47 bpy.ops.wm.append(48 filepath=filepath, 49 filename=filename,50 directory=directory)51 52 53 toolbox.deselect_all(True)54 faceCollection = bpy.data.collections['2DFace']55 56 57 print ("DOOOOOF " + str(SELECTED_OBJECT_COLLECTION_NAME))58 59 60 active_collection = bpy.context.view_layer.active_layer_collection.collection61 targetCollection = SELECTED_OBJECT_COLLECTION62 print ("DOOOOOF " + str(targetCollection.name))63 64 #bpy.context.scene.collection.children.unlink(faceCollection)65 66 active_collection.children.unlink(faceCollection) 67 targetCollection.children.link(faceCollection)68 69 70 71 72 # targetCollection.children.link(faceCollection)73 # bpy.context.scene.collection.children.unlink(faceCollection)74 75 76 77 78 for obj in bpy.data.collections['2DFace'].all_objects:79 obj.select_set(True)80 81 82 83def get_selected_object():84 85 global SELECTED_OBJECT86 global SELECTED_OBJECT_LOCATION 87 global SELECTED_OBJECT_FOUND 88 global SELECTED_OBJECT_DIMENSION89 global SELECTED_OBJECT_COLLECTION90 global SELECTED_OBJECT_COLLECTION_NAME91 print ("OBJECT SELECTED")92 93 print (SELECTED_OBJECT.name)94 print (str(SELECTED_OBJECT.dimensions))95 bpy.context.scene.cursor.location = SELECTED_OBJECT.location96 SELECTED_OBJECT_FOUND = 197 SELECTED_OBJECT_LOCATION = SELECTED_OBJECT.matrix_world.translation 98 SELECTED_OBJECT_DIMENSION = SELECTED_OBJECT.dimensions99 print (str(SELECTED_OBJECT.users_collection[0].name))100 SELECTED_OBJECT_COLLECTION = SELECTED_OBJECT.users_collection[0]101 SELECTED_OBJECT_COLLECTION_NAME = SELECTED_OBJECT.users_collection[0].name102 103 104 105 106def move_2Dface():107 global SELECTED_OBJECT_DIMENSION108 global SELECTED_OBJECT_FOUND109 global SELECTED_OBJECT110 global SELECTED_OBJECT_LOCATION111 global SCALE_FACTOR112 global DISTANCE_PERCENTAGE113 if SELECTED_OBJECT_FOUND == 0:114 print("NO selected Object found")115 return116 117 118 toolbox.deselect_all(True)119 # for obj in bpy.data.collections['2Dface'].all_objects:120 # obj.select_set(True)121 print("SELECTED_OBJECT_LOCATION " + str(SELECTED_OBJECT_LOCATION)) 122 armobj = find_face_armature()123 if armobj == None:124 print("Face Armature not found")125 return126 127 #armobj = bpy.data.objects["Armature"]128 armobj.select_set(True)129 130 131 #bpy.ops.object.select_grouped(type='CHILDREN_RECURSIVE') myObj.select = True 132 133 # for obj in bpy.context.selected_objects:134 # print (obj.name) 135 136 dim_y = SELECTED_OBJECT_DIMENSION.y137 print ("Dimension selected Object Y:" + str(dim_y))138 armobj.location = SELECTED_OBJECT_LOCATION139 140 141 142 143 armobj.location.y = armobj.location.y - ((dim_y / 2) + (dim_y / 100 * DISTANCE_PERCENTAGE))144 # armobj.location.x = SELECTED_OBJECT_LOCATION.x145 146 147 148 149def do_resizinig():150 151 global SELECTED_OBJECT_DIMENSION152 global SELECTED_OBJECT_FOUND153 global SELECTED_OBJECT154 global SCALE_FACTOR155 if SELECTED_OBJECT_FOUND == 0:156 return157 158 159 print ("RESIZING")160 toolbox.deselect_all(True)161 armobj = find_face_armature()162 163 toolbox.select_object(armobj, True)164 165 166 167 168 169 170 171 172 173 174 bpy.ops.transform.resize(value=(SCALE_FACTOR, SCALE_FACTOR, SCALE_FACTOR), orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', mirror=True, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False)175 176 177 toolbox.deselect_all(True)178 179 180 181 182 183 184 185 186 187def do_shrinkwrapping():188 189 global SELECTED_OBJECT_DIMENSION190 global SELECTED_OBJECT_FOUND191 global SELECTED_OBJECT192 if SELECTED_OBJECT_FOUND == 0:193 return194 195 eye_L = get_object_from_face("eye_object.L")196 eye_R = get_object_from_face("eye_object.R")197 eye_L_outline = get_object_from_face("eye_object_outline.L")198 eye_R_outline = get_object_from_face("eye_object_outline.R")199 mouth = get_object_from_face("mouth_object")200 mouth_outline = get_object_from_face("mouth_object_outline")201 brow_L = get_object_from_face("brow_object.L")202 brow_R = get_object_from_face("brow_object.R")203 204 205 206 eye_L_outline.modifiers.new(name="Shrinkwrap",type='SHRINKWRAP')207 eye_L_outline.modifiers["Shrinkwrap"].wrap_method = 'PROJECT'208 eye_L_outline.modifiers["Shrinkwrap"].wrap_mode = 'OUTSIDE_SURFACE'209 eye_L_outline.modifiers["Shrinkwrap"].use_positive_direction = False210 eye_L_outline.modifiers["Shrinkwrap"].use_negative_direction = True211 eye_L_outline.modifiers["Shrinkwrap"].target = SELECTED_OBJECT212 eye_L_outline.modifiers["Shrinkwrap"].offset = 0.04213 deselect_all()214 eye_L_outline.select_set(True)215 bpy.context.view_layer.objects.active = eye_L_outline216 #bpy.ops.object.modifier_move_to_index(modifier="Shrinkwrap", index=0)217 bpy.context.view_layer.objects.active = None218 deselect_all() 219 220 eye_R_outline.modifiers.new(name="Shrinkwrap",type='SHRINKWRAP')221 eye_R_outline.modifiers["Shrinkwrap"].wrap_method = 'PROJECT'222 eye_R_outline.modifiers["Shrinkwrap"].wrap_mode = 'OUTSIDE_SURFACE'223 eye_R_outline.modifiers["Shrinkwrap"].use_positive_direction = False224 eye_R_outline.modifiers["Shrinkwrap"].use_negative_direction = True225 eye_R_outline.modifiers["Shrinkwrap"].target = SELECTED_OBJECT226 eye_R_outline.modifiers["Shrinkwrap"].offset = 0.04227 deselect_all()228 eye_R_outline.select_set(True)229 bpy.context.view_layer.objects.active = eye_R_outline230 #bpy.ops.object.modifier_move_to_index(modifier="Shrinkwrap", index=0)231 bpy.context.view_layer.objects.active = None232 deselect_all() 233 234 235 236 237 238 eye_L.modifiers.new(name="Shrinkwrap",type='SHRINKWRAP')239 eye_L.modifiers["Shrinkwrap"].wrap_method = 'PROJECT'240 eye_L.modifiers["Shrinkwrap"].wrap_mode = 'OUTSIDE_SURFACE'241 eye_L.modifiers["Shrinkwrap"].use_positive_direction = False242 eye_L.modifiers["Shrinkwrap"].use_negative_direction = True243 eye_L.modifiers["Shrinkwrap"].target = SELECTED_OBJECT244 eye_L.modifiers["Shrinkwrap"].offset = 0.06245 deselect_all()246 eye_L.select_set(True)247 bpy.context.view_layer.objects.active = eye_L248 #bpy.ops.object.modifier_move_to_index(modifier="Shrinkwrap", index=0)249 bpy.context.view_layer.objects.active = None250 deselect_all() 251 252 253 brow_L.modifiers.new(name="Shrinkwrap",type='SHRINKWRAP')254 brow_L.modifiers["Shrinkwrap"].wrap_method = 'PROJECT'255 brow_L.modifiers["Shrinkwrap"].wrap_mode = 'OUTSIDE_SURFACE'256 brow_L.modifiers["Shrinkwrap"].use_positive_direction = False257 brow_L.modifiers["Shrinkwrap"].use_negative_direction = True258 brow_L.modifiers["Shrinkwrap"].target = SELECTED_OBJECT259 brow_L.modifiers["Shrinkwrap"].offset = 0.05260 deselect_all()261 brow_L.select_set(True)262 bpy.context.view_layer.objects.active = brow_L263 #bpy.ops.object.modifier_move_to_index(modifier="Shrinkwrap", index=0)264 bpy.context.view_layer.objects.active = None265 deselect_all() 266 267 268 eye_R.modifiers.new(name="Shrinkwrap",type='SHRINKWRAP')269 eye_R.modifiers["Shrinkwrap"].wrap_method = 'PROJECT'270 eye_R.modifiers["Shrinkwrap"].wrap_mode = 'OUTSIDE_SURFACE'271 eye_R.modifiers["Shrinkwrap"].use_positive_direction = False272 eye_R.modifiers["Shrinkwrap"].use_negative_direction = True273 eye_R.modifiers["Shrinkwrap"].target = SELECTED_OBJECT274 eye_R.modifiers["Shrinkwrap"].offset = 0.06275 deselect_all()276 eye_R.select_set(True)277 bpy.context.view_layer.objects.active = eye_R278 #bpy.ops.object.modifier_move_to_index(modifier="Shrinkwrap", index=0)279 bpy.context.view_layer.objects.active = None280 deselect_all() 281 282 brow_R.modifiers.new(name="Shrinkwrap",type='SHRINKWRAP')283 brow_R.modifiers["Shrinkwrap"].wrap_method = 'PROJECT'284 brow_R.modifiers["Shrinkwrap"].wrap_mode = 'OUTSIDE_SURFACE'285 brow_R.modifiers["Shrinkwrap"].use_positive_direction = False286 brow_R.modifiers["Shrinkwrap"].use_negative_direction = True287 brow_R.modifiers["Shrinkwrap"].target = SELECTED_OBJECT288 brow_R.modifiers["Shrinkwrap"].offset = 0.05289 deselect_all()290 brow_R.select_set(True)291 bpy.context.view_layer.objects.active = brow_R292 #bpy.ops.object.modifier_move_to_index(modifier="Shrinkwrap", index=0)293 bpy.context.view_layer.objects.active = None294 deselect_all()295 296 mouth.modifiers.new(name="Shrinkwrap",type='SHRINKWRAP')297 mouth.modifiers["Shrinkwrap"].wrap_method = 'PROJECT'298 mouth.modifiers["Shrinkwrap"].wrap_mode = 'OUTSIDE_SURFACE'299 mouth.modifiers["Shrinkwrap"].use_positive_direction = False300 mouth.modifiers["Shrinkwrap"].use_negative_direction = True301 mouth.modifiers["Shrinkwrap"].target = SELECTED_OBJECT302 mouth.modifiers["Shrinkwrap"].offset = 0.05303 toolbox.deselect_all(True)304 mouth.select_set(True)305 bpy.context.view_layer.objects.active = mouth306 #bpy.ops.object.modifier_move_to_index(modifier="Shrinkwrap", index=0)307 bpy.context.view_layer.objects.active = None308 toolbox.deselect_all(True)309 mouth_outline.modifiers.new(name="Shrinkwrap",type='SHRINKWRAP')310 mouth_outline.modifiers["Shrinkwrap"].wrap_method = 'PROJECT'311 mouth_outline.modifiers["Shrinkwrap"].wrap_mode = 'OUTSIDE_SURFACE'312 mouth_outline.modifiers["Shrinkwrap"].use_positive_direction = False313 mouth_outline.modifiers["Shrinkwrap"].use_negative_direction = True314 mouth_outline.modifiers["Shrinkwrap"].target = SELECTED_OBJECT315 mouth_outline.modifiers["Shrinkwrap"].offset = 0.03316 toolbox.deselect_all(True)317 mouth_outline.select_set(True)318 bpy.context.view_layer.objects.active = mouth_outline319 #bpy.ops.object.modifier_move_to_index(modifier="Shrinkwrap", index=0)320 bpy.context.view_layer.objects.active = None321 toolbox.deselect_all(True)322def parent_to_headbone():323 global SELECTED_OBJECT_DIMENSION324 global SELECTED_OBJECT_FOUND325 global SELECTED_OBJECT326 327 head_armature = SELECTED_OBJECT.parent328 if (head_armature != None):329 print ("HEAD_ARMATURE FOUND!")330 else:331 print ("NO HEAD_ARMATURE_FOUND")332 return333 334 toolbox.deselect_all(True)335 face_armature = get_object_from_face("Armature")336 if (face_armature != None):337 print("Face Armature found....")338 else:339 print ("No face armature found")340 return341 342 face_armature.select_set(True)343 bpy.context.view_layer.objects.active = face_armature344 bpy.ops.object.posemode_toggle()345 master = face_armature.pose.bones.get("FACE_Masterbone")346 347 348 crc = master.constraints.new('CHILD_OF')349 # bpy.ops.pose.constraint_add(type='CHILD_OF')350 print (head_armature.name)351 crc.target = head_armature352 crc.subtarget = "HEADBONE"353 354 355 #crc.childof_clear_inverse(constraint="Child Of", owner='BONE')356 #crc.childof_set_inverse(constraint="Child Of", owner='BONE')357 bpy.ops.object.posemode_toggle()358 359 360 361def main():362 #clear_all()363 #import_head()364 get_selected_object() 365 import_face() 366 move_2Dface()367 do_resizinig()368 369 parent_to_headbone()370 371 do_shrinkwrapping()372 373 #finish()374 #deselect_all()375def find_face_armature():376 #print("Finding face armature.....")377 for obj in bpy.data.collections['2DFace'].all_objects:378 #print(obj.name)379 if obj.name.startswith('Armature'):380 #print ("Found obj.. returning " + obj.name)381 return obj382 return None 383 384def get_object_from_face(name):385 for obj in bpy.data.collections['2DFace'].all_objects:386 if obj.name.startswith(name):387 return obj388 return None 389 390def deselect_all():391 #bpy.context.view_layer.objects.active = None392 for obj in bpy.context.selected_objects:393 obj.select_set(False) 394 395 396def finish():397 global SELECTED_OBJECT398 col = bpy.data.collections.get("2DFace")399 if col:400 col.name = SELECTED_OBJECT.name + "_2DFace.ready" ...
openlock_floor_base.py
Source:openlock_floor_base.py
...44 t.ri(d=x / 2)45 t.fd(d=y / 2)46 # move turtle47 t.pu()48 t.deselect_all()49 t.set_position(v=start_loc)50 t.fd(d=outer_w)51 t.pd()52 # draw loop 253 t.add_vert()54 t.begin_path()55 t.ri(d=x / 2 - outer_w)56 t.fd(d=y / 2 - outer_w)57 t.select_path()58 # create bevel for corner supports59 bpy.ops.mesh.bevel(offset_type='WIDTH', offset=0.11811, offset_pct=0, affect='VERTICES')60 # move turtle61 t.pu()62 t.deselect_all()63 t.home()64 t.set_position(v=start_loc)65 t.fd(d=outer_w + slot_w)66 t.pd()67 # draw loop 368 t.add_vert()69 t.begin_path()70 t.ri(d=x / 2 - slot_w - outer_w)71 t.fd(d=y / 2 - slot_w - outer_w)72 t.select_path()73 # create bevel for corner supports74 bpy.ops.mesh.bevel(offset_type='WIDTH', offset=0.11811, offset_pct=0, affect='VERTICES')75 # bridge bevels76 leg = support_w / sqrt(2)77 t.pu()78 t.home()79 t.set_position(v=start_loc)80 t.fd(d=outer_w + slot_w)81 t.ri(d=x / 2 - outer_w - slot_w - leg)82 t.select_at_cursor()83 t.bk(d=slot_w)84 t.ri(d=slot_w)85 t.select_at_cursor()86 t.ri(d=leg)87 t.fd(d=leg)88 t.select_at_cursor()89 t.fd(d=slot_w)90 t.lf(d=slot_w)91 t.select_at_cursor()92 bpy.ops.mesh.edge_face_add()93 t.deselect_all()94 t.select_at_cursor()95 t.lf(d=leg)96 t.bk(d=leg)97 t.select_at_cursor()98 bpy.ops.mesh.delete(type='EDGE')99 t.bk(d=slot_w)100 t.ri(d=slot_w)101 t.select_at_cursor()102 t.fd(d=leg)103 t.ri(d=leg)104 t.select_at_cursor()105 bpy.ops.mesh.delete(type='EDGE')106 # check if either side is greater than 100.6mm (jut under 4").107 # if yes we add some extra support between outer and inner ring108 if x >= 3.99:109 x_supports = 1 + floor((x - 3.99) / 2)110 else:111 x_supports = 0112 if y >= 3.99:113 y_supports = 1 + floor((y - 3.99) / 2)114 else:115 y_supports = 0116 t.home()117 t.pu()118 t.set_position(v=start_loc)119 t.fd(d=outer_w)120 t.rt(d=90)121 if x_supports > 0:122 add_extra_supports(x_supports, 'x', support_w, slot_w, x, y, leg, outer_w, start_loc, side_length=x)123 t.home()124 t.ri(d=x / 2 - outer_w)125 t.rt(d=180)126 if y_supports > 0:127 add_extra_supports(128 y_supports,129 'y',130 support_w,131 -slot_w,132 x,133 y,134 leg,135 -outer_w,136 start_loc,137 side_length=y)138 # extrude inner sides up139 t.pd()140 t.select_all()141 t.up(d=support_h)142 t.select_all()143 t.merge()144 t.deselect_all()145 t.home()146 t.set_position(v=start_loc)147 # join outer edges148 t.pd()149 t.select_at_cursor()150 t.fd(d=outer_w)151 t.deselect_all()152 t.pu()153 t.fd(d=slot_w)154 t.pd()155 t.select_at_cursor()156 t.fd(d=y / 2 - slot_w - outer_w)157 t.ri(d=x / 2 - slot_w - outer_w)158 t.pu()159 t.deselect_all()160 t.ri(d=slot_w)161 t.pd()162 t.select_at_cursor()163 t.ri(d=outer_w)164 t.select_all()165 t.merge()166 # fill base167 t.pu()168 t.deselect_all()169 t.home()170 t.set_position(v=start_loc)171 t.select_by_location(172 lbound=turtle.location,173 ubound=(turtle.location[0] + x / 2,174 turtle.location[1] + y / 2,175 turtle.location[2]))176 bpy.ops.mesh.fill()177 t.select_all()178 bpy.ops.mesh.normals_make_consistent()179 # draw extra support roofs180 t.pu()181 t.deselect_all()182 t.home()183 t.set_position(v=start_loc)184 t.fd(d=outer_w)185 t.rt(d=90)186 t.up(d=support_h)187 if x_supports > 0:188 fill_extra_supports(x_supports, 'x', support_w, slot_w)189 t.home()190 t.ri(d=x / 2 - outer_w)191 t.rt(d=180)192 t.up(d=support_h)193 if y_supports > 0:194 fill_extra_supports(y_supports, 'y', support_w, -slot_w)195 # draw corner support roofs196 t.deselect_all()197 t.pu198 t.home()199 t.set_position(v=start_loc)200 t.fd(d=outer_w)201 t.up(d=support_h)202 t.ri(d=x / 2 - outer_w - leg)203 t.select_at_cursor()204 t.ri(d=leg)205 t.fd(d=leg)206 t.select_at_cursor()207 t.fd(d=slot_w)208 t.lf(d=slot_w)209 t.select_at_cursor()210 t.lf(d=leg)211 t.bk(d=leg)212 t.select_at_cursor()213 bpy.ops.mesh.edge_face_add()214 # bridge_slot215 t.deselect_all()216 t.home()217 t.set_position(v=start_loc)218 t.fd(d=outer_w)219 t.up(d=support_h)220 t.select_at_cursor()221 t.pd()222 t.fd(d=slot_w)223 t.deselect_all()224 t.pu()225 t.ri(d=x / 2 - outer_w - slot_w)226 t.fd(d=y / 2 - outer_w - slot_w)227 t.select_at_cursor()228 t.pd()229 t.ri(d=slot_w)230 t.pu()231 # draw duplicate top of supports and slot232 t.deselect_all()233 t.home()234 t.set_position(v=start_loc)235 t.fd(d=outer_w)236 t.up(d=support_h)237 t.select_by_location(238 lbound=turtle.location,239 ubound=(turtle.location[0] + x / 2 - outer_w,240 turtle.location[1] + y / 2 - outer_w,241 turtle.location[2]))242 bpy.ops.mesh.duplicate_move(243 TRANSFORM_OT_translate={244 "value": (0, 0, slot_h - support_h),245 "orient_type": 'CURSOR'})246 t.merge()247 bpy.ops.mesh.edge_face_add()248 # extrude down and clean249 t.pd()250 t.dn(d=slot_h - support_h)251 bpy.ops.mesh.delete(type='FACE')252 # clean non-manifolds at end253 t.pu()254 t.deselect_all()255 t.home()256 t.set_position(v=start_loc)257 t.fd(d=outer_w)258 t.up(d=support_h)259 t.select_at_cursor()260 t.fd(d=slot_w)261 t.select_at_cursor()262 bpy.ops.mesh.delete(type='EDGE')263 t.ri(d=x / 2 - outer_w)264 t.fd(d=y / 2 - outer_w - slot_w)265 t.select_at_cursor()266 t.lf(d=slot_w)267 t.select_at_cursor()268 bpy.ops.mesh.delete(type='EDGE')269 # draw outer wall270 t.pu()271 t.home()272 t.set_position(v=start_loc)273 t.up(d=support_h)274 t.select_at_cursor()275 t.ri(d=x / 2)276 t.select_at_cursor()277 t.fd(d=y / 2)278 t.select_at_cursor()279 t.pd()280 t.up(d=z - support_h)281 # draw top282 t.pu()283 t.deselect_all()284 t.home()285 t.up(d=z)286 t.pd()287 t.add_vert()288 t.begin_path()289 t.ri(d=x / 2)290 t.select_path()291 t.bk(d=y / 2)292 # clean up293 t.select_all()294 t.merge()295 bpy.ops.mesh.normals_make_consistent()296 t.deselect_all()297 t.home()298def add_extra_supports(299 num_supports,300 axis,301 support_w,302 slot_w,303 x,304 y,305 leg,306 outer_w,307 start_loc,308 side_length):309 turtle = bpy.context.scene.cursor310 t = bpy.ops.turtle311 t.select_at_cursor()312 bpy.ops.mesh.delete(type='VERT')313 t.lf(d=slot_w)314 t.select_at_cursor()315 bpy.ops.mesh.delete(type='VERT')316 t.pd()317 t.add_vert()318 t.fd(d=extra_sup_dist / 2)319 t.ri(d=slot_w)320 t.bk(d=extra_sup_dist / 2)321 t.deselect_all()322 t.pu()323 t.fd(d=(extra_sup_dist / 2) + support_w)324 for i in range(num_supports - 1):325 t.pd()326 t.add_vert()327 t.begin_path()328 t.fd(d=extra_sup_dist)329 t.lf(d=slot_w)330 t.bk(d=extra_sup_dist)331 t.stroke_path()332 t.pu()333 t.deselect_all()334 t.ri(d=slot_w)335 t.fd(d=extra_sup_dist + support_w)336 if axis == 'x':337 a = num_supports * extra_sup_dist338 b = extra_sup_dist / 2339 c = support_w * (num_supports + 1)340 d = outer_w + leg341 t.pd()342 t.add_vert()343 if num_supports > 1:344 t.fd(d=side_length / 2 - a + b - c - d + support_w)345 t.pu()346 t.deselect_all()347 t.bk(d=side_length / 2 - a + b - c - d + support_w)348 t.select_at_cursor()349 t.pd()350 t.lf(d=slot_w)351 t.fd(d=side_length / 2 - a + b - c - d - slot_w + support_w)352 else:353 t.fd(d=side_length / 2 - b - d - support_w)354 t.pu()355 t.deselect_all()356 t.bk(d=side_length / 2 - b - d - support_w)357 t.select_at_cursor()358 t.pd()359 t.lf(d=slot_w)360 t.fd(d=side_length / 2 - b - d - support_w - slot_w)361 t.select_all()362 t.merge()363 t.deselect_all()364 t.pu()365 else:366 a = extra_sup_dist * (num_supports - 1) - support_w367 b = extra_sup_dist / 2368 c = support_w * (num_supports + 1)369 d = outer_w - leg370 t.pd()371 t.add_vert()372 if num_supports > 1:373 t.fd(d=side_length / 2 - a - b - c + d)374 t.pu()375 t.deselect_all()376 t.bk(d=side_length / 2 - a - b - c + d)377 t.select_at_cursor()378 t.pd()379 t.lf(d=slot_w)380 t.fd(d=side_length / 2 - a - b - c + d + slot_w)381 else:382 t.fd(d=side_length / 2 - b - support_w + d)383 t.pu()384 t.deselect_all()385 t.bk(d=side_length / 2 - b - support_w + d)386 t.select_at_cursor()387 t.pd()388 t.lf(d=slot_w)389 t.fd(d=side_length / 2 - b - support_w + d + slot_w)390 t.select_all()391 t.merge()392 t.deselect_all()393 t.pu()394def fill_extra_supports(num_supports, axis, support_w, slot_w):395 turtle = bpy.context.scene.cursor396 t = bpy.ops.turtle397 t.fd(d=extra_sup_dist / 2)398 t.select_at_cursor()399 t.lf(d=slot_w)400 t.select_at_cursor()401 t.fd(d=support_w)402 t.select_at_cursor()403 t.ri(d=slot_w)404 t.select_at_cursor()405 bpy.ops.mesh.edge_face_add()406 t.deselect_all()407 for i in range(num_supports - 1):408 t.fd(d=extra_sup_dist)409 t.select_at_cursor()410 t.lf(d=slot_w)411 t.select_at_cursor()412 t.fd(d=support_w)413 t.select_at_cursor()414 t.ri(d=slot_w)415 t.select_at_cursor()416 bpy.ops.mesh.edge_face_add()...
utils.py
Source:utils.py
...52 '''Applies smooth shading to the provided object.53 '''54 if bpy.context.mode != 'OBJECT':55 bpy.ops.object.mode_set(mode = 'OBJECT')56 deselect_all()57 set_active(item)58 item.select_set(True)59 if item.type == 'MESH':60 bpy.ops.object.shade_smooth()61 deselect_all()62def apply_edge_split(item, angle = math.pi / 8):63 '''Applies the edge-split modifier to the provided object.64 '''65 if bpy.context.mode != 'OBJECT':66 bpy.ops.object.mode_set(mode = 'OBJECT')67 deselect_all()68 set_active(item)69 item.select_set(True)70 try:71 bpy.ops.object.modifier_add(type = 'EDGE_SPLIT')72 bpy.context.object.modifiers["EdgeSplit"].split_angle = angle73 except:74 pass75 deselect_all()76def find_object_in_collection(coll_name, item_name):77 '''Retrieves an object by name from a collection.78 '''79 collection = bpy.data.collections.get(coll_name)80 for item in collection.objects:81 if item.name == item_name:82 return item83 return None84def move_selected_to_collection(name):85 '''Moves selected objects to collection.86 '''87 collection = bpy.data.collections.get(name)88 selected = bpy.context.selected_objects89 for select in selected:90 old = find_collection(select)91 collection.objects.link(select)92 old.objects.unlink(select)93def find_collection(item):94 '''Find the current collection of an object.95 '''96 collections = item.users_collection97 if len(collections) > 0:98 return collections[0]99 return bpy.context.scene.collection100def get_collection(name):101 '''Get a collection.102 '''103 collection = bpy.data.collections.get(name)104 return collection105def remove_collection(name, remove_objects = True):106 '''Removes a collection and all its contents.107 '''108 collection = bpy.data.collections.get(name)109 if collection:110 if remove_objects:111 obs = [o for o in collection.objects if o.users == 1]112 while obs:113 bpy.data.objects.remove(obs.pop())114 bpy.data.collections.remove(collection)115def make_collection(name):116 '''Creates a new collection, deleting collections with the same name.117 '''118 remove_collection(name)119 collection = bpy.data.collections.new(name)120 bpy.context.scene.collection.children.link(collection)121 return collection122def deselect_all():123 bpy.ops.object.select_all(action = 'DESELECT')124# def parent_object(parent, child):125# deselect_all()126# parent.select_set(True)127# child.select_set(True)128# set_active(child)129# bpy.ops.object.parent_set(type = 'OBJECT')130# deselect_all()131def create_object_parent(parent, child):132 if bpy.context.mode != 'OBJECT':133 bpy.ops.object.mode_set(mode = 'OBJECT')134 deselect_all()135 set_active(child)136 child.select_set(True)137 constraint = get_object_parent(parent, child)138 if not constraint:139 try:140 bpy.ops.object.constraint_add(type = 'CHILD_OF')141 constraint = bpy.context.object.constraints["Child Of"]142 constraint.name = "{}_to_{}".format(parent.name, child.name)143 constraint.target = parent144 constraint.influence = 0.145 except:146 pass147 deselect_all()148 return constraint149def get_object_parent(parent, child):150 if bpy.context.mode != 'OBJECT':151 bpy.ops.object.mode_set(mode = 'OBJECT')152 deselect_all()153 set_active(child)154 child.select_set(True)155 try:156 name = "{}_to_{}".format(parent.name, child.name)157 constraint = bpy.context.object.constraints[name]158 return constraint159 except:160 pass161 return None162def parent_object(parent, child, frame):163 # Set current frame so inverse matrix can properly be computed164 bpy.context.scene.frame_set(frame)165 constraint = create_object_parent(parent, child)166 # Set object as parented in frame167 constraint.influence = 1168 constraint.keyframe_insert(data_path = "influence", frame = frame)169 # Compute inverse transform for proper parenting170 deselect_all()171 set_active(child)172 child.select_set(True)173 context_py = bpy.context.copy()174 context_py["constraint"] = constraint175 bpy.ops.constraint.childof_set_inverse(context_py,176 constraint = constraint.name,177 owner = "OBJECT")178 deselect_all()179 # Set object as unparented in prior frame180 constraint.influence = 0181 constraint.keyframe_insert(data_path = "influence", frame = frame - 1)182def unparent_object(parent, child, frame):183 bpy.context.scene.frame_set(frame - 1)184 constraint = get_object_parent(parent, child)185 if not constraint:186 return187 # Set object as parented in prior frame188 constraint.influence = 1189 constraint.keyframe_insert(data_path = "influence", frame = frame - 1)190 # Set object as unparented in prior frame191 constraint.influence = 0192 constraint.keyframe_insert(data_path = "influence", frame = frame)193def set_active(item):194 bpy.context.view_layer.objects.active = item195def add_material(item, material):196 '''Adds a material to an object.197 '''198 if item.data.materials:199 # assign to 1st material slot200 item.data.materials[0] = material201 else:202 # no slots203 item.data.materials.append(material)204def set_color(obj, element):205 if 'color' in element:206 # TODO: figure out a better way to make new materials?207 mat = bpy.data.materials.new(name = str(random.randint(1, 100000)))208 if len(element['color']) > 3:209 mat.diffuse_color = element['color']210 else:211 mat.diffuse_color = element['color'] + (1., )212 add_material(obj, mat)213def pose_to_quat(pose):214 '''Takes a pose dict and extracts the orientation quaternion. ROS quaternions or XYZW, but Blender's are WXYZ, so215 reorder them.216 '''217 if 'x' in pose['orientation']:218 q = pose['orientation']219 return mathutils.Quaternion([q['w'], q['x'], q['y'], q['z']])220 if isinstance(pose['orientation'][0], str):221 # Means there's a NAN somewhere.222 return mathutils.Quaternion((1.0, 0.0, 0.0, 0.0))223 else:224 return mathutils.Quaternion(pose['orientation'][3:] +225 pose['orientation'][:3])226def pose_to_vec(pose):227 '''Takes a pose dict and extracts the position vector.228 '''229 if 'x' in pose['position']:230 v = pose['position']231 return mathutils.Vector([v['x'], v['y'], v['z']])232 return mathutils.Vector(pose['position'])233def pose_add(obj, pose1, pose2):234 '''Adds the second pose after the first. For something like link origins, the235 joint pose should be passed first, then the link origin236 '''237 # Get the objects for the pose vectors and quaternions.238 p1_vec = pose_to_vec(pose1)239 q1 = pose_to_quat(pose1)240 p2_vec = pose_to_vec(pose2)241 q2 = pose_to_quat(pose2)242 # Rotate pose2's vec by pose1's quaternion.243 p2_vec.rotate(q1)244 obj.location = p1_vec + p2_vec245 # Apply pose2's quaternion to the existing rotation.246 q2.rotate(q1)247 obj.rotation_mode = 'QUATERNION'248 obj.rotation_quaternion = q2249def set_pose(obj, pose):250 '''Sets the pose of a blender object by passing in a pose dict.251 '''252 obj.location = pose_to_vec(pose)253 obj.rotation_mode = 'QUATERNION'254 obj.rotation_quaternion = pose_to_quat(pose)255def read_YAML_data(file_name):256 '''Returns the yaml data structure of the data stored.257 '''258 full_name = resolve_path(file_name)259 if not full_name:260 logging.warn('Cannot open {}'.format(file_name))261 return None262 with open(full_name) as input_file:263 return yaml.load(input_file.read())264def remove_doubles(item, threshold = 0.0001):265 if bpy.context.mode != 'OBJECT':266 bpy.ops.object.mode_set(mode = 'OBJECT')267 deselect_all()268 set_active(item)269 item.select_set(True)270 if item.type == 'MESH':271 bpy.ops.object.mode_set(mode = 'EDIT')272 bpy.ops.mesh.select_all(action = 'SELECT')273 bpy.ops.mesh.remove_doubles(threshold = threshold)274 bpy.ops.object.mode_set(mode = 'OBJECT')275 deselect_all()276def remove_inner_faces(item):277 if bpy.context.mode != 'OBJECT':278 bpy.ops.object.mode_set(mode = 'OBJECT')279 deselect_all()280 set_active(item)281 item.select_set(True)282 if item.type == 'MESH':283 bpy.ops.object.mode_set(mode = 'EDIT')284 bpy.ops.mesh.select_all(action = 'SELECT')285 bpy.ops.mesh.select_mode(type = 'FACE')286 bpy.ops.mesh.select_interior_faces()287 bpy.ops.mesh.delete(type = 'FACE')288 bpy.ops.object.mode_set(mode = 'OBJECT')289 deselect_all()290def clear_alpha(obj):291 for mat in obj.data.materials:292 if not mat:293 continue294 for node in mat.node_tree.nodes:295 if 'Alpha' in node.inputs:296 node.inputs['Alpha'].default_value = 1.297def get_tf_origin_xml(xml):298 xyz = [0, 0, 0]299 rpy = [0, 0, 0]300 if xml.origin:301 rpy = xml.origin.rpy302 xyz = xml.origin.xyz303 rot = mathutils.Euler(rpy, 'XYZ').to_matrix()...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!