C4D UV脚本 Python : ProjUV(原版+汉化版)

2014-11-21 22:33 发布 | 作品版权归原作者所有,仅供参考学习,禁止商业使用!

插件脚本 /[UV/贴图]
1853 7 0
C4D UV脚本 Python : ProjUV
C4D UV脚本 Python : ProjUV(原版+汉化版) - C4D之家 - image_01.png C4D UV脚本 Python : ProjUV(原版+汉化版) - C4D之家 - image_02.png C4D UV脚本 Python : ProjUV(原版+汉化版) - C4D之家 - image_03.png

Fichier d'exemple : Télécharger

Projette le dépliage UV d'un objet sur un autre.

L'outil se présente sous la forme d'une propriété à appliquer sur l'objet à texturer.

L'utilisation d'une même projection sur deux modèles différents permet de les texturer de la même façon malgré leur différence de maillage.

v 1.2 :
- Correction de l'absence de rafraîchissement sous l'OpenGL.

v 1.15 :
- Modèle de projection d'exemple modifié.

v 1.1 :
- Les polygones affectés par la projection sont enregistrés.
- Correction d'une faute de frappe dans le menu.


Copier le dossier « vonc_projuv » dans le dossier « plugins » du répertoire de Cinéma 4D.
Fichier vonc_projuv.PYP :
  1. # ProjUV - v 1.2 - vonc.fr

  2. import os
  3. import c4d
  4. from c4d import bitmaps, plugins, utils, Vector
  5. from c4d.utils import GeRayCollider, Neighbor

  6. MODULE_ID = 1029370
  7. VONCPROJUV_MAJAUTO = 1000
  8. VONCPROJUV_OBJ = 1001
  9. VONCPROJUV_UVW = 1002
  10. VONCPROJUV_SEL = 1003
  11. VONCPROJUV_ACTU = 1004
  12. VONCPROJUV_PLAN = 1005
  13. VONCPROJUV_INVNOR = 1006
  14. VONCPROJUV_INFO = 1007
  15. VONCPROJUV_AFF = 1008

  16. class ProjUV(plugins.TagData):
  17. lienObj = None
  18. lienUVW = None
  19. lienSel = None
  20. lienAff = None
  21. majauto = False
  22. actuprio = False
  23. creerplan = False
  24. invnor = False

  25. def InitDonneeAff(self, donnees, obj) :
  26. if self.lienAff : return
  27. propAff = c4d.BaseTag(c4d.Tpolygonselection)
  28. propAff[c4d.ID_BASELIST_NAME] = "ProjUV Sel"
  29. obj.InsertTag(propAff)
  30. donnees.SetLink(VONCPROJUV_AFF, propAff)
  31. self.lienAff = propAff

  32. def InitDonneeUVW(self, donnees, obj) :
  33. if self.lienUVW : return
  34. propUVW = obj.GetTag(c4d.Tuvw)
  35. if propUVW :
  36. donnees.SetLink(VONCPROJUV_UVW, propUVW)
  37. self.lienUVW = propUVW

  38. def RecupDonnees(self, donnees, doc) :
  39. self.lienObj = donnees.GetLink(VONCPROJUV_OBJ, doc)
  40. self.lienUVW = donnees.GetLink(VONCPROJUV_UVW, doc)
  41. self.lienSel = donnees.GetLink(VONCPROJUV_SEL, doc)
  42. self.lienAff = donnees.GetLink(VONCPROJUV_AFF, doc)
  43. self.majauto = donnees.GetBool(VONCPROJUV_MAJAUTO)
  44. self.invnor = donnees.GetBool(VONCPROJUV_INVNOR)

  45. def Verifieur(self, obj, uvw, tex) :
  46. if self.actuprio :
  47. self.actuprio = False
  48. return True
  49. if not self.majauto : return False
  50. if not uvw or not tex : return False
  51. if not obj.CheckType(c4d.Opolygon) : return False
  52. if obj.IsDirty(c4d.DIRTY_MATRIX) : return True
  53. if obj.IsDirty(c4d.DIRTY_DATA) : return True
  54. texEtGosses = [tex]
  55. for o in texEtGosses :
  56. if o.IsDirty(c4d.DIRTY_DATA) or o.IsDirty(c4d.DIRTY_MATRIX) :
  57. return True
  58. texEtGosses.extend(o.GetChildren())

  59. return False

  60. def Applatisseur(self, o, doc) :
  61. if not o : return
  62. obj = utils.SendModelingCommand(command=c4d.MCOMMAND_CURRENTSTATETOOBJECT, list=[o], doc=doc)
  63. if not obj : return
  64. neutre = c4d.BaseObject(c4d.Onull)
  65. obj[0].InsertUnder(neutre)
  66. obj = utils.SendModelingCommand(command=c4d.MCOMMAND_JOIN, list=[neutre], doc=doc)
  67. if not obj : return
  68. utils.SendModelingCommand(command=c4d.MCOMMAND_TRIANGULATE, list=obj, doc=doc)
  69. if self.invnor :
  70. utils.SendModelingCommand(command=c4d.MCOMMAND_REVERSENORMALS, list=obj, doc=doc)
  71. return obj[0]

  72. def CalculNormaleEtUVW(self, o) :
  73. uvw = o.GetTag(c4d.Tuvw)
  74. if not uvw : return None, None
  75. polygones = o.GetAllPolygons()
  76. nbPoints = o.GetPointCount()
  77. normales = [Vector()] * nbPoints
  78. uvws = [Vector()] * nbPoints
  79. i = 0

  80. for poly in polygones :
  81. a = poly.a
  82. b = poly.b
  83. c = poly.c
  84. d = poly.d
  85. ap = o.GetPoint(a)
  86. bp = o.GetPoint(b)
  87. cp = o.GetPoint(c)
  88. dp = o.GetPoint(d)
  89. n = (ap - cp).Cross(bp - dp)
  90. normales[a] += n
  91. normales[b] += n
  92. normales[c] += n
  93. normales[d] += n
  94. uvwdict = uvw.GetSlow(i)
  95. uvws[a] = uvwdict["a"]
  96. uvws[b] = uvwdict["b"]
  97. uvws[c] = uvwdict["c"]
  98. uvws[d] = uvwdict["d"]

  99. i += 1

  100. return normales, uvws

  101. def CompleteNormales(self, n, obj, objNbPts, objNor, objNorBol) :
  102. for ip in xrange(objNbPts) :
  103. ipolys = n.GetPointPolys(ip)
  104. if not objNorBol[ip] : continue
  105. direc = objNor[ip]
  106. for ipoly in ipolys :
  107. poly = obj.GetPolygon(ipoly)
  108. a = poly.a
  109. b = poly.b
  110. c = poly.c
  111. d = poly.d
  112. if objNor[a] == Vector() : objNor[a] = direc
  113. if objNor[b] == Vector() : objNor[b] = direc
  114. if objNor[c] == Vector() : objNor[c] = direc
  115. if objNor[d] == Vector() : objNor[d] = direc
  116. objNorBol[a] = True
  117. objNorBol[b] = True
  118. objNorBol[c] = True
  119. objNorBol[d] = True

  120. def CreerProjPlanaire(self, doc, donnees) :
  121. self.creerplan = False
  122. bd = doc.GetActiveBaseDraw()
  123. cam = bd.GetSceneCamera(doc)
  124. mg = cam.GetMg() * c4d.utils.MatrixMove(c4d.Vector(0,0,300))

  125. hn = c4d.BaseObject(c4d.Osds)
  126. hn[c4d.SDSOBJECT_SUBDIVIDE_UV] = c4d.SDSOBJECT_SUBDIVIDE_UV_EDGE
  127. hn[c4d.SDSOBJECT_SUBEDITOR_CM] = 2
  128. hn[c4d.SDSOBJECT_SUBRAY_CM] = 2
  129. hn.SetMg(mg)
  130. plan = c4d.BaseObject(c4d.Oplane)
  131. plan[c4d.PRIM_PLANE_WIDTH] = 200
  132. plan[c4d.PRIM_PLANE_HEIGHT] = 200
  133. plan[c4d.PRIM_PLANE_SUBW] = 3
  134. plan[c4d.PRIM_PLANE_SUBH] = 3
  135. plan[c4d.PRIM_AXIS] = 4
  136. plan[c4d.ID_BASEOBJECT_USECOLOR] = 2
  137. plan[c4d.ID_BASEOBJECT_COLOR] = c4d.Vector(1.0, 0.8, 0.0)
  138. aff = c4d.BaseTag(c4d.Tdisplay)
  139. aff[c4d.DISPLAYTAG_AFFECT_DISPLAYMODE] = True
  140. aff[c4d.DISPLAYTAG_SDISPLAYMODE] = c4d.DISPLAYTAG_SDISPLAY_NOSHADING
  141. aff[c4d.DISPLAYTAG_WDISPLAYMODE] = c4d.DISPLAYTAG_WDISPLAY_ISOPARMS
  142. hn.InsertTag(aff)
  143. plan.InsertUnder(hn)
  144. res = utils.SendModelingCommand(command = c4d.MCOMMAND_MAKEEDITABLE, list = [plan], doc = doc)
  145. res[0].InsertUnder(hn)
  146. doc.InsertObject(hn)
  147. donnees.SetLink(VONCPROJUV_OBJ, hn)

  148. def Init(self, node):
  149. donnees = node.GetDataInstance()
  150. donnees.SetBool(VONCPROJUV_MAJAUTO, True)
  151. return True

  152. def Execute(self, tag, doc, op, bt, priority, flags):
  153. donnees = tag.GetDataInstance()
  154. if not donnees : return c4d.EXECUTIONRESULT_OK

  155. if self.creerplan : self.CreerProjPlanaire(doc, donnees)

  156. self.RecupDonnees(donnees, doc)
  157. self.InitDonneeUVW(donnees, op)
  158. self.InitDonneeAff(donnees, op)

  159. obj = op
  160. objPropUVW = self.lienUVW
  161. tex = self.lienObj
  162. selPolys = self.lienSel

  163. verif = self.Verifieur(obj, objPropUVW, tex)
  164. if not verif : return c4d.EXECUTIONRESULT_OK

  165. tex = self.Applatisseur(tex, doc)
  166. if not tex : return c4d.EXECUTIONRESULT_OK

  167. texNor, texUVW = self.CalculNormaleEtUVW(tex)
  168. if not texUVW : return c4d.EXECUTIONRESULT_OK

  169. texPts = tex.GetAllPoints()
  170. texMg = tex.GetMg()

  171. objNbPolys = obj.GetPolygonCount()
  172. objNbPts = obj.GetPointCount()
  173. objPts = obj.GetAllPoints()
  174. objMg = obj.GetMg()
  175. objIMg = ~objMg
  176. objUVW = [None] * objNbPts
  177. objNor = [Vector()] * objNbPts
  178. objNorBol = [False] * objNbPts

  179. bs = None
  180. objPtsSel = [False] * objNbPts
  181. if selPolys :
  182. bs = selPolys.GetBaseSelect()
  183. for index, sele in enumerate(bs.GetAll(objNbPolys)) :
  184. if not sele : continue
  185. poly = obj.GetPolygon(index)
  186. objPtsSel[poly.a] = True
  187. objPtsSel[poly.b] = True
  188. objPtsSel[poly.c] = True
  189. objPtsSel[poly.d] = True

  190. distance = (obj.GetRad() + obj.GetMp() + objMg.off + (tex.GetRad() + tex.GetMp() + texMg.off)).GetLength()
  191. distance *= 2

  192. rayon = GeRayCollider()
  193. rayon.Init(obj)
  194. i = 0
  195. for pt in texPts :
  196. depart = objIMg.Mul(pt) + 0.0001
  197. direc = objIMg.MulV(texNor[i])
  198. #ajVec(depart, direc)
  199. coll = rayon.Intersect(depart, direc, distance)
  200. inter = rayon.GetNearestIntersection()

  201. if inter :
  202. pos = inter["hitpos"]
  203. poly = inter["face_id"]
  204. poly = obj.GetPolygon(poly)
  205. nor = direc.GetNormalized() * -1
  206. #ajVec(pos, direc)
  207. objNor[poly.a] += nor
  208. objNor[poly.b] += nor
  209. objNor[poly.c] += nor
  210. objNor[poly.d] += nor
  211. objNorBol[poly.a] = True
  212. objNorBol[poly.b] = True
  213. objNorBol[poly.c] = True
  214. objNorBol[poly.d] = True
  215. i += 1

  216. n = Neighbor()
  217. n.Init(obj)
  218. somme = sum(objNorBol)
  219. while somme != objNbPts :
  220. sommeAns = somme
  221. self.CompleteNormales(n, obj, objNbPts, objNor, objNorBol)
  222. somme = sum(objNorBol)
  223. if sommeAns == somme : break

  224. rayon.Init(tex)
  225. i = 0
  226. for pt in objPts :
  227. if bs :
  228. if not objPtsSel[i] :
  229. i += 1
  230. continue
  231. depart = objMg.Mul(pt) + 0.0001
  232. direc = objMg.MulV(objNor[i].GetNormalized())
  233. #ajVec(depart, direc)
  234. coll = rayon.Intersect(depart, direc, distance)
  235. inter = rayon.GetNearestIntersection()

  236. if inter :
  237. bc = inter['barrycoords']
  238. poly = inter["face_id"]
  239. poly = tex.GetPolygon(poly)
  240. a = poly.a
  241. b = poly.b
  242. c = poly.c
  243. objUVW[i] = texUVW[a]*bc.x + texUVW[b]*bc.y + texUVW[c]*bc.z

  244. i += 1

  245. #doc.AddUndo(c4d.UNDOTYPE_CHANGE, objPropUVW)

  246. objPolAff = self.lienAff.GetBaseSelect()
  247. objPolAff.DeselectAll()

  248. if bs :
  249. for index, sele in enumerate(bs.GetAll(objNbPolys)):
  250. if not sele : continue
  251. poly = obj.GetPolygon(index)
  252. a = objUVW[poly.a]
  253. b = objUVW[poly.b]
  254. c = objUVW[poly.c]
  255. d = objUVW[poly.d]
  256. if not a or not b or not c or not d : continue
  257. objPolAff.Select(index)
  258. objPropUVW.SetSlow(index, a, b, c, d)

  259. else :
  260. for j in xrange(obj.GetPolygonCount()) :
  261. poly = obj.GetPolygon(j)
  262. a = objUVW[poly.a]
  263. b = objUVW[poly.b]
  264. c = objUVW[poly.c]
  265. d = objUVW[poly.d]
  266. if not a or not b or not c or not d : continue
  267. objPolAff.Select(j)
  268. objPropUVW.SetSlow(j, a, b, c, d)

  269. #doc.EndUndo()
  270. #c4d.EventAdd()

  271. obj.Message(c4d.MSG_UPDATE)

  272. return c4d.EXECUTIONRESULT_OK

  273. def Message(self, op, type, donnees) :
  274. if type == c4d.MSG_DESCRIPTION_COMMAND :
  275. id = donnees["id"][0].id
  276. if id == VONCPROJUV_ACTU :
  277. self.actuprio = True
  278. elif id == VONCPROJUV_PLAN :
  279. self.creerplan = True
  280. return True

  281. if __name__ == "__main__":
  282. bmp = bitmaps.BaseBitmap()
  283. dir, file = os.path.split(__file__)
  284. fn = os.path.join(dir, "res", "vonc_projuv.tif")
  285. bmp.InitWith(fn)
  286. plugins.RegisterTagPlugin(id=MODULE_ID, str="ProjUV",
  287. info=c4d.TAG_EXPRESSION | c4d.TAG_VISIBLE, g=ProjUV,
  288. description="vonc_projuv", icon=bmp)
