我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用bpy.data()。
def move_verts(self,obj,ratio_x,ratio_y): bpy.ops.object.mode_set(mode="EDIT") bpy.ops.mesh.reveal() bpy.ops.object.mode_set(mode="OBJECT") data = obj.data.vertices if obj.data.shape_keys != None: data = obj.data.shape_keys.key_blocks[0].data for vert in data: co_x = vert.co[0] * ratio_x co_y = vert.co[2] * ratio_y vert.co = Vector((co_x,0,co_y)) obj.coa_sprite_dimension = Vector((get_local_dimension(obj)[0],0,get_local_dimension(obj)[1])) obj.coa_tiles_x = self.tiles_x obj.coa_tiles_y = self.tiles_y
def layout_id_prop(layout, data, prop): prop_obj = data.bl_rna.properties[prop] prop_name = prop_obj.name value_key = _create_value_key(prop_name) ref_id = data.get(value_key, None) field_name = json.loads(prop_obj.description)["field_name"] row = layout.row(align=True) row.prop_search(data, prop, bpy.context.scene, field_name) if field_name == "objects": op_props = row.operator("view3d.object_picker_operator", emboss=True, text="", icon="EYEDROPPER") op_props.to_populate_data = repr(data) op_props.to_populate_field = prop op_props = row.operator(FindSelected.bl_idname, emboss=True, icon="VIEWZOOM") op_props.to_populate_data = repr(data) op_props.to_populate_field = prop
def create_getter(data_field, value_key): def fn(self): data = getattr(bpy.context.scene, data_field) ob_id = self.get(value_key, None) id_to_hash = ID_TO_HASH[data_field] hash_to_name = HASH_TO_NAME[data_field] ob_hash = id_to_hash.get(ob_id, None) ob_name = hash_to_name.get(ob_hash, None) exists = ob_name is not None and ob_name in data if not exists: for name, ob in data.items(): if ob_hash == hash(ob): hash_to_name[ob_hash] = name ob_name = name break if ob_name is None: ob_name = "" return ob_name return fn
def load_text(self, context=None): if self.text_file in bpy.data.texts: if self.text_file == 'Text': self.text_file = '' return text = text_remap(self.text_file) try: mod = importlib.import_module("svrx.nodes.script.{}".format(text)) importlib.reload(mod) except Exception as err: error.show(self, err, script=True) else: self.adjust_sockets() self.color = READY_COLOR self.use_custom_color = True else: pass # fail
def rna_backup_gen(data, include_props=None, exclude_props=None, root=()): # only writable properties... for p in data.bl_rna.properties: pid = p.identifier if pid in {'rna_type', }: continue path = root + (pid,) if include_props is not None and path not in include_props: continue if exclude_props is not None and path in exclude_props: continue val = getattr(data, pid) if val is not None and p.type == 'POINTER': # recurse! yield from rna_backup_gen(val, include_props, exclude_props, root=path) elif data.is_property_readonly(pid): continue else: yield path, val
def do_clear_previews(do_objects, do_groups, do_scenes, do_data_intern): if do_data_intern: bpy.ops.wm.previews_clear(id_type=INTERN_PREVIEW_TYPES) if do_objects: for ob in ids_nolib(bpy.data.objects): ob.preview.image_size = (0, 0) if do_groups: for grp in ids_nolib(bpy.data.groups): grp.preview.image_size = (0, 0) if do_scenes: for scene in ids_nolib(bpy.data.scenes): scene.preview.image_size = (0, 0) print("Saving %s..." % bpy.data.filepath) bpy.ops.wm.save_mainfile()
def users_id(self): """ID data blocks which use this library""" import bpy # See: readblenentry.c, IDTYPE_FLAGS_ISLINKABLE, # we could make this an attribute in rna. attr_links = ("actions", "armatures", "brushes", "cameras", "curves", "grease_pencil", "groups", "images", "lamps", "lattices", "materials", "metaballs", "meshes", "node_groups", "objects", "scenes", "sounds", "speakers", "textures", "texts", "fonts", "worlds") return tuple(id_block for attr in attr_links for id_block in getattr(bpy.data, attr) if id_block.library == self)
def makePolyLine(objName, curveName, cList): #objName and curveName are strings cList is a list of vectors curveData = bpy.data.curves.new(name=curveName, type='CURVE') curveData.dimensions = '3D' # objectData = bpy.data.objects.new(objName, curveData) # objectData.location = (0,0,0) #object origin # bpy.context.scene.objects.link(objectData) polyline = curveData.splines.new('BEZIER') polyline.bezier_points.add(len(cList)-1) for num in range(len(cList)): x, y, z = cList[num] polyline.bezier_points[num].co = (x, y, z) polyline.bezier_points[num].handle_left_type = polyline.bezier_points[num].handle_right_type = "AUTO" # return objectData return curveData
def avail(self,context): datablock_type = bpy.context.scene.amth_datablock_types if datablock_type == 'IMAGE_DATA': where = [] for im in bpy.data.images: if im.name not in {'Render Result', 'Viewer Node'}: where.append(im) elif datablock_type == 'MATERIAL': where = bpy.data.materials elif datablock_type == 'GROUP_VCOL': where = [] for ob in bpy.data.objects: if ob.type == 'MESH': for v in ob.data.vertex_colors: if v and v not in where: where.append(v) where = list(set(where)) items = [(str(i),x.name,x.name, datablock_type, i) for i,x in enumerate(where)] items = sorted(list(set(items))) return items
def execute(self,context): datablock_type = bpy.context.scene.amth_datablock_types if datablock_type == 'IMAGE_DATA': where = [] for im in bpy.data.images: if im.name not in {'Render Result', 'Viewer Node'}: where.append(im) elif datablock_type == 'MATERIAL': where = bpy.data.materials elif datablock_type == 'GROUP_VCOL': where = [] for ob in bpy.data.objects: if ob.type == 'MESH': for v in ob.data.vertex_colors: if v and v not in where: where.append(v) where = list(set(where)) bpy.context.scene.amth_list_users_for_x_name = where[int(self.list_type_select)].name return {'FINISHED'}
def uptext(text): source = text.source_text if source in bpy.data.texts: r = bpy.data.texts[source].as_string() else: r = source base = len(r) prog = text.scrambler_progress / 100.0 c = int(base * prog) clean = r[:base - c] scrambled = "" for i in range(c): scrambled += random.choice(text.characters) text.body = clean + scrambled # Typing Text #
def animate_text(scene): objects = scene.objects for obj in objects: if obj.type == "FONT" and "runAnimation" in obj and obj.runAnimation: endFrame = obj.startFrame + (len(obj.defaultTextBody) * obj.typeSpeed) if obj.manualEndFrame: endFrame = obj.endFrame if scene.frame_current < obj.startFrame: obj.data.body = "" elif scene.frame_current >= obj.startFrame and scene.frame_current <= endFrame: frameStringLength = (scene.frame_current - obj.startFrame) / obj.typeSpeed obj.data.body = obj.defaultTextBody[0:int(frameStringLength)] elif scene.frame_current > endFrame: obj.data.body = obj.defaultTextBody # Typewriter #
def draw(self, context): layout = self.layout layout.operator("mocap.samples", text='Samples to Beziers') layout.operator('mocap.pathediting', text="Follow Path") activeIsArmature = isinstance(context.active_object.data, bpy.types.Armature) if activeIsArmature: enduser_arm = context.active_object.data selectBox = layout.box() selectRetargets = selectBox.row() selectRetargets.label("Retargeted Animations:") selectRetargets.prop_search(enduser_arm, "active_mocap", enduser_arm, "mocapNLATracks") stitchBox = layout.box() stitchBox.label("Animation Stitching") settings = enduser_arm.stitch_settings stitchBox.prop_search(settings, "first_action", enduser_arm, "mocapNLATracks") stitchBox.prop_search(settings, "second_action", enduser_arm, "mocapNLATracks") stitchSettings = stitchBox.row() stitchSettings.prop(settings, "blend_frame") stitchSettings.prop(settings, "blend_amount") stitchSettings.prop(settings, "second_offset") stitchBox.prop_search(settings, "stick_bone", context.active_object.pose, "bones") stitchBox.operator('mocap.animstitchguess', text="Guess Settings") stitchBox.operator('mocap.animstitch', text="Stitch Animations")
def execute(self, context): scene = context.scene s_frame = scene.frame_start e_frame = scene.frame_end enduser_obj = context.active_object performer_obj = [obj for obj in context.selected_objects if obj != enduser_obj] if enduser_obj is None or len(performer_obj) != 1: print("Need active and selected armatures") else: performer_obj = performer_obj[0] s_frame, e_frame = performer_obj.animation_data.action.frame_range s_frame = int(s_frame) e_frame = int(e_frame) if retarget.isRigAdvanced(enduser_obj) and not enduser_obj.data.advancedRetarget: print("Recommended to use Advanced Retargeting method") enduser_obj.data.advancedRetarget = True else: retarget.totalRetarget(performer_obj, enduser_obj, scene, s_frame, e_frame) return {'FINISHED'}
def side(self, nombre, offset): bpy.ops.object.mode_set(mode="EDIT", toggle=0) OBJECT = bpy.context.active_object ODATA = bmesh.from_edit_mesh(OBJECT.data) bpy.context.tool_settings.mesh_select_mode = (True, False, False) for VERTICE in ODATA.verts[:]: VERTICE.select = False if nombre is False: for VERTICES in ODATA.verts[:]: if VERTICES.co[0] < (offset): VERTICES.select = 1 else: for VERTICES in ODATA.verts[:]: if VERTICES.co[0] > (offset): VERTICES.select = 1 ODATA.select_flush(False) bpy.ops.object.mode_set(mode="EDIT", toggle=0)
def execute(self, context): with open("%s_%s_SYM_TEMPLATE.xml" % (os.path.join(os.path.dirname(bpy.data.filepath), bpy.context.scene.name), bpy.context.object.name)) as file: ob = bpy.context.object actgr = ob.vertex_groups.active actind = ob.vertex_groups.active_index ls = eval(file.read()) wdict = {left: actgr.weight(right) for left, right in ls.items() for group in ob.data.vertices[right].groups if group.group == actind} actgr.remove( [vert.index for vert in ob.data.vertices if vert.co[0] <= 0]) for ind, weight in wdict.items(): actgr.add([ind], weight, 'REPLACE') bpy.context.object.data.update() return {'FINISHED'} # ------------------------------------ RESYM MESH-------------------------
def SelDoubles(self, context): bm = bmesh.from_edit_mesh(bpy.context.object.data) for v in bm.verts: v.select = 0 dictloc = {} rd = lambda x: (round(x[0],4),round(x[1],4),round(x[2],4)) for vert in bm.verts: dictloc.setdefault(rd(vert.co),[]).append(vert.index) for loc, ind in dictloc.items(): if len(ind) > 1: for v in ind: bm.verts[v].select = 1 bpy.context.scene.objects.active = bpy.context.scene.objects.active
def draw(self, context): layout = self.layout adv_obj = context.scene.advanced_objects layout.prop(adv_obj, "arrange_c_use_selected") if not adv_obj.arrange_c_use_selected: layout.prop(adv_obj, "arrange_c_select_type", expand=True) if adv_obj.arrange_c_select_type == 'O': layout.column(align=True).prop_search( adv_obj, "arrange_c_obj_arranjar", bpy.data, "objects" ) elif adv_obj.arrange_c_select_type == 'G': layout.column(align=True).prop_search( adv_obj, "arrange_c_obj_arranjar", bpy.data, "groups" ) if context.object.type == 'CURVE': layout.operator("object.arranjar_numa_curva", text="Arrange Objects")
def check_texture(img, mat): # finds a texture from an image # makes a texture if needed # adds it to the material if it isn't there already tex = bpy.data.textures.get(img.name) if tex is None: tex = bpy.data.textures.new(name=img.name, type='IMAGE') tex.image = img # see if the material already uses this tex # add it if needed found = False for m in mat.texture_slots: if m and m.texture == tex: found = True break if not found and mat: mtex = mat.texture_slots.add() mtex.texture = tex mtex.texture_coords = 'UV' mtex.use_map_color_diffuse = True
def replace_name(self): # use the chosen material as a base one, check if there is a name self.check_no_name = (False if self.mat_keep in {""} else True) if self.check_no_name is True: for mat in bpy.data.materials: name = mat.name if name == self.mat_keep: try: base, suffix = name.rsplit('.', 1) # trigger the exception num = int(suffix, 10) self.mat_keep = base mat.name = self.mat_keep return except ValueError: if name not in self.mat_error: self.mat_error.append(name) return return
def fixup_slot(self, slot): if not slot.material: return base, suffix = self.split_name(slot.material) if suffix is None: return try: base_mat = bpy.data.materials[base] except KeyError: print("\n[Materials Utils Specials]\nLink to base names\nError:" "Base material %r not found\n" % base) return slot.material = base_mat
def draw(self, context): layout = self.layout layout.operator_context = 'INVOKE_REGION_WIN' ob = context.object if (not c_data_has_materials()): layout.label(text="*No Materials in the data*", icon="INFO") elif (not ob): layout.label(text="*No Objects to select*", icon="INFO") else: if ob.mode == 'OBJECT': # show all used materials in entire blend file for material_name, material in bpy.data.materials.items(): if (material.users > 0): layout.operator("view3d.select_material_by_name", text=material_name, icon='MATERIAL_DATA', ).matname = material_name elif ob.mode == 'EDIT': # show only the materials on this object mats = ob.material_slots.keys() for m in mats: layout.operator("view3d.select_material_by_name", text=m, icon='MATERIAL_DATA').matname = m
def check_mat_name_unique(name_id="Material_new"): # check if the new name pattern is in materials' data name_list = [] suffix = 1 try: if c_data_has_materials(): name_list = [mat.name for mat in bpy.data.materials if name_id in mat.name] new_name = "{}_{}".format(name_id, len(name_list) + suffix) if new_name in name_list: # KISS failed - numbering is not sequential # try harvesting numbers in material names, find the rightmost ones test_num = [] from re import findall for words in name_list: test_num.append(findall("\d+", words)) suffix += max([int(l[-1]) for l in test_num]) new_name = "{}_{}".format(name_id, suffix) return new_name except Exception as e: print("\n[Materials Utils Specials]\nfunction: check_mat_name_unique\nError: %s \n" % e) pass return name_id
def execute(self, context): value = self.value data_path = self.data_path # match the pointer type from the target property to bpy.data.* # so we lookup the correct list. data_path_base, data_path_prop = data_path.rsplit(".", 1) data_prop_rna = eval("context.%s" % data_path_base).rna_type.properties[data_path_prop] data_prop_rna_type = data_prop_rna.fixed_type id_iter = None for prop in bpy.data.rna_type.properties: if prop.rna_type.identifier == "CollectionProperty": if prop.fixed_type == data_prop_rna_type: id_iter = prop.identifier break if id_iter: value_id = getattr(bpy.data, id_iter).get(value) exec("context.%s = value_id" % data_path) return operator_path_undo_return(context, data_path)
def _values_store(self, context): data_path_iter = self.data_path_iter data_path_item = self.data_path_item self._values = values = {} for item in getattr(context, data_path_iter): try: value_orig = eval("item." + data_path_item) except: continue # check this can be set, maybe this is library data. try: exec("item.%s = %s" % (data_path_item, value_orig)) except: continue values[item] = value_orig
def execute(self, context): op_strings = [] tot = 0 for op_module_name in dir(bpy.ops): op_module = getattr(bpy.ops, op_module_name) for op_submodule_name in dir(op_module): op = getattr(op_module, op_submodule_name) text = repr(op) if text.split("\n")[-1].startswith("bpy.ops."): op_strings.append(text) tot += 1 op_strings.append('') textblock = bpy.data.texts.new("OperatorList.txt") textblock.write('# %d Operators\n\n' % tot) textblock.write('\n'.join(op_strings)) self.report({'INFO'}, "See OperatorList.txt textblock") return {'FINISHED'} # ----------------------------------------------------------------------------- # Add-on Operators
def prop_search(self, obj, layout, *args, **kwargs): """ Resolves and renders a :meth:`bpy.types.UIlayout.prop_search` connected to a registered property on a GUI :class:`bpy.types.UILayout` object given a reference to the (correct) object type. In the example in :class:`PropertyGroupWrapper`: .. code-block:: python def draw(context, layout): global_properties.gui_properties.selected_mesh.prop_search(context.scene, layout, bpy.data,'objects', icon='VIEWZOOM', text='') Additional positional arguments and keyword arguments are passed to :meth:`bpy.types.UIlayout.prop` :param obj: A Blender object with additional properties defined by the wrapper classes. :param layout: A :class:`bpy.types.UIlayout` reference. """ obj = getattr(obj,'RobotDesigner') for i in self.reference[:-1]: obj = getattr(obj,i) layout.prop_search(obj, self.reference[-1], *args, **kwargs)
def draw(self, context): mesh_type = global_properties.mesh_type.get(context.scene) hide_bone = global_properties.display_mesh_selection.get(context.scene) layout = self.layout current_model = context.active_object segment_names = [bone.name for bone in current_model.data.bones] # for bone in sorted(segment_names, key=str.lower): # x = muscles.SelectSegmentMuscle # x.segment_name = bone # layout.operator(x.bl_idname, text=bone).segment_name = bone for root in [bone.name for bone in current_model.data.bones if bone.parent is None]: layout.operator(muscles.SelectSegmentMuscle.bl_idname, text=root).segment_name = root def recursion(children, level=0): for bone in sorted([bone.name for bone in children], key=str.lower): text = ' ' * level + '\__ ' + bone layout.operator(muscles.SelectSegmentMuscle.bl_idname, text=text).segment_name = bone recursion(current_model.data.bones[bone].children, level + 1) recursion(current_model.data.bones[root].children)
def putMenu(cls,layout, context, text=None, **kwargs): hide_obj = cls.show_connected.get(context.scene) # Get selected meshes selected = [i for i in bpy.context.selected_objects if i.type == cls.blender_type] text = cls.text if len(selected) == 1: object = selected[0] if object.parent_bone and not hide_obj == 'disconnected': text = object.name + " --> " + object.parent_bone elif not object.parent_bone and not hide_obj == 'connected': text = object.name layout.menu(cls.bl_idname, text=text) row = layout.row(align=True) cls.show_connected.prop(context.scene, row, expand=True, icon_only=True) row.separator() cls.quick_search.prop_search(bpy.context.scene, row, bpy.data,'objects', icon='VIEWZOOM', text='') #row.prop_search(bpy.context.scene.RobotEditor, cls.quick_search, bpy.data, 'objects', # icon='VIEWZOOM', text='')
def draw(self, context): current_model = context.active_object layout = self.layout for root in [bone.name for bone in current_model.data.bones if bone.parent is None]: layout.operator(segments.SelectSegment.bl_idname, text=root).segment_name = root def recursion(children, level=0): for bone in sorted([bone.name for bone in children], key=str.lower): text = ' ' * level + '\__ ' + bone layout.operator(segments.SelectSegment.bl_idname, text=text).segment_name = bone recursion(current_model.data.bones[bone].children, level + 1) recursion(current_model.data.bones[root].children)
def execute(self, context): from .model import SelectModel from .rigid_bodies import SelectGeometry, AssignGeometry from .segments import SelectSegment C = bpy.context D = bpy.data model_name = C.active_object.name segment_names = [i.name for i in C.active_object.data.bones if i.parent] for segment in segment_names: SelectModel.run(model_name=model_name) SelectSegment.run(segment_name=segment) GenerateMeshFromSegment.run() return {'FINISHED'} # operator to select mesh
def drawCallback(self, context, path): with cm_draw.bglWrapper: obj = bpy.data.objects[path.objectName] M = obj.matrix_world up = Vector((0.0, 0.0, 1.0)) for edge in obj.data.edges: a = M * obj.data.vertices[edge.vertices[0]].co b = M * obj.data.vertices[edge.vertices[1]].co if str(edge.index) in path.revDirec: a, b = b, a close = (2 * a + b) / 3 far = (a + 2 * b) / 3 mid = (a + b) / 2 edgeVec = a - b perp = edgeVec.cross(up).normalized() * (edgeVec.length / 12) cm_draw.drawLine3D((0, 1, 0, 0.7), close + perp, far) cm_draw.drawLine3D((0, 1, 0, 0.7), close - perp, far)
def format_text(self): global TW out = [] if self.text: lines = self.text.splitlines() elif self.text_file: text_file = bpy.data.texts.get(self.text_file) if text_file: lines = get_lines(text_file) else: return [] else: return [] width = self.width TW.width = int(width) // TEXT_WIDTH for t in lines: out.extend(TW.wrap(t)) out.append("") return out
def build(self, buildRequest): t = time.time() obj = bpy.context.scene.objects[self.settings["inputObject"]] if buildRequest.deferGeo: cp = bpy.data.objects.new("Empty", None) cp.matrix_world = obj.matrix_world cp["cm_deferObj"] = obj.name cp["cm_materials"] = buildRequest.materials else: cp = obj.copy() for m in cp.material_slots: if m.name in buildRequest.materials: replacement = buildRequest.materials[m.name] m.material = bpy.data.materials[replacement] buildRequest.group.objects.link(cp) bpy.context.scene.objects.link(cp) cm_timings.placement["GeoTemplateOBJECT"] += time.time() - t cm_timings.placementNum["GeoTemplateOBJECT"] += 1 return GeoReturn(cp)
def build(self, buildRequest): t = time.time() ob = bpy.context.scene.objects[self.settings["PointObject"]] pos = buildRequest.pos if self.settings["PointType"] == "OBJECT": point = ob.location else: # self.settings["PointObject"] == "MESH": if self.kdtree is None: mesh = ob.data self.kdtree = KDTree(len(mesh.vertices)) for i, v in enumerate(mesh.vertices): self.kdtree.insert(v.co, i) self.kdtree.balance() co, ind, dist = self.kdtree.find(ob.matrix_world.inverted() * pos) point = ob.matrix_world * co direc = point - pos rotQuat = direc.to_track_quat('Y', 'Z') buildRequest.rot = rotQuat.to_euler() cm_timings.placement["TemplatePOINTTOWARDS"] += time.time() - t cm_timings.placementNum["TemplatePOINTTOWARDS"] += 1 self.inputs["Template"].build(buildRequest)
def check(self): if "Template" not in self.inputs: return False if not isinstance(self.inputs["Template"], Template): return False if isinstance(self.inputs["Template"], GeoTemplate): return False if self.settings["targetType"] == "object": if self.settings["targetGroups"] not in bpy.data.groups: return False elif self.settings["targetType"] == "vertex": if self.settings["targetObject"] not in bpy.context.scene.objects: return False if bpy.context.scene.objects[self.settings["targetObject"]].type != 'MESH': return False return True
def build(self, buildRequest): t = time.time() if self.octree is None: objs = bpy.data.groups[self.settings["obstacleGroup"]].objects margin = self.settings["margin"] mVec = Vector((margin, margin, margin)) radii = [(o.dimensions / 2) + mVec for o in objs] self.octree = createOctreeFromBPYObjs(objs, allSpheres=False, radii=radii) intersections = self.octree.checkPoint(buildRequest.pos) cm_timings.placement["TemplateOBSTACLE"] += time.time() - t cm_timings.placementNum["TemplateOBSTACLE"] += 1 if len(intersections) == 0: self.inputs["Template"].build(buildRequest)
def _get_namelist_items(self, context, nl): # FIXME move from here """Get namelist IDs available in Free Text File""" # Get Free Text File value = str() sc = context.scene if sc.bf_head_free_text: value = bpy.data.texts[sc.bf_head_free_text].as_string() # Tokenize value and manage exception try: tokens = fds.to_py.tokenize(value) except Exception as err: pass # FIXME not good # Select MATL tokens, get IDs, return ids = list() for token in tokens: if token[1] != nl: continue for param in token[2]: if param[1] == "ID": # Built like this: (("Steel", "Steel", "",) ...) ids.append((param[2],param[2],"",)) ids.sort(key=lambda k:k[1]) return ids
def layout_id_prop(layout, data, prop): prop_obj = data.bl_rna.properties[prop] prop_name = prop_obj.name value_key = _create_value_key(prop_name) ref_id = data.get(value_key, None) field_name = json.loads(prop_obj.description)["field_name"] row = layout.row(align=True) row.prop_search(data, prop, bpy.data, field_name) if field_name == "objects": op_props = row.operator(SelectedToIdProperty.bl_idname, emboss=True, icon="EYEDROPPER") op_props.to_populate_data = repr(data) op_props.to_populate_field = prop op_props = row.operator(FindSelected.bl_idname, emboss=True, icon="VIEWZOOM") op_props.to_populate_data = repr(data) op_props.to_populate_field = prop
def create_getter(data_field, value_key): def fn(self): data = getattr(bpy.data, data_field) ob_id = self.get(value_key, None) id_to_hash = ID_TO_HASH[data_field] hash_to_name = HASH_TO_NAME[data_field] ob_hash = id_to_hash.get(ob_id, None) ob_name = hash_to_name.get(ob_hash, None) exists = ob_name is not None and ob_name in data if not exists: for name, ob in data.items(): if ob_hash == hash(ob): hash_to_name[ob_hash] = name ob_name = name break if ob_name is None: ob_name = "" return ob_name return fn
def load_file(_=None): for col_name, _ in SUPPORTED_COLLECTIONS: id_to_hash = {} hash_to_name = {} ID_TO_HASH[col_name] = id_to_hash HASH_TO_NAME[col_name] = hash_to_name col = getattr(bpy.data, col_name) all_obs = sorted(list(col), key=lambda ob: ob.name, reverse=True) for ob in all_obs: # on load, if we encounter an object with a dup id. unset it and let it # regenerate as a unique id if ob.id in id_to_hash: ob["id"] = 0 id_to_hash[ob.id] = hash(ob) hash_to_name[hash(ob)] = ob.name
def create_mesh(self,context,name="Sprite",width=100,height=100,pos=Vector((0,0,0))): me = bpy.data.meshes.new(name) me.show_double_sided = True obj = bpy.data.objects.new(name,me) context.scene.objects.link(obj) context.scene.objects.active = obj obj.select = True self.create_verts(width,height,pos,me,tag_hide=False) v_group = obj.vertex_groups.new("coa_base_sprite") v_group.add([0,1,2,3],1.0,"REPLACE") v_group.lock_weight = True mod = obj.modifiers.new("coa_base_sprite","MASK") mod.vertex_group = "coa_base_sprite" mod.invert_vertex_group = True mod.show_in_editmode = True mod.show_render = False mod.show_viewport = False mod.show_on_cage = True obj.data.coa_hide_base_sprite = False obj.data.uv_textures.new("UVMap") set_uv_default_coords(context,obj) obj.location = Vector((pos[0],pos[1],-pos[2]))*self.scale + Vector((self.offset[0],self.offset[1],self.offset[2]))*self.scale obj["coa_sprite"] = True if self.parent != "None": obj.parent = bpy.data.objects[self.parent] return obj
def create_material(self,context,obj,name="Sprite"): mat = bpy.data.materials.new(name) #mat.use_shadeless = True mat.use_transparency = True mat.alpha = 0.0 mat.specular_intensity = 0.0 mat.diffuse_intensity = 1.0 mat.emit = 1.0 mat.use_object_color = True mat.diffuse_color = (1.0,1.0,1.0) obj.data.materials.append(mat) return mat
def create_texture(self,context,mat,img,name="Sprite"): tex = bpy.data.textures.new(name,"IMAGE") tex.extension = "CLIP" tex.filter_type = "BOX" tex.image = img tex_slot = mat.texture_slots.add() tex_slot.texture = tex tex_slot.use_map_alpha = True if img.source == "MOVIE": tex.image_user.frame_duration = img.frame_duration tex.image_user.frame_start = 0 tex.image_user.use_auto_refresh = True
def execute(self,context): if os.path.exists(self.path): data = bpy.data sprite_name = os.path.basename(self.path) sprite_found = False for image in bpy.data.images: if os.path.exists(bpy.path.abspath(image.filepath)) and os.path.exists(self.path): if os.path.samefile(bpy.path.abspath(image.filepath),self.path): sprite_found = True img = image img.reload() break if not sprite_found: img = data.images.load(self.path) obj = self.create_mesh(context,name=img.name,width=img.size[0],height=img.size[1],pos=self.pos) mat = self.create_material(context,obj,name=img.name) tex = self.create_texture(context,mat,img,name=img.name) msg = sprite_name + " Sprite created." assign_tex_to_uv(img,obj.data.uv_textures.active) obj.coa_sprite_dimension = Vector((get_local_dimension(obj)[0],0,get_local_dimension(obj)[1])) obj.coa_tiles_x = self.tilesize[0] obj.coa_tiles_y = self.tilesize[1] selected_objects = [] for obj2 in context.selected_objects: selected_objects.append(obj2) if obj2 != context.active_object: obj2.select = False obj.coa_z_value = -self.pos[1] for obj2 in selected_objects: obj2.select = True self.report({'INFO'},msg) return{'FINISHED'} else: self.report({'WARNING'},'File does not exist.') return{'CANCELLED'}
def execute(self, context): sprite_found = False for image in bpy.data.images: if os.path.exists(bpy.path.abspath(image.filepath)) and os.path.exists(self.filepath): if os.path.samefile(bpy.path.abspath(image.filepath),self.filepath): sprite_found = True img = image img.reload() break if not sprite_found: img = bpy.data.images.load(self.filepath) scale = get_addon_prefs(context).sprite_import_export_scale active_obj = bpy.data.objects[context.active_object.name] obj = context.active_object if self.name != "" and self.name in bpy.data.objects: obj = bpy.data.objects[self.name] bpy.context.scene.objects.active = obj mat = obj.active_material tex = mat.texture_slots[0].texture tex.image = img tiles_x = int(obj.coa_tiles_x) tiles_y = int(obj.coa_tiles_y) obj.coa_tiles_x = 1 obj.coa_tiles_y = 1 img_dimension = img.size sprite_dimension = Vector(obj.coa_sprite_dimension) * (1/scale) ratio_x = img_dimension[0] / sprite_dimension[0] ratio_y = img_dimension[1] / sprite_dimension[2] self.move_verts(obj,ratio_x,ratio_y) bpy.context.scene.objects.active = active_obj return {'FINISHED'}
def adjust_selected_image(self, context): scene = context.scene try: image = bpy.data.images.load(scene.cubester_load_image) scene.cubester_image = image.name except RuntimeError: self.report({"ERROR"}, "CubeSter: '{}' could not be loaded".format(scene.cubester_load_image)) # load color image if possible
def adjust_selected_color_image(self, context): scene = context.scene try: image = bpy.data.images.load(scene.cubester_load_color_image) scene.cubester_color_image = image.name except RuntimeError: self.report({"ERROR"}, "CubeSter: '{}' could not be loaded".format(scene.cubester_load_color_image)) # crate block at center position x, y with block width 2*hx and 2*hy and height of h
def create_f_curves(mesh, frames, frame_step_size, style): # use data to animate mesh action = bpy.data.actions.new("CubeSterAnimation") mesh.animation_data_create() mesh.animation_data.action = action data_path = "vertices[%d].co" vert_index = 4 if style == "blocks" else 0 # index of first vertex # loop for every face height value for frame_start_vert in range(len(frames[0])): # only go once if plane, otherwise do all four vertices that are in top plane if blocks end_point = frame_start_vert + 4 if style == "blocks" else frame_start_vert + 1 # loop through to get the four vertices that compose the face for frame_vert in range(frame_start_vert, end_point): fcurves = [action.fcurves.new(data_path % vert_index, i) for i in range(3)] # fcurves for x, y, z frame_counter = 0 # go through each frame and add position temp_v = mesh.vertices[vert_index].co # loop through frames for frame in frames: vals = [temp_v[0], temp_v[1], frame[frame_start_vert]] # new x, y, z positions for i in range(3): # for each x, y, z set each corresponding fcurve fcurves[i].keyframe_points.insert(frame_counter, vals[i], {'FAST'}) frame_counter += frame_step_size # skip frames for smoother animation vert_index += 1 # only skip vertices if made of blocks if style == "blocks": vert_index += 4 # create material with given name, apply to object