1 from __future__ import print_function 2 import sys, os, math, wx 3 4 try: 5 import poser 6 except ImportError: 7 # Not required while inside Poser Python, but very helpfull for external editors. 8 # See https://adp.spdns.org 9 from PoserLibs import POSER_FAKE as poser 10 11 if sys.version_info.major > 2: 12 # Python 3 (Poser 12 and above) 13 map = lambda a, b: [a(_b) for _b in b] 14 basestring = str 15 else: 16 range = xrange 17 18 SCENE = poser.Scene() 19 GROUPINGNAME = "BackgroundHDR" 20 P_ROTS = poser.kParmCodeXROT, poser.kParmCodeYROT, poser.kParmCodeZROT 21 P_SCALES = poser.kParmCodeXSCALE, poser.kParmCodeYSCALE, poser.kParmCodeZSCALE 22 P_TRANS = poser.kParmCodeXTRAN, poser.kParmCodeYTRAN, poser.kParmCodeZTRAN 23 24 ROTOBJ_COLOR = (200, 100, 120) 25 26 27 def get_node(shader, nodetype, name=None): 28 """:rtype: poser.ShaderNodeType""" 29 for node in shader.Nodes(): 30 if node.Type() == nodetype: 31 if name and node.Name() != name: 32 continue 33 return node 34 return None 35 36
37 def get_or_create_node(shader, nodetype, name=None): 38 node = get_node(shader, nodetype, name) 39 if node is None: 40 node = shader.CreateNode(nodetype) 41 if name: 42 node.SetName(name) 43 return node 44 45
46 def get_input(node, inp_name): 47 """:rtype: poser.ShaderNodeInputType""" 48 for inp in node.Inputs(): 49 if inp.Name() == inp_name: 50 return inp 51 return None 52 53
54 def set_input(node, name, value): 55 inp = get_input(node, name) 56 if inp: 57 if isinstance(value, basestring): 58 inp.SetString(value) 59 elif isinstance(value, (list, tuple)): 60 inp.SetColor(*value) 61 else: 62 inp.SetFloat(value) 63 64
65 def set_inputs(node, **names_and_values): 66 for name, value in names_and_values.items(): 67 set_input(node, name, value) 68 69
70 def create_background(imagename=None): 71 SCENE.Actor("GROUND").SetVisible(0) 72 shdr = SCENE.BackgroundShaderTree() 73 bkgrnd = get_or_create_node(shdr, poser.kNodeTypeCodeBACKGROUND) 74 set_inputs(bkgrnd, 75 Color=(1.0, 1.0, 1.0), 76 Specular_Color=(0.0, 0.0, 0.0), 77 Diffuse_Color=(0.0, 0.0, 0.0), 78 Cast_Light=1.0 79 ) 80 81 brightness = get_or_create_node(shdr, poser.kNodeTypeCodeCyclesBRIGHTCONTRAST) 82 img = get_or_create_node(shdr, poser.kNodeTypeCodeCyclesENVIRONMENTTEXTURE) 83 mapping = get_or_create_node(shdr, poser.kNodeTypeCodeCyclesMAPPING) 84 tcoords = get_or_create_node(shdr, poser.kNodeTypeCodeCyclesTEXTURECOORDINATE) 85 86 shdr.AttachTreeNodes(bkgrnd, "Color", brightness) 87 shdr.AttachTreeNodes(brightness, "Color", img) 88 shdr.AttachTreeNodes(img, "Vector", mapping) 89 shdr.AttachTreeNodes(mapping, "Vector", tcoords) 90 91 if isinstance(imagename, basestring): 92 get_input(img, "Image").SetString(imagename) 93 94 shdr.UpdatePreview() 95 96 97 _visible_states = dict(state=1, figures=dict(), props=dict()) # used for scene object hiding. 98 99
100 def create_rotobj(): 101 def _cb_bright(parm, value): 102 node = get_node(SCENE.BackgroundShaderTree(), poser.kNodeTypeCodeCyclesBRIGHTCONTRAST) 103 if node: 104 set_input(node, "Bright", value * .01) 105 return value 106 107 def _cb_contrast(parm, value): 108 node = get_node(SCENE.BackgroundShaderTree(), poser.kNodeTypeCodeCyclesBRIGHTCONTRAST) 109 if node: 110 set_input(node, "Contrast", value * .01) 111 return value 112 113 def _cb_light(parm, value): 114 for ac in SCENE.Lights(): # type: poser.ActorType 115 if ac.LightType() != poser.kLightCodeIMAGE: 116 try: 117 ac.SetLightOn(int(value)) 118 except poser.error: 119 pass 120 return value 121 122 def _cb_scene_hide(parm, value): 123 if _visible_states.get("state", 1) == int(value): 124 return value 125 126 poser.ProcessCommand(1090) 127 _visible_states["state"] = int(value) 128 if int(value) == 0: 129 d = _visible_states.setdefault("figures", dict()) 130 for fig in SCENE.Figures(): 131 d[fig.InternalName()] = fig.Visible() 132 fig.SetVisible(0) 133 d = _visible_states.setdefault("props", dict()) 134 for ac in [_a for _a in SCENE.Actors() if _a.IsProp()]: #type: poser.ActorType 135 if ac.Name() == GROUPINGNAME: 136 continue 137 d[ac.InternalName()] = ac.Visible() 138 ac.SetVisible(0) 139 else: 140 for iname, v in _visible_states.get("figures", dict()).items(): 141 SCENE.FigureByInternalName(iname).SetVisible(v) 142 for iname, v in _visible_states.get("props", dict()).items(): 143 SCENE.ActorByInternalName(iname).SetVisible(v) 144 145 poser.ProcessCommand(1091) 146 return value 147 148 def _inp_set(parm, v): 149 assert isinstance(parm, poser.ParmType) 150 shdr = SCENE.BackgroundShaderTree() 151 node = get_node(shdr, poser.kNodeTypeCodeCyclesMAPPING) 152 if node: 153 code = parm.TypeCode() 154 if code == poser.kParmCodeXTRAN: 155 inp = get_input(node, "Location") 156 idx = 0 157 elif code == poser.kParmCodeYTRAN: 158 inp = get_input(node, "Location") 159 idx = 1 160 elif code == poser.kParmCodeZTRAN: 161 inp = get_input(node, "Location") 162 idx = 2 163 elif code == poser.kParmCodeXROT: 164 inp = get_input(node, "Rotation") 165 idx = 0 166 elif code == poser.kParmCodeYROT: 167 inp = get_input(node, "Rotation") 168 idx = 1 169 elif code == poser.kParmCodeZROT: 170 inp = get_input(node, "Rotation") 171 idx = 2 172 elif code == poser.kParmCodeXSCALE: 173 inp = get_input(node, "Scale") 174 idx = 0 175 elif code == poser.kParmCodeYSCALE: 176 inp = get_input(node, "Scale") 177 idx = 1 178 elif code == poser.kParmCodeZSCALE: 179 inp = get_input(node, "Scale") 180 idx = 2 181 else: 182 return v 183 184 vec = list(inp.Value()) 185 if code in P_ROTS: 186 vec[idx] = math.radians(v) 187 elif code == poser.kParmCodeXSCALE: 188 vec[idx] = -v 189 else: 190 vec[idx] = v 191 inp.SetColor(*vec) 192 return v 193 194 try: 195 obj = SCENE.Actor(GROUPINGNAME) 196 except poser.error: 197 obj = SCENE.CreateGrouping() 198 obj.SetName(GROUPINGNAME) 199 obj.SetDisplayStyle(poser.kDisplayCodeEDGESONLY) 200 201 shdr = obj.Material("Preview").ShaderTree() 202 r, g, b = map(lambda n: 1.0 / 255.0 * n, ROTOBJ_COLOR) 203 shdr.Node(0).Input(0).SetColor(r, g, b) 204 shdr.UpdatePreview() 205 206 parmnames = [p.Name() for p in obj.Parameters()] 207 for parmname, cb, pp in [ 208 ("Hide/Show Scene", _cb_scene_hide, (0, 1, 1, 1)), 209 ("Lights Off/On", _cb_light, (0, 1, 1, 1)), 210 ("Contrast %", _cb_contrast, (-100, 100, 0, 1)), 211 ("Brightness %", _cb_bright, (-100, 100, 0, 1)), 212 ]: 213 if parmname in parmnames: 214 p = obj.Parameter(parmname) 215 else: 216 p = obj.CreateValueParameter(parmname) 217 min_v, max_v, default_v, sens = pp 218 if min_v is not None: 219 p.SetMinValue(min_v) 220 if max_v is not None: 221 p.SetMaxValue(max_v) 222 if default_v is not None: 223 p.SetValue(default_v) 224 if min_v is not None or max_v is not None: 225 p.SetForceLimits(1) 226 p.ApplyLimits() 227 if sens is not None: 228 p.SetSensitivity(sens) 229 p.SetUpdateCallback(cb) 230 231 for code in P_TRANS + P_ROTS + P_SCALES: 232 obj.ParameterByCode(code).SetUpdateCallback(_inp_set) 233 for code in P_SCALES: 234 obj.ParameterByCode(code).SetValue(1) 235 for code in P_TRANS + P_ROTS: 236 obj.ParameterByCode(code).SetValue(0) 237 238 return obj 239 240 241 if __name__ == "__main__": 242 filename = None 243 # with wx.FileDialog(None, "Select Background Image") as dlg: 244 # if dlg.ShowModal() == wx.ID_OK: 245 # filename = os.path.join(dlg.GetPath()) 246 create_background(filename) 247 obj = create_rotobj() 248 SCENE.DrawAll() 249