复制代码

Fichier vonc_projuv.H :
  1. #ifndef _vonc_projuv_H_
  2. #define _vonc_projuv_H_

  3. enum
  4. {
  5. VONCPROJUV_MAJAUTO = 1000,
  6. VONCPROJUV_OBJ = 1001,
  7. VONCPROJUV_UVW = 1002,
  8. VONCPROJUV_SEL = 1003,
  9. VONCPROJUV_ACTU = 1004,
  10. VONCPROJUV_PLAN = 1005,
  11. VONCPROJUV_INVNOR = 1006,
  12. VONCPROJUV_INFO = 1007,
  13. VONCPROJUV_AFF = 1008
  14. }

  15. #endif
复制代码
Fichier vonc_projuv.RES :
  1. CONTAINER vonc_projuv
  2. {
  3. NAME VONCPROJUV_TITRE;
  4. INCLUDE Texpression;

  5. GROUP ID_TAGPROPERTIES
  6. {
  7. GROUP {
  8. BUTTON VONCPROJUV_PLAN { }
  9. }
  10. SEPARATOR { LINE; }
  11. GROUP {
  12. LINK VONCPROJUV_OBJ { ACCEPT { Obase; } }
  13. LINK VONCPROJUV_UVW { ACCEPT { Tuvw; } }
  14. LINK VONCPROJUV_AFF { ACCEPT { Tpolygonselection; } }
  15. LINK VONCPROJUV_SEL { ACCEPT { Tpolygonselection; } }
  16. BOOL VONCPROJUV_INVNOR { }
  17. }
  18. SEPARATOR { LINE; }
  19. GROUP {
  20. COLUMNS 2;
  21. BOOL VONCPROJUV_MAJAUTO { }
  22. BUTTON VONCPROJUV_ACTU { }
  23. }
  24. SEPARATOR { LINE; }
  25. GROUP {
  26. STATICTEXT VONCPROJUV_INFO { }
  27. }
  28. }
  29. }
