Skip to content

Commit c6b0cfb

Browse files
Devostateddtzxporter
authored andcommitted
Cast Model import update
- usage of enums - added comments - usage of bools - parenting bones in an extra loop - minor cleanup Missing: - pole #60 (comment) - usage of c4d.quaternion() #60 (comment) - removing unpack_list steps #60 (comment)
1 parent 09d59f5 commit c6b0cfb

File tree

1 file changed

+43
-39
lines changed

1 file changed

+43
-39
lines changed

plugins/cinema4d/import_cast.pyp

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import math
55
from c4d import plugins, bitmaps, Vector, gui, BaseObject
66
import mxutils
77

8-
resDir = os.path.join(os.path.dirname(__file__), "res")
9-
mxutils.ImportSymbols(resDir)
10-
with mxutils.LocalImportPath(resDir):
8+
RESOURCE_DIR = os.path.join(os.path.dirname(__file__), "res")
9+
mxutils.ImportSymbols(RESOURCE_DIR)
10+
with mxutils.LocalImportPath(RESOURCE_DIR):
1111
from cast import Cast, CastColor, Model, Animation, Instance, File
1212

1313
__pluginname__ = "Cast"
@@ -164,7 +164,7 @@ def importModelNode(doc, node, model, path):
164164
newMesh.SetPoint(vertexIndex, Vector(x, y, -z)) # Matching the Maya default import
165165

166166
# Faces
167-
for i in range(0, len(faces), 3):
167+
for i in range(0, faceIndicesCount, 3):
168168
polyIndex = i // 3
169169
a, b, c = faces[i:i+3]
170170
newMesh.SetPolygon(polyIndex, c4d.CPolygon(a, b, c))
@@ -204,7 +204,11 @@ def importModelNode(doc, node, model, path):
204204
normalListUnpacked = unpack_list([(vertexNormals[x * 3], vertexNormals[(x * 3) + 1], vertexNormals[(x * 3) + 2]) for x in faces])
205205
normalList = [Vector(normalListUnpacked[i], normalListUnpacked[i+1], -normalListUnpacked[i+2]) for i in range(0, len(normalListUnpacked), 3)]
206206

207-
# Even if it's a Tri, you should pass a value.
207+
"""
208+
Raw data normal structure for one polygon is 12 int16 value (4 vectors
209+
for each vertex of a Cpolygon * 3 components for each vector), even if
210+
the Cpolygon is a triangle.
211+
"""
208212
for i in range(len(normalList) // 3):
209213
normalList.insert((i + 1) * 4 - 1, Vector(0, 0, 0))
210214

@@ -219,11 +223,10 @@ def importModelNode(doc, node, model, path):
219223
if model.Skeleton() is not None and node[CAST_IMPORT_BIND_SKIN]:
220224
skinObj = BaseObject(c4d.Oskin)
221225
skinningMethod = mesh.SkinningMethod()
222-
skinType = c4d.ID_CA_SKIN_OBJECT_TYPE
223226
if skinningMethod == "linear":
224-
skinObj[skinType] = 0
227+
skinObj[c4d.ID_CA_SKIN_OBJECT_TYPE] = c4d.ID_CA_SKIN_OBJECT_TYPE_LINEAR
225228
elif skinningMethod == "quaternion":
226-
skinObj[skinType] = 1
229+
skinObj[c4d.ID_CA_SKIN_OBJECT_TYPE] = c4d.ID_CA_SKIN_OBJECT_TYPE_QUAT
227230
doc.InsertObject(skinObj, parent=newMesh)
228231

229232
weightTag = c4d.modules.character.CAWeightTag()
@@ -247,7 +250,7 @@ def importModelNode(doc, node, model, path):
247250
)
248251
elif maximumInfluence > 0: # Fast path for simple weighted meshes
249252
weightBoneBuffer = mesh.VertexWeightBoneBuffer()
250-
for x in range(len(newMesh.vertices)):
253+
for x in range(vertexCount):
251254
weightTag.SetWeight(
252255
weightBoneBuffer[x],
253256
x,
@@ -261,7 +264,7 @@ def importModelNode(doc, node, model, path):
261264
material = materialArray[meshMaterial.Name()]
262265
material_tag = newMesh.MakeTag(c4d.Ttexture)
263266
material_tag[c4d.TEXTURETAG_MATERIAL] = material
264-
material_tag[c4d.TEXTURETAG_PROJECTION] = 6
267+
material_tag[c4d.TEXTURETAG_PROJECTION] = c4d.TEXTURETAG_PROJECTION_UVW
265268

266269
doc.InsertObject(newMesh, parent=modelNull)
267270
newMesh.Message(c4d.MSG_UPDATE)
@@ -286,56 +289,56 @@ def importSkeletonConstraintNode(skeleton, boneIndexes):
286289
# C4D's constraint system is a bit worse than Blender's
287290
constraintTag = c4d.BaseTag(CONSTRAINT_TAG)
288291
constraintBone.InsertTag(constraintTag)
289-
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR] = 1
292+
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR] = True
290293

