def gen_rig_and_reparent(self, metarig, unparent_at_rest=True): """ genrigして再ペアレントする。 """ if metarig.type != "ARMATURE": return False layers_current = fjw.layers_current_state() fjw.layers_showall() objects_bu = fjw.ObjectsPropBackups(bpy.context.scene.objects) objects_bu.store("hide") for obj in bpy.context.scene.objects: obj.hide = False self.set_metarig(metarig) self.set_rig(self.find_rig()) rig_old = None if self.rig: rig_old = self.rig if unparent_at_rest: self.rig.obj.data.pose_position = 'REST' self.rig.rigged_objects.parent_clear() rigdata = self.rig.obj.data fjw.delete([self.rig.obj]) bpy.data.armatures.remove(rigdata) fjw.deselect() fjw.activate(metarig) bpy.ops.pose.rigify_generate() new_rig = Rig(fjw.active()) if self.rig: new_rig.edit_bones_data.restore_info_from(self.rig.edit_bones_data) self.rig.rigged_objects.reset_rig(new_rig.obj) # bpy.ops.view3d.layers(nr=0, extend=False) if self.rig: self.rig.rigged_objects.reparent() # bpy.ops.view3d.layers(nr=0, extend=False) fjw.layers_show_list(layers_current) metarig.hide = True fjw.activate(new_rig.obj) fjw.mode("POSE") if rig_old: new_rig.restore_settings_from(rig_old) objects_bu.restore() return True
def execute(self,context): if is_pagemode(): #ファイルの存在チェック # for img in bpy.data.images: # path = bpy.path.abspath(img.filepath) # if not os.path.exists(path): # #ないのでオブジェクト消す dellist = [] for obj in bpy.context.scene.objects: if obj.type != "MESH": continue #blendファイルの無いものを削除 #ないならimgも削除すべき self_dir = os.path.dirname(bpy.data.filepath) imgdir = self_dir + os.sep + "pageutils" + os.sep + "img" + os.sep blend_filepath = self_dir + os.sep + obj.name + ".blend" if not os.path.exists(blend_filepath): dellist.append(obj) img_path = imgdir + obj.name + ".png" if os.path.exists(img_path): os.remove(img_path) #imgのないものは削除 print("MESH Object") for mat in obj.data.materials: print(mat) for tslot in mat.texture_slots: if tslot is not None and tslot.texture is not None and tslot.texture.image is not None: img = tslot.texture.image path = img.filepath print(path) if not os.path.exists(path): bpy.data.images.remove(img) print("not exists:%s"%(path)) if obj not in dellist: dellist.append(obj) break print("dellist:") print(dellist) fjw.delete(dellist) for n in range(50): if not refresh_command(self): break bpy.ops.file.make_paths_relative() return {"FINISHED"}
def setup_mdwork_main(cls, self, context): if "_MDWork" not in bpy.data.filepath: fjw.framejump(cls.last_frame) dir = os.path.dirname(bpy.data.filepath) name = os.path.splitext(os.path.basename(bpy.data.filepath))[0] blend_md = dir + os.sep + name + "_MDWork.blend" bpy.ops.wm.save_as_mainfile(filepath=blend_md) bpy.context.scene.layers[0] = True for i in range(19): bpy.context.scene.layers[i + 1] = False for i in range(5): bpy.context.scene.layers[i] = True #ポーズだけついてるやつをポーズライブラリに登録する for armature_proxy in bpy.context.visible_objects: if armature_proxy.type != "ARMATURE": continue if "_proxy" not in armature_proxy.name: continue fjw.deselect() fjw.activate(armature_proxy) fjw.mode("POSE") bpy.ops.pose.select_all(action='SELECT') bpy.ops.fujiwara_toolbox.set_key() fjw.mode("OBJECT") fjw.mode("OBJECT") bpy.ops.object.select_all(action='SELECT') bpy.ops.file.make_paths_absolute() selection = fjw.get_selected_list() for obj in selection: md_garment_path_list = cls.__get_prop(obj, "md_garment_path_list") md_export_id = cls.__get_prop(obj, "md_export_id") md_garment_index = cls.__get_prop(obj, "md_garment_index") md_export_depth = cls.__get_prop(obj, "md_export_depth") # obj.dupli_group.library.filepath link_path = "" if obj.dupli_group is not None and obj.dupli_group.library is not None: link_path = obj.dupli_group.library.filepath if link_path == "" or link_path is None: continue fjw.deselect() fjw.activate(obj) bpy.ops.object.duplicates_make_real(use_base_parent=True, use_hierarchy=True) realized_objects = fjw.get_selected_list() for robj in realized_objects: robj["linked_path"] = link_path root = fjw.get_root(robj) if md_garment_path_list is not None: root["md_garment_path_list"] = md_garment_path_list if md_export_id is not None: root["md_export_id"] = md_export_id if md_garment_index is not None: root["md_garment_index"] = md_garment_index if md_export_depth is not None: root["md_export_depth"] = md_export_depth #garment_pathを絶対パス化する if "md_garment_path_list" in robj: pathlist_base = robj["md_garment_path_list"] print("*" * 15) print("* make garment path to abs") print("*" * 15) lib_dir = os.path.dirname(link_path) pathlist_new = [] for garment_path in pathlist_base: new_path = bpy.path.abspath(garment_path, start=lib_dir) pathlist_new.append(new_path) print(new_path) robj["md_garment_path_list"] = pathlist_new print("*" * 15) #proxyの処理 #同一のアーマチュアデータを使っているものを探してポーズライブラリを設定する。 for armature_proxy in bpy.data.objects: if armature_proxy.type != "ARMATURE": continue if "_proxy" not in armature_proxy.name: continue for armature in bpy.data.objects: if armature.type != "ARMATURE": continue if armature == armature_proxy: continue if armature.data == armature_proxy.data: #同一データを使用している #のでポーズライブラリの設定をコピーする armature.pose_library = armature_proxy.pose_library #回収したポーズライブラリを反映する fjw.mode("OBJECT") fjw.activate(armature) if fjw.active() is not None: aau = fjw.ArmatureActionUtils(armature) armu = fjw.ArmatureUtils(armature) fjw.active().hide = False fjw.mode("POSE") poselist = aau.get_poselist() if poselist is not None: for pose in aau.get_poselist(): frame = int( str(pose.name).replace("mdpose_", "")) fjw.framejump(frame) #ジオメトリはゼロ位置にする geo = armu.GetGeometryBone() armu.clearTrans([geo]) bpy.ops.pose.select_all(action='SELECT') armu.databone(geo.name).select = False aau.apply_pose(pose.name) #1フレームではデフォルトポーズに fjw.mode("POSE") fjw.framejump(1) bpy.ops.pose.select_all(action='SELECT') bpy.ops.pose.transforms_clear() #proxyの全削除 fjw.mode("OBJECT") prxs = fjw.find_list("_proxy") fjw.delete(prxs) # bpy.app.handlers.scene_update_post.append(process_proxy) bpy.context.space_data.show_only_render = False
def mdresult_auto_import_main(cls, self, context, attouch_fjwset=False): import_ext = ".obj" #存在確認 # blendname = os.path.splitext(os.path.basename(bpy.data.filepath))[0] # dir_path = get_mddatadir() + blendname + os.sep dir_path = get_mddatadir() self.report({"INFO"}, dir_path) if not os.path.exists(dir_path): self.report({"INFO"}, "キャンセルされました。") # bpy.ops.wm.quit_blender() return {'CANCELLED'} #既存のリザルトを処分 fjw.deselect() dellist = [] for obj in bpy.context.scene.objects: if obj.type == "MESH" and "result" in obj.name: dellist.append(obj) fjw.delete(dellist) root_objects = [] for obj in bpy.context.scene.objects: if obj.parent is None: root_objects.append(obj) mdj = MDJson() entries = mdj.get_thisfile_entries() # files = os.listdir(dir_path) # for file in files: files = [] for entry in entries: file = entry + import_ext # 対応するファイルが実際にあるかをチェックしないとだめ! filepath = dir_path + file if not os.path.exists(filepath): continue files.append(file) for file in files: self.report({"INFO"}, file) print("MDResult found:" + file) # targetname = file # dirname = os.path.basename(os.path.dirname(file)) targetname = file # rootobjでの設置だとルートがないとおかしなことになる # dupli_groupの名前でみて、同一名のもののアーマチュアを探して、 # vislble_objects内のそのデータと同一のプロクシないしアーマチュア、のジオメトリを指定すればいいのでは #グループ名だと全部"MainGroup"だからマッチしない! #グループの親ファイルパスの名前を見る!! #複製子除去 # targetname = re.sub(r"\.\d+", "", targetname) print("basename:%s, targetname:%s" % (file, targetname)) #fileと同名のdupli_groupを検索 dupli_group = None for group in bpy.data.groups: # gname = re.sub(r"\.\d+", "", group.name) if group.name == "MainGroup": if group.library is not None: g_filepath = group.library.filepath g_filename = os.path.basename(g_filepath) gname, ext = os.path.splitext(g_filename) else: gname = group.name else: gname = group.name if targetname == gname: dupli_group = group break if not dupli_group: print("!targetname not in bpy.data.groups!:%s" % targetname) else: print("*targetname found*:%s" % targetname) if dupli_group: dgroup = dupli_group #Bodyが参照しているアーマチュアのデータを取得 target_armature = None #この検索問題 名前Bodyじゃない可能性がある まずカスタムプロパティで探すべき #普通に全てのアーマチュアがシーン上のアーマチュアと一致したらそれ採用でいいのでは? # for obj in dgroup.objects: for scene_amature in bpy.context.visible_objects: if scene_amature.type != "ARMATURE": continue linked_object = scene_amature.proxy_group if linked_object: #グループが一致 if linked_object.dupli_group == dupli_group: #ルートアーマチュアを採用する target_armature = cls.__find_root_armature( scene_amature) break # if "Body" in dgroup.objects: # Body = dgroup.objects["Body"] # modu = fjw.Modutils(Body) # armt = modu.find("Armature") # if armt is not None: # armature = armt.object # if armature is not None: # armature_data = armature.data # for scene_amature in bpy.context.visible_objects: # if scene_amature.type != "ARMATURE": # continue # if scene_amature.data != armature_data: # continue # #同一のアーマチュアデータを発見したのでこいつを使用する # target_armature = scene_amature # break if not target_armature: print("!target_armature not found!") if target_armature is not None: print("target_armature:%s" % target_armature) arm = target_armature print("MDImport Step 0") fjw.mode("OBJECT") fjw.deselect() fjw.activate(arm) print("MDImport Step 1") fjw.mode("POSE") armu = fjw.ArmatureUtils(arm) geo = armu.GetGeometryBone() armu.activate(geo) print("MDImport Step 2") fjw.mode("POSE") self.report({"INFO"}, dir_path + file) print("MDImport Selecting GeoBone:" + dir_path + file) #インポート # mdresultpath = dir_path + file + os.sep + "result" + import_ext mdresultpath = dir_path + file MarvelousDesingerUtils.import_mdresult(mdresultpath, attouch_fjwset) print("MDImport Import MDResult:" + mdresultpath) fjw.mode("OBJECT") for obj in bpy.context.visible_objects: if "result" in obj.name: obj.select = True #テクスチャのパック cls.__pack_img_of_obj(obj)
def fjw_texture_export_uvmap(cls, obj): """ アクティブオブジェクトのアクティブUVマップ書き出してテクスチャ作業をする。 arguments: オブジェクト outputs: アクティブUV画像を書き出し 作業ディレクトリ/textures/uv名.png jsonにテクスチャパスを保存する?いらないかも 既にblendファイルが存在する場合はそのblendファイルを開いて完了したい。 """ if obj.type != "MESH": return export_name = "uv_map" path = cls.get_exist_filepath(obj, export_name) if path: #既に存在するのでそれを開く bpy.ops.wm.open_mainfile(filepath=path) return fjw.activate(obj) # UVをチェック if len(obj.data.uv_layers) == 0: # なければUVを作成 fjw.mode("EDIT") bpy.ops.mesh.select_all(action='SELECT') bpy.ops.uv.smart_project() uv_layer = obj.data.uv_layers.active # テクスチャ作業ファイル作成 json = cls.make_texture_workingfile(obj, export_name) # if not json: # return dirname = os.path.dirname(bpy.data.filepath) basename = os.path.basename(bpy.data.filepath) name, ext = os.path.splitext(basename) print("filepath:%s" % bpy.data.filepath) # UVテクスチャ出力先 uv_dir = dirname + os.sep + "textures" if not os.path.exists(uv_dir): os.makedirs(uv_dir) uv_path = uv_dir + os.sep + name + ".png" print("uv_path:%s" % uv_path) fjw.mode("EDIT") bpy.ops.mesh.select_all(action='SELECT') bpy.ops.uv.export_layout(filepath=uv_path, check_existing=False, export_all=False, modified=False, mode='PNG', size=(1024, 1024), opacity=0.25, tessellated=False) fjw.mode("OBJECT") dellist = [] for delobj in bpy.context.scene.objects: if delobj != obj: dellist.append(delobj) # 全オブジェクトを削除してUVテクスチャの読み込み fjw.delete(dellist) bpy.ops.mesh.primitive_plane_add( radius=1, calc_uvs=True, view_align=False, enter_editmode=False, location=(0, 0, 0), layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False)) plane = fjw.active() plane.name = name mat = bpy.data.materials.new("uv_map") img = bpy.data.images.load(uv_path) # mat.use_shadeless = True # tslot = mat.texture_slots.add() # tex = bpy.data.textures.new("uv_map", "IMAGE") # tex.image = img # tslot.texture = tex plane.data.materials.append(mat) # 背景画像として追加 # https://blender.stackexchange.com/questions/6101/poll-failed-context-incorrect-example-bpy-ops-view3d-background-image-add/6105#6105 bpy.context.space_data.show_background_images = True for area in bpy.context.screen.areas: if area.type == 'VIEW_3D': space_data = area.spaces.active bg = space_data.background_images.new() bg.image = img bg.size = 2 bg.draw_depth = "FRONT" bg.view_axis = "TOP" break #SUN追加 bpy.ops.object.lamp_add(type='SUN', radius=1, view_align=False, location=(0, 0, 1), layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False)) bpy.ops.wm.save_as_mainfile()