复制代码

Fichier vonc_projuv.STR :
  1. STRINGTABLE vonc_projuv
  2. {
  3. VONCPROJUV_TITRE "ProjUV";
  4. VONCPROJUV_MAJAUTO "Mise \u00E0 jour auto";
  5. VONCPROJUV_ACTU "Actualiser";
  6. VONCPROJUV_OBJ "Projection";
  7. VONCPROJUV_UVW "UVW";
  8. VONCPROJUV_SEL "Limiter \u00E0 la s\u00E9lection";
  9. VONCPROJUV_AFF "Polygones affect\u00E9s";
  10. VONCPROJUV_PLAN "Cr\u00E9er une projection planaire modifiable";
  11. VONCPROJUV_INVNOR "Inverser la direction";
  12. VONCPROJUV_INFO "v 1.2 - C\u00E9sar Vonc - http://code.vonc.fr";
  13. }
复制代码







B Color Smilies

Comment :7

插件脚本
软件性质:
适用版本: C4D R15 - C4D 2026
软件版本: ProjUV - v 1.2
系统平台: Win MAC
软件语言: 汉化
插件来源: https://www.c4d.com.cn/c4dsoft.html

相关推荐

百套精美Kitbash3D模型专题合集下载
时尚卡通办公室人物C4D立体图标工程下载Cinema 4D Fashion Cartoon Office Character 3D Icon Project Download
C4D科技新闻片头电视栏目频道包装动画工程下载Cinema 4D Technology News Headline TV Program Channel Packaging Animation Project Download
关闭

C4D精选推荐 /10

知识
问答
快速回复 返回顶部 返回列表