291294
# Disabling all constraints, cause default is enabled
292-
constraintTag[CONSTRAIN_POS] = 0
293-
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_P_X] = 0
294-
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_P_Y] = 0
295-
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_P_Z] = 0
295+
constraintTag[CONSTRAIN_POS] = False
296+
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_P_X] = False
297+
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_P_Y] = False
298+
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_P_Z] = False
296299

297-
constraintTag[CONSTRAIN_SCALE] = 0
298-
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_S_X] = 0
299-
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_S_Y] = 0
300-
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_S_Z] = 0
300+
constraintTag[CONSTRAIN_SCALE] = False
301+
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_S_X] = False
302+
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_S_Y] = False
303+
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_S_Z] = False
301304

302-
constraintTag[CONSTRAIN_ROT] = 0
303-
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_R_X] = 0
304-
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_R_Y] = 0
305-
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_R_Z] = 0
305+
constraintTag[CONSTRAIN_ROT] = False
306+
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_R_X] = False
307+
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_R_Y] = False
308+
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_R_Z] = False
306309

307310
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_MAINTAIN] = constraint.MaintainOffset()
308311

309312
if type == "pt":
310-
constraintTag[CONSTRAIN_POS] = 1
313+
constraintTag[CONSTRAIN_POS] = True
311314
constraintTag[CONSTRAINT_TARGET] = targetBone
312-
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_LOCAL_P] = 1
315+
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_LOCAL_P] = True
313316
if not constraint.SkipX():
314-
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_P_X] = 1
317+
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_P_X] = True
315318
if not constraint.SkipY():
316-
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_P_Y] = 1
319+
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_P_Y] = True
317320
if not constraint.SkipZ():
318-
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_P_Z] = 1
321+
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_P_Z] = True
319322
elif type == "sc":
320-
constraintTag[CONSTRAIN_SCALE] = 1
323+
constraintTag[CONSTRAIN_SCALE] = True
321324
constraintTag[CONSTRAINT_TARGET] = targetBone
322-
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_LOCAL_S] = 1
325+
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_LOCAL_S] = True
323326
if not constraint.SkipX():
324-
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_S_X] = 1
327+
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_S_X] = True
325328
if not constraint.SkipY():
326-
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_S_Y] = 1
329+
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_S_Y] = True
327330
if not constraint.SkipZ():
328-
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_S_Z] = 1
331+
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_S_Z] = True
329332
elif type == "or":
330-
constraintTag[CONSTRAIN_ROT] = 1
333+
constraintTag[CONSTRAIN_ROT] = True
331334
constraintTag[CONSTRAINT_TARGET] = targetBone
332-
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_LOCAL_R] = 1
335+
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_LOCAL_R] = True
333336
if not constraint.SkipX():
334-
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_R_X] = 1
337+
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_R_X] = True
335338
if not constraint.SkipY():
336-
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_R_Y] = 1
339+
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_R_Y] = True
337340
if not constraint.SkipZ():
338-
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_R_Z] = 1
341+
constraintTag[c4d.ID_CA_CONSTRAINT_TAG_PSR_CONSTRAIN_R_Z] = True
339342
else:
340343
continue
341344

@@ -394,7 +397,7 @@ def importSkeletonNode(modelNull, skeleton):
394397
translation = Vector(tX, tY, -tZ)
395398

396399
rX, rY, rZ = utilityQuaternionToEuler(bone.LocalRotation())
397-
newBone.SetRotationOrder(5)
400+
newBone.SetRotationOrder(c4d.ROTATIONORDER_XYZGLOBAL)
398401

399402
scale_tuple = bone.Scale() or (1.0, 1.0, 1.0)
400403
scale = Vector(scale_tuple[0], scale_tuple[1], scale_tuple[2])
@@ -406,6 +409,7 @@ def importSkeletonNode(modelNull, skeleton):
406409
handles[i] = newBone
407410
boneIndexes[bone.Hash() or i] = newBone
408411

412+
for i, bone in enumerate(bones):
409413
if bone.ParentIndex() > -1:
410414
handles[i].InsertUnder(handles[bone.ParentIndex()])
411415
else:

0 commit comments

Comments
 (0)